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:
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");
First you need to define the bean
Test
in your configuration file:Then in main, get this bean from
ApplicationContext
:Now, from
test1
reference, you can only invoke method defined inTest
bean. To use the newly introduced behaviour, you need to typecast the reference to the interface containing that behaviour:Then, you can access the method of
Test2
from this reference:This will invoke the method defined in the bean as specified in the
defaultImpl
attribute of@DeclareParents
annotation.