SpringAOP introductions with AspectJ

混江龙づ霸主 提交于 2019-12-22 09:16:02

问题


I'm new in SpringAOP. I want to write simple example of Introductions but can't understand clearly how it must work.

In documentation I find that:

Introduction: declaring additional methods or fields on behalf of a type. Spring AOP allows you to introduce new interfaces (and a corresponding implementation) to any advised object. For example, you could use an introduction to make a bean implement an IsModified interface, to simplify caching. (An introduction is known as an inter-type declaration in the AspectJ community.)

And I write simple example: I write simple class with one method

public class Test {
    public void test1(){
        System.out.println("Test1");
    }
}

Then I write interface and class that implements this interface

public interface ITest2 {
    void test2();
}

public class Test2Impl implements ITest2{
    @Override
    public void test2() {
        System.out.println("Test2");
    }
}

and finally my aspect

@Aspect
public class AspectClass {

    @DeclareParents(
            value = "by.bulgak.test.Test+",
            defaultImpl = Test2Impl.class
    )
    public static ITest2 test2;
}

my spring configuration file look like this:

<aop:aspectj-autoproxy/>
<bean id="aspect" class="by.bulgak.aspect.AspectClass" />

So my question: How can I you this now. what I need to write in my main class to sea result? May be I need to write some other classes.(book in which I read about SpringAOP I can't find full examples)

UPDATE

My main method looks like this:

public static void main(String[] args) {
    ApplicationContext appContext = new ClassPathXmlApplicationContext("spring-configuration.xml");
    Test test = (Test) appContext.getBean("test");
    test.test1();
    ITest2 test2 = (ITest2) appContext.getBean("test");
    test2.test2();

}

When I execute my app i get this error:

Exception in thread "main" java.lang.ClassCastException: com.sun.proxy.$Proxy5 cannot be cast to by.bulgak.test.Test

at this line:

Test test = (Test) appContext.getBean("test");

回答1:


First you need to define the bean Test in your configuration file:

<bean id="test" class="Test" />

Then in main, get this bean from ApplicationContext:

Test test1 = (Test) context.getBean("test");

Now, from test1 reference, you can only invoke method defined in Test bean. To use the newly introduced behaviour, you need to typecast the reference to the interface containing that behaviour:

ITest2 test2 = (ITest2) context.getBean("test");

Then, you can access the method of Test2 from this reference:

test2.test2();

This will invoke the method defined in the bean as specified in the defaultImpl attribute of @DeclareParents annotation.



来源:https://stackoverflow.com/questions/17176169/springaop-introductions-with-aspectj

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!