I am trying to execute a aspect on proxy object
package thispkg;
public class MyLogger {
public void before() {
System.out.println("=========Before========");
}
public void after() {
System.out.println("=========After=========");
}
public void info() {
System.out.println("=========Info=========");
}
}
package thispkg;
public interface MyInterface {
public void speak();
}
package thispkg;
public class MyInterfaceImpl implements MyInterface {
@Override
public void speak() {
System.out.println("MyInterfaceImpl :: Hello world");
}
}
package thispkg;
public class RandomClass {
public void suvichar() {
System.out.println("RandomClass (suvichar)::Karm kiye jaa, fal ki chinta mat kar");
}
}
package thispkg;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("thispkg.xml");
MyInterface in = (MyInterface) context.getBean("randomClass", RandomClass.class);
in.speak();
}
}
My XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:spring-configured/>
<bean id="mylogger" class="thispkg.MyLogger"/>
<bean id="randomClass" class="thispkg.RandomClass"/>
<bean id="myInterfaceImpl" class="thispkg.MyInterfaceImpl"/>
<aop:config proxy-target-class="true">
<aop:aspect id="usageTrackerAspect" ref="mylogger">
<aop:declare-parents types-matching="thispkg.RandomClass+" implement-interface="thispkg.MyInterface" default-impl="thispkg.MyInterfaceImpl"/>
<aop:pointcut expression="this(thispkg.MyInterface)" id="randompointcut"/>
<aop:before pointcut-ref="randompointcut" method="info"/>
</aop:aspect>
</aop:config>
</beans>
I have tried both thispkg.MyInterfaceImpl
& thispkg.RandomClass
in pointcut expression but still can't get the ========Info========
printed. Only prints
MyInterfaceImpl :: Hello world
Any clue ?
So after some tries and then more reading and then more retries, i found that i can get the desired behaviour when using delegate-ref instead of default-impl, thus allowing spring to manage my implementation. Also correcting my pointcut expression.
<aop:aspect ref="mylogger">
<aop:declare-parents types-matching="thispkg.RandomClass+" implement-interface="thispkg.MyInterface" delegate-ref="myInterfaceImpl"/>
<aop:pointcut expression="execution(* thispkg.MyInterface.speak())" id="randompointcut"/>
<aop:before method="info" pointcut-ref="randompointcut"/>
</aop:aspect>
Output
=========Info=========
MyInterfaceImpl :: Hello world
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments