I would like to test the Spring @Configuration with a small test case, but it throws error
org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.mycompany.MavenVaadinSpring.service.UserManager] is defined:
What have I done wrong? My code is below:
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). This project is no longer supported since its inclusion in Spring 3.0. The packaging has changed, and JavaConfigApplicationContext is now called 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. If you need a GA release, Spring 3.0.6 is now available; if you can deal with milestones, try Spring 3.1 M2, or even a nightly snapshot of Spring 3.1. You’ll find Java configuration support has been significantly enhanced in these later versions.