Spring JavaConfig - NoSuchBeanDefinitionException:没有类型的唯一bean [英] Spring JavaConfig - NoSuchBeanDefinitionException: No unique bean of type

查看:568
本文介绍了Spring JavaConfig - NoSuchBeanDefinitionException:没有类型的唯一bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一个小测试用例来测试Spring @Configuration ,但它会抛出错误

I would like to test the Spring @Configuration with a small test case, but it throws error


org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义[com.mycompany.MavenVaadinSpring.service.UserManager]类型的唯一bean:

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:

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)


推荐答案

看来你在这里使用独立的spring-javaconfig项目(由.config.java打包和使用JavaConfigApplicationContext类证明)。自从包含在Spring 3.0中以来,该项目不再受支持。包装已更改,JavaConfigApplicationContext现在称为AnnotationConfigApplicationContext。然而,整体编程模型是相同的。

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.

针对Spring 3的版本提供相同的场景,看看你是否得到了相同的结果。如果您需要GA版本,现在可以使用Spring 3.0.6;如果您可以处理里程碑,请尝试Spring 3.1 M2,甚至是Spring 3.1的夜间快照。您会发现Java配置支持在这些更高版本中得到了显着增强。

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.

这篇关于Spring JavaConfig - NoSuchBeanDefinitionException:没有类型的唯一bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆