简体   繁体   English

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

[英]Error while creating datasource in spring boot

Hi I am new to spring boot and trying to create a simple project but I am getting following error嗨,我是 Spring Boot 的新手,正在尝试创建一个简单的项目,但出现以下错误

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa. org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为 'org.springframework.boot.autoconfigure.orm.jpa 的 bean 时出错。 HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; HibernateJpaAutoConfiguration':通过构造函数参数 0 表达的不满足的依赖; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/sears/web/security/JpaConfiguration.class]: Initialization of bean failed;嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类路径资源 [com/sears/web/security/JpaConfiguration.class] 中定义名称为“dataSource”的 bean 创建时出错:bean 初始化失败; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceInitializer': Invocation of init method failed;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“dataSourceInitializer”的 bean 时出错:init 方法调用失败; nested exception is java.lang.NoSuchMethodError: org.springframework.jdbc.config.SortedResourcesFactoryBean.嵌套异常是 java.lang.NoSuchMethodError: org.springframework.jdbc.config.SortedResourcesFactoryBean。 afterPropertiesSet()V at org.springframework.beans.factory.support.ConstructorResolver. afterPropertiesSet()V 在 org.springframework.beans.factory.support.ConstructorResolver。 createArgumentArray(ConstructorResolver.java:749) ~[spring-beans- 4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support. 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] at 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] at 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.抽象AutowireCapableBeanFactory。 createBeanInstance(AbstractAutowireCapableBeanFactory.java:1095) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:513) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework. 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] Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/sears/web/security/JpaConfiguration.class]: Initialization of bean failed; 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] 中定义:bean 初始化失败; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceInitializer': Invocation of init method failed;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“dataSourceInitializer”的 bean 时出错:调用 init 方法失败; nested exception is java.lang.NoSuchMethodError: org.springframework.jdbc.config.SortedResourcesFactoryBean.afterPropertiesSet()V at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.s嵌套异常是 java.lang.NoSuchMethodError: org.springframework.jdbc.config.SortedResourcesFactoryBean.afterPropertiesSet()V at 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.s pringframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] pringframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceInitializer': Invocation of init method failed;引起:org.springframework.beans.factory.BeanCreationException: Error created bean with name 'dataSourceInitializer': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.springframework.jdbc.config.SortedResourcesFactoryBean.afterPropertiesSet()V at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:137) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:409) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1620) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractA嵌套异常是 java.lang.NoSuchMethodError: org.springframework.jdbc.config.SortedResourcesFactoryBean.afterPropertiesSet()V at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor) ~java:137s 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.RELEASE. .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.AbstractA utowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE] utowireCapableBeanFactory.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] [306]~[spring-beans-4.3.13.RELEASE.jar:4.3.13.RELEASE]

Caused by: java.lang.NoSuchMethodError: org.springframework.jdbc.config.SortedResourcesFactoryBean.afterPropertiesSet()V at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.doGetResources(DataSourceInitializer.java:163) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.getResources(DataSourceInitializer.java:147) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.getScripts(DataSourceInitializer.java:140) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE] at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runSchemaScripts(DataSourceInitializer.java:87) ~[spring-boot-autoconfigure-1.5.9.RELEASE.jar:1.5.9.RELEASE]引起:java.lang.NoSuchMethodError: org.springframework.jdbc.config.SortedResourcesFactoryBean.afterPropertiesSet()V at 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]

I am also adding pom.xml and DB configuration class我还添加了 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>

DB configuration java file数据库配置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;
}    

} }

Please help me with this issue.请帮我解决这个问题。 Sorry I couldn't format stacktrace.抱歉,我无法格式化堆栈跟踪。

The class SortedResourcesFactoryBean was implementing FactoryBean till version 3.0.1.RELEASE. SortedResourcesFactoryBean 类在 3.0.1.RELEASE 版本之前一直在实现 FactoryBean。 3.0.2.RELEASE onwards it started extending AbstractFactoryBean class which contain the method afterPropertiesSet().从 3.0.2.RELEASE 开始,它开始扩展包含方法 afterPropertiesSet() 的 AbstractFactoryBean 类。

Update your maven version of org.springframework from更新您的 org.springframework Maven 版本

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM