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