我正在使用以下POM版本:
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springframework.version>4.3.5.RELEASE</springframework.version>
<springsecurity.version>4.0.4.RELEASE</springsecurity.version>
<hibernate.version>4.3.11.Final</hibernate.version>
<mysql.connector.version>5.1.31</mysql.connector.version>
</properties>
.
.
.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.2.4.RELEASE</version>
</dependency>
.
.
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.1.3.Final</version>
</dependency>
.
.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
以及测试库:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.9.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
<scope>test</scope>
<type>jar</type>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.web</groupId>
<artifactId>javax.el</artifactId>
<version>2.2.4</version>
</dependency>
我在@Configuration
批注中包含了每个用@ContextConfiguration(classes={...}
批注的类,但是仍然出现相同的错误,无法加载应用程序上下文。我知道我必须在这里添加每个依赖项,因此我已经将所有类添加到ContextConfiguration中,并且仍然相同。
我尝试在新创建的配置类上使用ComponentScan批注,但它仍然相同。
请注意,我使用的不是注解配置,而是xml文件。
错误多行之一:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateTokenRepositoryImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.hibernate.SessionFactory com.company.projectname.dao.AbstractDao.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.company.api.configuration.spirng.HibernateConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.hibernate4.LocalSessionFactoryBean]: Factory method 'sessionFactory' threw exception; nested exception is java.lang.IllegalStateException: @Bean method HibernateConfiguration.dataSource called as a bean reference for type [javax.sql.DataSource] but overridden by non-compatible bean instance of type [com.company.projectname.model.DataSource]. Overriding bean of same name declared in: null
这是@Configuration类之一:
@Configuration
@EnableWebMvc
@EnableScheduling
@ComponentScan(basePackages = {"com.company.api", "com.company.project"})
public class SpringConfiguration extends WebMvcConfigurerAdapter {
/**
* Added @Lazy to prevent using messageSource in UserService before its
* being created here
*/
@Autowired
@Lazy
RoleConverter roleConverter;
/**
* Added @Lazy to prevent using messageSource in UserService before its
* being created here
*/
@Autowired
@Lazy
ReportConverter reportConverter;
/**
* Added @Lazy to prevent using messageSource in UserService before its
* being created here
*/
@Autowired
@Lazy
GroupConverter groupConverter;
@Autowired
@Lazy
ReportStyleConverter reportStyleConverter;
@Autowired
@Lazy
LanguageConverter languageConverter;
@Autowired
@Lazy
UserProfileNotificationEventConverter userProfileNotificationEventConverter;
@Autowired
private ContextHolder context;
.
.
.
Other beans and functions
}