Results 1 to 2 of 2

Thread: Instantiate abstract class bean with abstract methods intercepted by AOP or otherwise

  1. #1
    Join Date
    Sep 2010
    Location
    Estonia
    Posts
    1

    Default 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. #2

    Default

    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";
    	}
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •