2016-04-28 89 views
2

我爲webapp使用springboot,我試圖設置一個外部目錄,它將容納最終用戶可以選擇使用的各種JDBC驅動程序。要做到這一點,我補充說:SpringBoot loader.path無法加載外部罐子

loader.path=/opt/myapp/lib/ 

我application.properties文件,這是由PropertySourcesPropertyResolver

2016-04-28 17:27:38.739 DEBUG 22539 --- [restartedMain] o.s.c.e.PropertySourcesPropertyResolver : Found key 'loader.path' in [applicationConfigurationProperties] with type [String] and value '/opt/myapp/lib/' 

我的問題是,我似乎無法加載任何JDBC驅動程序回升我放入這個目錄的任何罐子,我錯過了什麼?我正在使用默認的嵌入式tomcat服務器。當我嘗試使用Class.forName加載驅動程序時,我得到以下內容,例如該目錄中不存在罐子。

public Connection buildConnection(DataSource dataSource) throws ClassNotFoundException, SQLException { 

    if (dataSource == null) { 
     throw new NullPointerException("Data Source is null!"); 
    } 

    if (!dataSource.isReady()) { 
     throw new IllegalArgumentException("Data Source is reporting that it is not ready!"); 
    } 

    logger.debug("Loading JDBC Driver: {}", dataSource.getDriverClass()); 
    Class.forName(dataSource.getDriverClass()); 
    logger.debug("Loaded Driver: {}", dataSource.getDriverClass()); 

    logger.debug("Attempting to build connection using: {}", dataSource.getConnectionString()); 

    DriverManager.setLoginTimeout(10); 
    Connection c = DriverManager.getConnection(dataSource.getConnectionString(), dataSource.getUserName(), dataSource.getPassword()); 
    if (c != null) { 
     c.setAutoCommit(true); 
     c.setReadOnly(true); 
     return c; 
    } 
    throw new NullPointerException("Unable to create connection!"); 
} 

這是異常被拋出

2016-04-28 17:38:53.525 DEBUG 22539 --- [nio-8081-exec-5] c.c.reportout.processor.JobProcessor  : Loading JDBC Driver: com.mysql.jdbc.Driver 
2016-04-28 17:38:53.526 WARN 22539 --- [nio-8081-exec-5] c.c.reportout.processor.JobProcessor  : Unable to successfully test connection: com.mysql.jdbc.Driver 

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver 
at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_91] 
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_91] 
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_91] 
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_91] 
at org.springframework.boot.devtools.restart.classloader.RestartClassLoader.loadClass(RestartClassLoader.java:151) ~[spring-boot-devtools-1.3.3.RELEASE.jar:1.3.3.RELEASE] 
at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_91] 
at java.lang.Class.forName0(Native Method) ~[na:1.8.0_91] 
at java.lang.Class.forName(Class.java:264) ~[na:1.8.0_91] 

任何指針,我怎麼能調試這還是我在做什麼錯?

感謝

+0

您是如何構建和打包應用程序的? 'loader.path'只有在使用'PropertiesLauncher'時纔有效果。 –

+0

是的,我做過。我只是構建了jar並使用java -jar MyApp-1.0-SNAPSHOT.jar運行它,並獲得了相同的結果。 – csyperski

+0

如果我將jdbc驅動程序依賴項添加到我的pom中,它顯然可以工作,但由於某種原因我無法從外部目錄加載它。 – csyperski

回答

5

所以經過多次使用Google,事實證明,我能夠加入到解決這個問題:

<configuration> 
    <layout>ZIP</layout> 
</configuration> 

的彈簧引導Maven的插件插件在我的POM文件。所以現在的工作版本看起來像這樣:

<plugins> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
      <version>${spring-boot.version}</version> 
      <configuration> 
       <layout>ZIP</layout> 
      </configuration> 
      <executions> 
       <execution> 
        <goals> 
         <goal>repackage</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
+0

因爲我可以告訴它,它只是告訴Spring Boot在運行可執行jar時使用''PropertiesLauncher'',而不是在使用''mvn spring-boot:run''時幫助任何人 –