简体   繁体   English

当该bean已经通过xml设置了另一个属性时,是否可以通过自动装配注入属性?

[英]Is it possible to inject a property through autowired when that bean already has another property set through xml

I'm trying to define a bean. 我正在尝试定义一个bean。

<bean id="prop1" class="Prop1"/>

<bean id="myBean" class="myClass">
    <property name="prop2">
        <bean class="Prop2">
    </property>
</bean>

Now I have this class 现在我上这堂课

public class myClass(){
    @Autowired
    private Prop1 prop1;
    private Prop2 prop2;

    public setProp2(Prop2 prop2){
        this.prop2 = prop2;
    }
    ...
}

Im aware that I can just add property to myBean bean but I am trying to avoid that. 我知道我可以将属性添加到myBean bean中,但是我试图避免这种情况。 Right now im getting null on prop1. 现在,我在prop1上获得了空值。 Is it possible to initialized prop1 ? 可以初始化prop1吗? If not please explain or give me a link to read about this. 如果没有,请解释或给我链接以阅读有关此内容。

TIA TIA

This should just work. 这应该工作。 It is probably not working because you are likely missing a AutowiredAnnotationPostProcessor , which is a bean post processor responsible for wiring in @Autowired dependencies. 它可能不起作用,因为您可能缺少AutowiredAnnotationPostProcessor ,它是负责在@Autowired依赖项中进行接线的bean后处理器。 You can get it by just adding these to the xml config file: 您可以通过将它们添加到xml配置文件中来获得它:

<context:annoation-config/>

Or 要么

<context:component-scan/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM