沒有獨特的豆,我想用一個小的測試案例,測試春天@Configuration
,但它會拋出錯誤春季JavaConfig - NoSuchBeanDefinitionException:類型
org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有獨特的豆類型[com.mycompany.MavenVaadinSpring.service.UserManager]的定義如下:
我做了什麼錯了?我的代碼如下:
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();
}
堆棧跟蹤:
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)
巴洛斯感謝您改善我的問題的前景 – cscsaba