[英]Spring JavaConfig - NoSuchBeanDefinitionException: No unique bean of type
I would like to test the Spring @Configuration
with a small test case, but it throws error 我想用一个小的测试用例来测试Spring
@Configuration
,但是会抛出错误
org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.mycompany.MavenVaadinSpring.service.UserManager] is defined:
org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义[com.mycompany.MavenVaadinSpring.service.UserManager]类型的唯一Bean:
What have I done wrong? 我做错了什么? My code is below:
我的代码如下:
AppConfigTest.java
: AppConfigTest.java
:
public class AppConfigTest {
public static void main(String[] args) {
JavaConfigApplicationContext context = new JavaConfigApplicationContext(AppConfig.class);
UserManager userManager = context.getBean(UserManager.class);
}
}
AppConfig.java
@Configuration
@ResourceBundles("classpath:jdbc-mysql")
public abstract class AppConfig {
@Bean
public BasicDataSource dataSource() {
BasicDataSource basicDataSource = new BasicDataSource();
return basicDataSource;
}
@Bean
public UserDao userDao() {
UserDao UDI = new UserDaoImpl();
UDI.setDataSource(dataSource());
return UDI;
}
@Bean
public UserManager userManager() {
UserManager UM = new UserManagerImpl();
UM.setUserDao(userDao());
return UM;
}
abstract @ExternalValue("jdbc.url") String url();
abstract @ExternalValue("jdbc.username") String username();
abstract @ExternalValue("jdbc.password") String password();
abstract @ExternalValue("jdbc.jdbc.driverClassName") String classname();
}
Stacktrace: 堆栈跟踪:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.mycompany.MavenVaadinSpring.service.UserManager] is defined:
at org.springframework.config.java.context.TypeSafeBeanFactoryUtils.getBean(TypeSafeBeanFactoryUtils.java:46)
at org.springframework.config.java.context.JavaConfigApplicationContext.getBean(JavaConfigApplicationContext.java:378)
at com.mycompany.MavenVaadinSpring.AppConfigTest.main(AppConfigTest.java:11)
It appears you are using the standalone spring-javaconfig project here (as evidenced by the .config.java packaging and use of the JavaConfigApplicationContext class). 看来您在这里使用的是独立的spring-javaconfig项目(.config.java包装和JavaConfigApplicationContext类的使用证明了这一点)。 This project is no longer supported since its inclusion in Spring 3.0.
由于该项目已包含在Spring 3.0中,因此不再受支持。 The packaging has changed, and JavaConfigApplicationContext is now called AnnotationConfigApplicationContext.
包装已更改,并且JavaConfigApplicationContext现在称为AnnotationConfigApplicationContext。 The overall programming model, however, is the same.
但是,总体编程模型是相同的。
Give this same scenario a shot against a release of Spring 3 and see if you get the same results. 让这个场景与Spring 3的发布相吻合,看看是否得到相同的结果。 If you need a GA release, Spring 3.0.6 is now available;
如果需要GA版本,则可以使用Spring 3.0.6。 if you can deal with milestones, try Spring 3.1 M2, or even a nightly snapshot of Spring 3.1.
如果您可以处理里程碑,请尝试使用Spring 3.1 M2,甚至是Spring 3.1的夜间快照。 You'll find Java configuration support has been significantly enhanced in these later versions.
您会发现Java配置支持在这些更高版本中得到了显着增强。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.