java_enthu
java_enthu

Reputation: 2327

Does order matter while injecting properties in ProxyFactoryBean

I am trying to inject the aspects in a service. For this service I am creating a proxied object using classic way.

I have written a bean- baseProxy of type (ProxyFactoryBean) which contains a list of all the required advices.

    <bean id="baseProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="interceptorNames">
        <list>
            <value>methodInvocationAdvice</value>
        </list>
    </property>
</bean>

I am creating a proxy for the service like this :

<bean id="singproxy" parent="baseProxy">
    <property name="target" ref="singtarget" />
    <property name="targetClass" value="com.spring.learning.SingingService"></property>
</bean>

Which doesn't work but when I revert these two properties and write like this :

<bean id="singproxy" parent="baseProxy">
    <property name="targetClass" value="com.spring.learning.SingingService"></property>
    <property name="target" ref="singtarget" />
</bean>

To my surprise it works fine. In spring does it matter on the order for bean ? Or its a special case with ProxyFactoryBean? I tried with Spring 3.0 I am not sure same behavior exists with previous versions.

Upvotes: 2

Views: 1028

Answers (1)

Sean Patrick Floyd
Sean Patrick Floyd

Reputation: 298878

Concerning target and targetClass, It's one or the other, but not both. Here's the relevant source (from org.springframework.aop.framework.AdvisedSupport), a parent class of ProxyFactoryBean:

public void setTarget(Object target) {
    setTargetSource(new SingletonTargetSource(target));
}

public void setTargetSource(TargetSource targetSource) {
    this.targetSource = (targetSource != null ? targetSource : EMPTY_TARGET_SOURCE);
}


public void setTargetClass(Class targetClass) {
    this.targetSource = EmptyTargetSource.forClass(targetClass);
}

As you can see, both setTarget() and setTargetClass() write to the same field, so the last assignment wins.

Upvotes: 1

Related Questions