I am learning Spring AOP and i have created a simple project in order to understand how it works. Please find below the main part of my project:
My Spring-Customer.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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 ">
<aop:aspectj-autoproxy />
<bean id="customerBoNoIterface" class="com.core.CustomerBoNoIterface" />
<!-- Aspect -->
<bean id="logAspect" class="com.aspect.LoggingAspect" />
</beans>
Please find below my AOP pointcut:
@Before("execution(* com.core.CustomerBoNoIterface.addCustmer*(..))")
public void logBeforeDummy(JoinPoint joinPoint) {
System.out.println("hijacked : " + joinPoint.getSignature().getName());
System.out.println("******");
}
Please find below my class CustomerBoNoIterface:
public class CustomerBoNoIterface {
private void addCustmerPrivate() {
System.out.println("calling addCustmerPrivate()");
}
public void addCustmerPublic() {
System.out.println("calling addCustmerPublic()");
}
protected void addCustmerProtected() {
System.out.println("calling addCustmerProtected()");
}
void addCustmerDefault() {
System.out.println("calling addCustmerDefault()");
}
public String addCustmerReturnString() {
System.out.println("calling addCustmerPublic return string");
return "";
}
}
And finally my Main method:
public class App {
public static void main(String[] args) throws Exception {
ApplicationContext appContext = new ClassPathXmlApplicationContext("Spring-Customer.xml");
CustomerBoNoIterface customerBoNoIterfaceBean =appContext.getBean("customerBoNoIterface", CustomerBoNoIterface.class);
customerBoNoIterfaceBean.addCustmerPublic();
customerBoNoIterfaceBean.addCustmerDefault();
customerBoNoIterfaceBean.addCustmerProtected();
customerBoNoIterfaceBean.addCustmerReturnString();
}
According to the on line reference Spring AOP is done by proxy and a class needs to implement an interface in order for Spring to be able to weave the aspect and also Spring will weave aspect to public method only.
However in my case i have a class with no interface as shown in the above example and i am able to apply AOP in following method:
customerBoNoIterfaceBean.addCustmerPublic();
customerBoNoIterfaceBean.addCustmerDefault();
customerBoNoIterfaceBean.addCustmerProtected();
customerBoNoIterfaceBean.addCustmerReturnString();
So when i run the Main method i get the following output in my console in eclipse:
hijacked : addCustmerPublic
******
calling addCustmerPublic()
hijacked : addCustmerDefault
******
calling addCustmerDefault()
hijacked : addCustmerProtected
******
calling addCustmerProtected()
hijacked : addCustmerReturnString
******
calling addCustmerPublic return string
So I am not able to understand how Spring is able to weave aspect in methods other than public and also a class with no interface?
Thanks in advance,