在 Spring Boot 中创建数据源时出错

Posted

技术标签:

【中文标题】在 Spring Boot 中创建数据源时出错【英文标题】:Error while creating datasource in spring boot 【发布时间】:2018-10-13 12:36:57 【问题描述】:

您好,我是 Spring Boot 新手,正在尝试创建一个简单的项目,但出现以下错误

org.springframework.beans.factory.UnsatisfiedDependencyException: 错误 用名字创建bean 'org.springframework.boot.autoconfigure.orm.jpa. HibernateJpaAutoConfiguration': 通过构造函数参数 0 表示的不满足的依赖关系;嵌套的 异常是 org.springframework.beans.factory.BeanCreationException: 错误 创建在类路径资源中定义的名称为“dataSource”的bean [com/sears/web/security/JpaConfiguration.class]:bean的初始化 失败的;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建 bean 时出错 名称为“dataSourceInitializer”:调用 init 方法失败;嵌套的 例外是 java.lang.NoSuchMethodError: org.springframework.jdbc.config.SortedResourcesFactoryBean。 afterPropertiesSet()V 在 org.springframework.beans.factory.support.ConstructorResolver。 createArgumentArray(ConstructorResolver.java:749) ~[spring-beans- 4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support。 ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~ [spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support。 AbstractAutowireCapableBeanFactory.autowireConstructor (AbstractAutowireCapableBeanFactory.java:1193)~[spring-beans- 4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support. AbstractAutowireCapableBeanFactory。 createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 原因: org.springframework.beans.factory.BeanCreationException:错误 创建在类路径资源中定义的名称为“dataSource”的bean [com/sears/web/security/JpaConfiguration.class]:初始化 豆失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为“dataSourceInitializer”的 bean:调用 init 方法失败;嵌套异常是 java.lang.NoSuchMethodError: org.springframework.jdbc.config.SortedResourcesFactoryBean.afterPropertiesSet()V 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]

引起:org.springframework.beans.factory.BeanCreationException: 创建名为“dataSourceInitializer”的 bean 时出错:调用 初始化方法失败;嵌套异常是 java.lang.NoSuchMethodError: org.springframework.jdbc.config.SortedResourcesFactoryBean.afterPropertiesSet()V 在 org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] 在 org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]

引起:java.lang.NoSuchMethodError: org.springframework.jdbc.config.SortedResourcesFactoryBean.afterPropertiesSet()V 在 org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.doGetResources(DataSourceInitializer.java:163) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE] 在 org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.getResources(DataSourceInitializer.java:147) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE] 在 org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.getScripts(DataSourceInitializer.java:140) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE] 在 org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runSchemaScripts(DataSourceInitializer.java:87) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]

我也在添加 pom.xml 和 DB 配置类

 <?xml version="1.0" encoding="UTF-8"?>
 <project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
 <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
    </parent>
     <groupId>XXXXXXX</groupId>
  <artifactId>XXX</artifactId>
<version>1.0</version>
<packaging>war</packaging>
   <name>spring-boot-web-mvc</name>
  <description>
    Configure spring boot starter project for Web MVC as a WAR file, still 
  self executing.
 </description>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>com.mvmlabs.springboot.Application</start-class>
    <java.version>1.8</java.version>
    <main.basedir>$basedir/../..</main.basedir>
    <m2eclipse.wtp.contextRoot>/</m2eclipse.wtp.contextRoot>
 </properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
<!-- Added to allow configuration as a web MVC, built as a WAR file (still 
executable) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
<!-- Add Apache Tiles into the mix -->
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-jsp</artifactId>
        <version>3.0.8</version>
    </dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.ldap</groupId>
    <artifactId>spring-ldap-core</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-ldap</artifactId>
</dependency>
<dependency>
    <groupId>com.unboundid</groupId>
    <artifactId>unboundid-ldapsdk</artifactId>
</dependency>
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.2.Final</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.0.1.RELEASE</version>
</dependency> 
<dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc14</artifactId>
        <version>10.2.0.4</version>
    </dependency>
 <dependency>
 <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<!-- <dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.2.Final</version>
</dependency> -->
 <!-- https://mvnrepository.com/artifact/javax.persistence/persistence-api - 
 ->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId>
<version>1.0</version>
 </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
 </build>
</project>

数据库配置java文件

  
 @Configuration
 @EntityScan("com.sears")
 @EnableTransactionManagement
 @PropertySource("classpath:application.properties")

  public class JpaConfiguration 

@Value("$spring.datasource.driver-class-name") String driverClassName;
@Value("$spring.datasource.url") String url;
@Value("$spring.datasource.username") String username;
@Value("$spring.datasource.password") String password;

@Bean(name = "dataSource")

public DataSource getDataSource() throws SQLException 
    System.out.println("inside data source");

    DataSource dataSource = DataSourceBuilder
            .create()
            .username(username)
            .password(password)
            .url(url)
            .driverClassName(driverClassName)
            .build();
    return dataSource;

@Bean(name = "sessionFactory")
public SessionFactory getSessionFactory(DataSource dataSource) 

    LocalSessionFactoryBuilder sessionBuilder = new 
 LocalSessionFactoryBuilder(dataSource);
    sessionBuilder.scanPackages("com.sears");
    return sessionBuilder.buildSessionFactory();

@Bean(name = "transactionManager")
public HibernateTransactionManager getTransactionManager(
        SessionFactory sessionFactory) 
    HibernateTransactionManager transactionManager = new 
 HibernateTransactionManager(
            sessionFactory);
    return transactionManager;
   
@Bean
public DataSourceInitializer dataSourceInitializer(final DataSource 
 dataSource) 
    final DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    return initializer;
    

请帮我解决这个问题。抱歉,我无法格式化堆栈跟踪。

【问题讨论】:

【参考方案1】:

在 3.0.1.RELEASE 版本之前,SortedResourcesFactoryBean 类一直在实现 FactoryBean。 从 3.0.2.RELEASE 开始,它开始扩展 AbstractFactoryBean 类,其中包含方法 afterPropertiesSet()。

更新您的 org.springframework 的 maven 版本
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>3.0.1.RELEASE</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.1.4.RELEASE</version>
</dependency>

【讨论】:

以上是关于在 Spring Boot 中创建数据源时出错的主要内容,如果未能解决你的问题,请参考以下文章

在 Spring Boot 中创建自定义查询时出错

在 Spring Boot 中创建名为“entityManagerFactory”的 bean 时出错

在 Spring Boot 中创建名为“batchConfigurer”的 bean 时出错

在 WebFlux 中创建名为 requestMappingHandlerMapping 的 bean 时出错(没有 Spring Boot)

在 Spring Boot Web 应用程序中创建名称为“entityManagerFactory”的 bean 时出错

在CF中创建数据源时选择Spring Boot应该使用的参数?