Andreas Steffan
Andreas Steffan

Reputation: 6159

Using a MethodInterceptor to wrap calls to protected method

I would like to use XML based Spring configuration to wrap calls to a protected method in a 3rd party class. I have wired up some spring classes from org.springframework.aop.support. It works for public methods, but it fails for protected ones:

<bean id="sampleAutoProxyCreator" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="proxyTargetClass" value="true" />
<property name="beanNames">
    <list>
        <value>thrirdPartyBean</value>
    </list>
</property>
<property name="interceptorNames">
    <list>
        <value>sampleAdvisor</value>
    </list>
</property>
</bean>
<bean id="sampleMethodNamePointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">
    <property name="mappedNames">
        <list>
            <value>publicMethodThatWorks</value>
            <value>protectedMethodThatDoesNotWork</value>
        </list>
    </property>
</bean>
<bean id="sampleAdvice" class="sample.MyMethodInterceptor" />
<bean id="sampleAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
    <property name="pointcut" ref="sampleMethodNamePointcut" />
    <property name="advice" ref="sampleAdvice" />
</bean>

How can I tweak this to work with protected methods ?

Upvotes: 0

Views: 945

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279920

As the linked question/answer in the comments states, Springs AOP proxies can only apply to public methods.

With JDK proxies, this isn't possible because the proxy only has your target object's interface types so you can only interact with it through its public methods (remember that all methods declared in an interface are public).

With GGLIB proxies, because the proxy does have the target object's class type, you can interact with its protected methods. I would think for reasons of consistency between proxying mechanisms they would not allow it.

Upvotes: 0

Related Questions