org.springframework.beans.factory.UnsatisfiedDependencyException

最近在用spring boot ,mybatis ,sqlserver 2000,STS3.9 、jdk8 ,sqljdbc4 1.0-2000的一个环境,开发新的功能,这个环境是我同事搭建的在他那里可以正常运行。但在我这里启动过程中发现一个异常:org.springframework.beans.factory.UnsatisfiedDependencyException,考虑过彼此环境问题的不一致(除了编辑器不一样),检查了代码及相关配制,在百度上查询了很多,研究了一天,最终才发现原因所在。

具体错误展现如下:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'supplierBiz': Unsatisfied dependency expressed through field 'supplierDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'supplierDao' defined in file [D:\STS-CODE\yzyb\target\classes\com\cn\lbx\dao\SupplierDao.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Generic.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.microsoft.jdbc.sqlserver.SQLServerDriver
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.5.RELEASE.jar:5.1.5.RELEASE]

经过分析,它是不能创建bean及不能加载驱动。所以一开始就去检查代码问题,及驱动是否正常加载,找了很久也没有发现问题。从这个角度上去百度,基本上是没有找到解决的方法。

后来在搜索过程当中,换了一个角度去思考,看网上别人的环境是怎么搭建的,这才发现问题的细节。

百度上的说明

连接SqlServer2000 

Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
URL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=tempdb"; 

连接SqlServer2005 

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 
URL = "jdbc:sqlserver://localhost:1433;DatabaseName=tempdb"; 

本地环境配制:

org.springframework.beans.factory.UnsatisfiedDependencyException_第1张图片

经过确认自己所连的sqlserver版本为2000,然后检查自己配制,driver-class-name写错了!!!,我的与百度的正好相反,连接sqlserver2000,driver-class-name却是sqlserver2005写法,这也是是和驱动包有关。

现在启动的问题解决了,细心的朋友应该还会发现有地方配制错了,下面一篇中将会介绍到。

你可能感兴趣的:(经验分享,spring,boot,sqlserver2000)