【问题标题】:How can i fix "Bean Error" in spring-boot?如何修复 spring-boot 中的“Bean Error”?
【发布时间】:2019-06-12 05:12:15
【问题描述】:

我遇到了 bean 错误...

这是错误代码:

Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testMapper' defined in file [C:\Users\user\Desktop\workspace\SpringBootSample-4\target\classes\com\simplify\sample\db\mapper\TestMapper.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 [com/simplify/sample/db/DatabaseConfig.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$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.microsoft.sqlserver.jdbc.SQLServerDriver 
Related cause: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testMapper' defined in file [C:\Users\user\Desktop\workspace\SpringBootSample-4\target\classes\com\simplify\sample\db\mapper\TestMapper.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 [com/simplify/sample/db/DatabaseConfig.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$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.microsoft.sqlserver.jdbc.SQLServerDriver 
2019-06-12 13:50:03.183  INFO 10780 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2019-06-12 13:50:03.242  INFO 10780 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-06-12 13:50:03.320 ERROR 10780 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

这是配置:

@Configuration
@MapperScan(basePackages="com.simplify.sample.db.mapper")
@EnableTransactionManagement
public class DatabaseConfig {

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sessionFactory.setMapperLocations(resolver.getResources("classpath:mybatis/mapper/*.xml"));
        return sessionFactory.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) throws Exception {
      final SqlSessionTemplate sqlSessionTemplate = new SqlSessionTemplate(sqlSessionFactory);
      return sqlSessionTemplate;
    }


}

update-1 mssql 版本

       <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>6.4.0.jre8</version>
            <scope>test</scope>
        </dependency>

它有什么问题? 谢谢你的有趣! spring-boot, mybatis, mssql

【问题讨论】:

  • 您能否也发布您的 pom.xml,以查看您如何加载 MSSQL Server 驱动程序/
  • 检查您的堆栈跟踪,它显示“...无法加载驱动程序类:com.microsoft.sqlserver.jdbc.SQLServerDriver...”。检查类路径上是否有 JDBC 驱动程序。
  • 尝试将 mssql-jdbc 的 POM 范围从 test 更改为 runtime。所以在运行app的时候可以找到丢失的类。

标签: java spring-boot mybatis mssql-jdbc


【解决方案1】:

只需从 sqlserver 依赖项中删除范围:

    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>6.4.0.jre8</version>
    </dependency>

您在运行应用程序时需要此驱动程序。

【讨论】:

    猜你喜欢
    • 2019-09-10
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 1970-01-01
    • 2019-10-22
    • 2019-02-16
    • 2020-01-05
    相关资源
    最近更新 更多