jchips12
jchips12

Reputation: 1227

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

Upvotes: 1

Views: 52

Answers (1)

Biju Kunjummen
Biju Kunjummen

Reputation: 49915

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/>

Upvotes: 1

Related Questions