简体   繁体   中英

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 ?

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.

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