简体   繁体   中英

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 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. Right now im getting null on prop1. Is it possible to initialized prop1 ? If not please explain or give me a link to read about this.

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. You can get it by just adding these to the xml config file:

<context:annoation-config/>

Or

<context:component-scan/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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