自动装配以@Component注释的非托管Bean [英] Autowiring Unmanaged Beans Annotated With @Component

查看:154
本文介绍了自动装配以@Component注释的非托管Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用@AutoWired将配置有@Component的非托管bean注入到托管bean中.我很确定我拥有正确的配置,但是由于某些原因,我不断收到异常:

I want to use @AutoWired to inject a non-managed bean configured with @Component into a managed bean. I'm pretty sure I have the configuration right, but for some reason I keep getting the exception:

No unique bean of type [foo.Baz] is defined: Unsatisfied dependency of type [class foo.Baz]: expected at least 1 matching bean

基于该错误,我猜测它无法找到Baz类,但是我不确定为什么.据我了解,应该在XML配置中使用context:spring-configured元素来执行此操作.我还确保包括适当的jar文件(spring-weaving.jar和Aspectjweaver.jar).

Based on the error, I'm guessing it's not able to find the Baz class, but I'm not sure why. It's my understanding that the context:spring-configured element in the XML config was supposed to allow me to do this. I also made sure to include the appropriate jar files (spring-weaving.jar and aspectjweaver.jar).

这是我设置的一个简单示例.

Here's a simple example of my set up.

我的XML配置:

<beans ...>
    ...

    <context:annotation-config/>
    <context:spring-configured/>
    <context:component-scan base-package="foo"/>

    <bean id="bar" class="foo.Bar"/>
    ...
</beans>

我有一个托管bean:

I have one managed bean:

package foo;

public class Bar {

    @Autowired
    private Baz baz;

    public void setBaz(Baz baz) {
        this.baz = baz;
    }

    ...
}

和一个非托管bean:

And one unmanaged bean:

package foo;

@Component
public class Baz {
    ...
}

有什么我想念的吗?

EDIT :日志中列出了实例化的bean,而foo.Baz不是其中之一.我不知道为什么它不使用@Component带注释的类.

EDIT: The log lists the beans its instantiating, and foo.Baz isn't one of them. I don't know why it's not picking up the @Component annotated class.

推荐答案

由于Bar配置了xml,因此只能配置xml.也就是说,您不能将它们混合在一起.这样就不会拾取Baz上的"@Autowired"注解(所有注解都不会).只有在类级别添加spring注释时,spring才会侦听其他任何注释.

Because Bar is configured with xml, it can only be configured with xml. i.e. you can't mix them. So that "@Autowired" annotation on Baz is not getting picked up (none of the annotations would be). It is only when you add the spring annotation at class level that spring will listen to any of the other annotations.

您需要做的是在xml中将bean配置为按类型自动装配,为该类型添加一个setter,您将实现所需的行为.

What you'll need to do is in the xml configure the bean to be autowired by type, add a setter for that type and you'll achieve the desired behaviour.

<bean id="bar" class="foo.Bar" autowire="byType"/>

还有一件事情,当您使用@Component注释bean时,它 是一个弹簧管理的bean.仅仅因为它不是用xml创建的,并不意味着它是不受管的.不受管理的Bean是您从春季就无法获得的.

One more thing, when you annotate a bean with @Component it is a spring managed bean. Just because it is not created with xml does not mean it is unmanaged. An unmanaged bean is one you don't get from spring.

Bar和Baz都在春季进行管理.您选择用来定义它们的机制有所不同.

Bar and Baz are both spring managed. It is the mechanism you've chosen to define them that differs.

这篇关于自动装配以@Component注释的非托管Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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