Four implementation methods of spring AOP and four implementation methods of spring AOP
1. Classic proxy-based AOP
1. Create a notification: Define an Interface
Public interface Sleepable {void sleep ();} and then write a Human class, which implements this interface public Human implements Sleepable {public void sleep () {System. out. println ("sleeping ...! ");}}
2. Compile a SleepHelper class that includes the sleeping auxiliary work. In AOP terminology, it should be a notification.
Public class SleepHelper implements MethodBeforeAdvice, AfterReturningAdvice {public void before (Method method, Object [] arguments, Object target) throws Throwable {System. out. println ("before going to bed");} public void afterReturning (Object rturnValue, Method method, Object [] arguments, Object target) throws Throwable {System. out. println ("sleeping ");}}
Then configure in the spring configuration file:
<! -- Target object to be proxy -->
<Bean id = "Human" class = "cn. happy. dao. human "> </bean> <bean id =" SleepHelper "class =" cn. happy. aop. sleepHelper "> </bean> // two common methods for defining cut points: 1) use regular expressions 2) use AspectJ expressions, // use regular expressions here <! -- Consultant --> <bean id = "BeforAdvisor" class = "org. springframework. aop. support. regexpMethodPointcutAdvisor "> <property name =" advice "ref =" BeforAdvice "> </property> <property name =" pattern "value = ". * l. * g. * "> </property> </bean> <! -- Proxy object --> // ProxyFactoryBean is a proxy, we can convert it to the proxy object specified in // proxyInterfaces to implement this interface <bean id = "serviceProxy" class = "org. springframework. aop. framework. proxyFactoryBean "> <property name =" target "ref =" Human "> </property> <property name =" interceptorNames "value =" BeforAdvisor "> </property> </bean>
Proxy
Public class StuTest {public static void main (String [] args) {// instantiate Spring context ApplicationContext = new ClassPathXmlApplicationContext ("applicationContext. xml "); // use the getBean () method of ApplicationContext to obtain the Bean instance Sleepable s = (Sleepable) context according to the id. getBean ("Human"); s. sleep ();}}
3. Use the AspectJ Annotation
Use the annotation of @ Aspect to identify the Aspect
@ Aspect // This class is a public class MyAspect {@ Before (value = "execution (public **(..)) ") public void mybefore () {System. out. println ("pre-enhancement");} // post-enhancement @ AfterReturning (value = "execution (public **(..)) ") public void myafterReturning () {System. out. println ("post enhancement");} // exception enhancement @ AfterThrowing (value = "execution (public **(..)) ") public void myafterThrowing () {System. out. println ("exception enhancement");} // surround enhancement @ Around (value = "execution (public **(..)) ") public void myAround (ProceedingJoinPoint jp) {System. out. println ("enhancement before surround"); try {jp. proceed ();} catch (Throwable e) {// TODO Auto-generated catch block e. printStackTrace ();} System. out. println ("enhancement After surround");} // final enhancement @ After (value = "execution (public **(..)) ") public void myafterLogger () {System. out. println ("final enhancement ");}}
Spring configuration file:
<! -- Target object --> <bean id = "Human" class = "cn. happy. dao. IserviceImpl"> </bean> <! -- Aspect: --> <bean id = "myAspect" class = "cn. happy. aop. MyAspect"> </bean> <aop: aspectj-autoproxy/>
Test class
public class StuTest { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Sleepable s=(Sleepable)context.getBean("Service"); s.sleep(); }}
4. Use <aop: config> to configure (POJO-only)
<Aop: advisor> defines an AOP notifier
<Aop: after> post notification
<Aop: after-returning> post-return notification
<Aop: after-throwing> post-throw notification
<Aop: around> notifications
<Aop: aspect> define a plane
<Aop: before> pre-notification
<Aop: config> top-level Configuration elements, similar to <beans>
<Aop: pointcut> define a cut point
Public class MyAspect {public void mybefore () {System. out. println ("pre-enhancement");} public String myafterReturning (String Returning) {System. out. println ("pre-enhancement"); return Returning ;}}
Public class IserviceImpl implements Iservice {public void log () {System. out. println ("enable transaction");} public String dofirst () {System. out. println ("logging"); return "";}}
Spring configuration file:
<! -- Target object --> <bean id = "Service" class = "cn. happy. dao. IserviceImpl"> </bean> <! -- Aspect: --> <bean id = "myAspect" class = "cn. happy. aop. MyAspect"> </bean> <! -- Configuration section --> <aop: config> <! -- Configure the entry point --> <aop: pointcut id = "pointcut" expression = "execution (public ** .. Iservice. log (..)"/> <! -- Define the class method as the final enhancement and reference the pointcut entry point --> <aop: aspect ref = "myAspect"> <aop: after method = "myafterReturning" pointcut-ref = "pointcut"/> </aop: aspect> </aop: config>
Test class
public class SpringTest { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); Iservice bean = (Iservice)context.getBean("Service"); bean.log(); String count=bean.dofirst(); System.out.println(count); }}