Announcement Announcement Module
Collapse
No announcement yet.
Instantiate abstract class bean with abstract methods intercepted by AOP or otherwise Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Instantiate abstract class bean with abstract methods intercepted by AOP or otherwise

    Hi.

    What I want to do is following. I write an abstract class like this:

    Code:
    public abstract class MyService {
    
        public abstract String getSomeValue();
    
        public String getAnotherValue() { return "test" };
    
    }
    What I aim for is that this class gets enhanced by AOP or CGLIB directly, so that calling abstract methods is intercepted and replaced by some logic. I don't want to use interfaces, because I still want non-abstract methods there.

    At what level and how should I implement this? Enhancing some class with CGLIB or advising with AOP is not a problem, but I can't think of a way to properly put it all together. If it is possible to be done with AOP, then how? If I should use CGLIB, then at what point in beans lifecycle can I enhance my bean?

  • #2
    Here's what you need.

    applicationContext.xml
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    
    	<bean id="myBean" name="myBean" class="MyService">
    		<replaced-method name="getSomeValue" replacer="myBeanHelper">
    			<arg-type>String</arg-type>
    		</replaced-method>
    	</bean>
    
    	<bean id="myBeanHelper" name="myBeanHelper" class="MyBeanHelper">
    	</bean>
    </beans>
    MyService.java
    Code:
    public abstract class MyService {
    
    	public MyService(){};
    	
        public abstract String getSomeValue();
    
        public String getAnotherValue() { return "test"; };
    
    }
    MyBeanHealper.java
    Code:
    public class MyBeanHelper implements MethodReplacer{
    
    	public Object reimplement(Object obj, java.lang.reflect.Method method,
    			Object[] args) throws Throwable {
            return "some value";
    	}
    }

    Comment

    Working...
    X