in XML
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xmlns:context= "Http://www.springframework.org/schema/context"
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/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<bean id= "HelloWorldService" class= "Cn.javass.spring.chapter6.service.impl.HelloWorldService"/>
<bean id= "aspect" class= "Cn.javass.spring.chapter6.aop.HelloWorldAspect"/>
<bean id= "Beforeadvice" class= "Cn.javass.spring.chapter6.aop.BeforeAdviceImpl"/>
<aop:config>
<aop:advisor pointcut= "Execution (* cn.javass. *.sayadvisorbefore (..)) "
advice-ref= "Beforeadvice"/>
<aop:aspect ref= "Aspect" >
<aop:before pointcut= "Execution (* cn.javass. *.saybefore (..)) and args (param) "
Method= "Beforeadvice (java.lang.String)"
arg-names= "param"/>
<aop:after-returning pointcut= "Execution (* cn.javass. *.sayafterreturning (..)) "
Method= "Afterreturningadvice"
Arg-names= "RetVal"
returning= "RetVal"/>
<aop:after-throwing pointcut= "Execution (* cn.javass. *.sayafterthrowing (..)) "
Method= "Afterthrowingadvice"
Arg-names= "Exception"
throwing= "Exception"/>
<aop:after pointcut= "Execution (* cn.javass. *.sayafterfinally (..)) "
method= "Afterfinallyadvice"/>
<aop:around pointcut= "Execution (* cn.javass. *.sayaround (..)) "
method= "Aroundadvice"/>
<aop:declare-parents types-matching= "Cn.javass. *. Ihelloworldservice+ "
Implement-interface= "Cn.javass.spring.chapter6.service.IIntroductionService"
Default-impl= "Cn.javass.spring.chapter6.service.impl.IntroductiondService"/>
</aop:aspect>
</aop:config>
</beans>
Example 1 in Java
@Test
public void Testschemabeforeadvice () {
System.out.println ("======================================");
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Chapter6/advice.xml");
Ihelloworldservice HelloWorldService = Ctx.getbean ("HelloWorldService", Ihelloworldservice.class);
Helloworldservice.saybefore ("before");
System.out.println ("======================================");
}
Example 2
@Test
public void Testschemaadvisor () {
System.out.println ("======================================");
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Chapter6/advice.xml");
Ihelloworldservice HelloWorldService = Ctx.getbean ("HelloWorldService", Ihelloworldservice.class);
Helloworldservice.sayadvisorbefore ("haha");
System.out.println ("======================================");
}
Here's a description.
So "<aop:aspect>" actually defines the crosscutting logic, which is what is done on the connection point, and "<aop:advisor>" defines what the connection points apply <aop:aspect>. The benefit of spring is that multiple cross-cutting logic (that is, <aop:aspect> defined) can be used multiple times to provide reusability.
What is the difference between <aop:advisor> and <aop:aspect> in spring?