2015-01-15 164 views
1

我正在開發Spring Batch程序,將其作爲命令行應用程序運行(目前通過STS進行測試)。當我通過org.springframework.batch.core.launch.support.CommandLineJobRunner通過Eclipse運行/調試配置運行它時,它無法加載SQL Server JDBC驅動程序。我確定我在類路徑上有它,但顯然它仍然存在問題。無法加載JDBC驅動程序類 - Spring批處理獨立程序

下面是從控制檯登錄我的異常跟蹤:

09:11:59.291 [main] ERROR o.s.b.c.l.s.CommandLineJobRunner - Job Terminated in error: Error creating bean with name 'legacyDatasource' defined in class path resource [META-INF/spring/module-context.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: 
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.microsoft.jdbc.sqlserver.SQLServerDriver] 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'legacyDatasource' defined in class path resource [META-INF/spring/module-context.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are: 
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.microsoft.jdbc.sqlserver.SQLServerDriver] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1506) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) ~[spring-context-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.batch.core.launch.support.CommandLineJobRunner.start(CommandLineJobRunner.java:290) [spring-batch-core-3.0.2.RELEASE.jar:3.0.2.RELEASE] 
    at org.springframework.batch.core.launch.support.CommandLineJobRunner.main(CommandLineJobRunner.java:590) [spring-batch-core-3.0.2.RELEASE.jar:3.0.2.RELEASE] 
Caused by: org.springframework.beans.PropertyBatchUpdateException: Failed properties: Property 'driverClassName' threw exception; nested exception is java.lang.IllegalStateException: Could not load JDBC driver class [com.microsoft.jdbc.sqlserver.SQLServerDriver] 
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:121) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1502) ~[spring-beans-4.1.4.RELEASE.jar:4.1.4.RELEASE] 
    ... 14 common frames omitted 

從Spring的XML我的數據源配置(詳細信息已更改,以保護無辜的課程):

<bean id="legacyDatasource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource"> 
    <property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" /> 
    <property name="url" value="jdbc:microsoft:sqlserver://server:1433;DatabaseName=database" /> 
    <property name="username" value="user" /> 
    <property name="password" value="pw" /> 
</bean> 

現在到classpath東西...安裝在我本地的maven回購:

enter image description here

在我的POM:

<dependency> 
    <groupId>com.microsoft.sqlserver</groupId> 
    <artifactId>sqljdbc41</artifactId> 
    <version>4.1</version> 
</dependency> 

而且它在我的項目的Maven依賴:

enter image description here

在我跑的配置,我試過直接將它添加到類路徑Classpath選項卡上,並通過虛擬機參數。

enter image description here

我敢肯定,這是簡單的東西。任何想法爲什麼驅動程序無法加載?對我來說還值得注意的是,它不是一個沒有找到類的錯誤(雖然它可能在封面而不是這個堆棧跟蹤的一部分)。

回答

1

如果jdbc & sqlserver轉換爲包名稱。

<bean id="legacyDatasource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource"> 
    <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" /> 
    <property name="url" value="jdbc:sqlserver://server:1433;DatabaseName=database" /> 
    <property name="username" value="user" /> 
    <property name="password" value="pw" /> 
</bean> 
相關問題