您好我是春季启动的新手,并尝试创建一个简单的项目,但我收到了以下错误
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name
'org.springframework.boot.autoconfigure.orm.jpa.
HibernateJpaAutoConfiguration':
Unsatisfied dependency expressed through constructor parameter 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; nested exception is
org.springframework.beans.factory.BeanCreationException: Error creating 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.support.ConstructorResolver.
createArgumentArray(ConstructorResolver.java:749) ~[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]
at
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。 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] at 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]中定义:bean的初始化失败;嵌套异常是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] at 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'的init时出错:init方法的调用失败;嵌套异常是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] at 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;
}
}
请帮我解决这个问题。抱歉,我无法格式化堆栈跟踪。
答案 0 :(得分:1)
SortedResourcesFactoryBean类正在实现FactoryBean,直到版本3.0.1.RELEASE。 3.0.2.RELEASE从此开始扩展AbstractFactoryBean类,其中包含afterPropertiesSet()方法。
从
更新您的maven版本的org.springframework<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>