Announcement Announcement Module
Collapse
No announcement yet.
intercepting protected methods Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • intercepting protected methods

    I'm sort of new to the AOP scene and I haven't found much in spring documentation on intercepting protected methods or even if that was possible.

    I have an example program that uses spring's AOP system and it appears that the cglib-modded classes do not forward protected method calls between instances of classes in the same package. From this, I gather that spring's AOP is not as transparent as it should be (assuming AOP was supposed to be transparent to start) or is this a side effect of the interface enforced development style? Conscious decision not to support this? Something else perhaps?

    Code:
    package org.sample.spring;
    
    public class SimpleBean
    {
    	AnotherSimpleBean asb;
    	
    	public void setAsb(AnotherSimpleBean asb)
    	{
    		this.asb = asb;
    	}
    	
    	public String getValue()
    	{
    		return this.asb.getComputedValue();
    	}
    }
    Code:
    package org.sample.spring;
    
    public class AnotherSimpleBean
    {
    	private String value;
    
    	public void setValue(String value)
    	{
    		this.value = value;
    	}
    
    	protected String getComputedValue()
    	{
    		return this.value + this.value;
    	}
    }
    Code:
    <?xml version="1.0"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http&#58;//www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
    	<bean id="simplebeanTarget" class="org.sample.spring.SimpleBean" singleton="false">
    		<property name="asb">
    			<ref bean="anothersimplebean"/>
    		</property>
    	</bean>
    	<bean id="anothersimplebeanTarget" class="org.sample.spring.AnotherSimpleBean" singleton="false">
    		<property name="value">
    			<value>foobar</value>
    		</property>
    	</bean>
    
    	<bean id="debugInterceptor" class="org.springframework.aop.interceptor.DebugInterceptor"/>
    
    	<bean id="simplebean" class="org.springframework.aop.framework.ProxyFactoryBean">
    		<property name="target"><ref bean="simplebeanTarget"/></property>
    		<property name="proxyTargetClass"><value>true</value></property>
    		<property name="interceptorNames">
    			<list>
    				<value>debugInterceptor</value>
    			</list>
    		</property>
    	</bean>
    	<bean id="anothersimplebean" class="org.springframework.aop.framework.ProxyFactoryBean">
    		<property name="target"><ref bean="anothersimplebeanTarget"/></property>
    		<property name="proxyTargetClass"><value>true</value></property>
    		<property name="interceptorNames">
    			<list>
    				<value>debugInterceptor</value>
    			</list>
    		</property>
    	</bean>
    
    </beans>

  • #2
    I'm only guessing : maybe cglib can't proxy protected methods, because they are protected?

    Try this

    Code:
      public String getComputedValue&#40;&#41; &#123;
        return getComputedValue2&#40;&#41;;
      &#125;
    
      protected String getComputedValue2&#40;&#41; &#123;
        return value + value;
      &#125;

    Comment


    • #3
      That's my guess as well but that means Spring AOP is not as transparent. Protected methods are still accessible to classes within the same package. The solution you presented was considered when we first ran into the problem, but it was deemed too much of a work-around for something that should've worked (naturally our object model is more cpmplicated than the sample presented).

      My feeling is that Spring AOP drives to integrate at component level (e.g. public classes/interfaces that serve as a facade to a package) instead of implementation level (e.g. objects relationships within a package). I believe Mr. Johnson states this in his reference doc.

      Any comments on this from the Spring developers?

      Comment

      Working...
      X