Spring aop-several different ways of use, springaop --
I recently heard from my teacher about the AOP class on containers. I suddenly felt that I didn't have a good understanding of aop. I didn't mean that I had very few applications, I have also written several blogs about AOP recently, all of which are simple demos. I will talk about Several implementation methods of Spring AOP here today.
We often use the following types
1. Proxy-based AOP
2. Simple java object aspect
3. @ Aspect annotation form
4. Injection form Aspcet aspect
Next we will apply them one by one.
Below we will first write several basic classes.
Interface Class:
/*** Define an interface * @ author Chen Lina * @ version 9:16:50 AM on April 9 */public interface Sleepable {/*** sleeping Method * @ author Chen Lina * @ version 9:17:14 AM on April 9 * /void sleep ();}
Implementation class:
/*** Chen Lina implements the sleeping interface * @ author Chen Lina * @ version 4:51:43 on January 1, May 31, 2015 */public class ChenLliNa implements Sleepable {@ Overridepublic void sleep () {// TODO Auto-generated method stub System. out. println ("hey, it's time to go to bed! ");}}
Enhancement class:
/*** Define a sleep enhancement and implement both the frontend and the backend * @ author Chen Lina * @ version 9:24:43 AM, January 1, May 31, 2015 */public class SleepHelper implements MethodBeforeAdvice, afterReturningAdvice {@ Overridepublic void afterReturning (Object returnValue, Method method, Object [] args, Object target) throws Throwable {System. out. println ("apply mask before going to bed");} @ Overridepublic void before (Method method, Object [] args, Object target) throws Throwable {System. out. println ("dream after going to bed ");}}
I. Proxy-based AOP
<! -- Create an enhanced advice --> <bean id = "sleepHelper" class = "com. tgb. springaop. aspect. sleepHelper "/> <bean id =" lina "class =" com. tgb. springaop. service. impl. chenLliNa "/> <! -- Define the method of matching all sleep by the cut point --> <bean id = "sleepPointcut" class = "org. springframework. aop. support. jdkRegexpMethodPointcut "> <property name =" pattern "value = ". * sleep "> </property> </bean> <! -- Cut plane enhancement + cut point combination --> <bean id = "sleepHelperAdvisor" class = "org. springframework. aop. support. defaultPointcutAdvisor "> <property name =" advice "ref =" sleepHelper "/> <property name =" pointcut "ref =" sleepPointcut "/> </bean> <! -- Define proxy object --> <bean id = "linaProxy" class = "org. springframework. aop. framework. proxyFactoryBean "> <property name =" target "ref =" lina "/> <property name =" interceptorNames "value =" sleepHelperAdvisor "/> <! -- <Property name = "proxyInterfaces" value = "com. tgb. springaop. service. Sleepable"/> --> </bean>
As shown in the configuration file:
The pattern attribute specifies a regular expression that matches all sleep methods.
The purpose of using org. springframework. aop. support. DefaultPointcutAdvisor is to combine the cut points and enhancements to form a complete cut surface.
After the configuration is complete, a final proxy object is generated through org. springframework. aop. framework. ProxyFactoryBean.
Ii. Simple java object aspect
In my opinion, compared with the first configuration, java does not need to use a proxy, but uses the internal mechanism of spring to automatically scan, in this case, we should modify the configuration file as follows:
<! -- Create an enhanced advice --> <bean id = "sleepHelper" class = "com. tgb. springaop. aspect. SleepHelper"/> <! -- Target class --> <bean id = "lina" class = "com. tgb. springaop. service. impl. ChenLliNa"/> <! -- Configure cut points and notifications --> <bean id = "sleepAdvisor" class = "org. springframework. aop. support. regexpMethodPointcutAdvisor "> <property name =" advice "ref =" sleepHelper "> </property> <property name =" pattern "value = ". * sleep "/> </bean> <! -- Automatic proxy configuration --> <bean class = "org. springframework. aop. framework. autoproxy. DefaultAdvisorAutoProxyCreator"/>
Is it much simpler than the first one, so you don't have to configure the proxy any more.
Iii. @ Aspect annotation form
According to our experience, the form of annotation is simpler than that of the configuration file. In this case, you need to annotate the existing methods or classes:
/*** Add enhancement via annotation * @ author Chen Lina * @ version 10:26:13, December 11, May 31, 2015 */@ Aspect @ Componentpublic class SleepHelper03 {/* @ Pointcut ("execution (* com. tgb. springaop. service. impl .. *(..)) ") */@ Pointcut (" execution (**. sleep (..)) ") public void sleeppoint () {}@ Before (" sleeppoint () ") public void beforeSleep () {System. out. println ("apply mask before going to bed");} @ AfterReturning ("sleeppoint ()") public void afterSleep () {System. out. println ("dream after going to bed ");}
In the configuration file, you only need to write:
<! -- Scan package --> <context: component-scan base-package = "com. tgb" annotation-config = "true"/> <! -- ASPECTJ annotation --> <aop: aspectj-autoproxy proxy-target-class = "true"/> <! -- Target class --> <bean id = "lina" class = "com. tgb. springaop. service. impl. ChenLliNa"/>
Iv. Injection form Aspcet aspect
Personally, this is the simplest, most commonly used, and most flexible. The configuration file is as follows:
<! -- Target class --> <bean id = "lina" class = "com. tgb. springaop. service. impl. chenLliNa "/> <bean id =" sleepHelper "class =" com. tgb. springaop. aspect. sleepHelper02 "/> <aop: config> <aop: aspect ref =" sleepHelper "> <aop: before method =" beforeSleep "pointcut =" execution (**. sleep (..)) "/> <aop: after method =" afterSleep "pointcut =" execution (**. sleep (..)) "/> </aop: aspect> </aop: config>
The SleepHelper02 class mentioned in the configuration file is as follows:
/*** Add enhancement via annotation * @ author Chen Lina * @ version May 31, 2015 10:26:13 */public class SleepHelper02 {public void beforeSleep () {System. out. println ("apply mask before going to bed");} public void afterSleep () {System. out. println ("dream after going to bed ");}}
Is it easy to look at? Is it true that everyone will use spring aop ?!
There are several test classes for how to call them. Let's take a look at them. They are basically the same:
/*** Configuration file spring_aop.xml via proxy * @ author Chen Lina * @ version May 31, 2015 10:09:10 */@ Testpublic void test () {ApplicationContext ct = new ClassPathXmlApplicationContext ("spring_aop.xml "); sleepable sleeper = (Sleepable) ct. getBean ("linaProxy"); sleeper. sleep ();}/*** configuration file spring_aop_01.xml short answer java object * @ author Chen Lina * @ version May 31, 2015 10:09:37 */@ Testpublic void test01 () {ApplicationContext ct = new ClassPathXmlApplicationContext ("spring_aop_01.xml"); Sleepable sleeper = (Sleepable) ct. getBean ("lina"); sleeper. sleep ();}/*** configuration file spring_aop_03.xml via aspect annotation * @ author Chen Lina * @ version May 31, 2015 10:09:37 */@ Testpublic void test03 () {ApplicationContext ct = new ClassPathXmlApplicationContext ("spring_aop_03.xml"); Sleepable sleeper = (Sleepable) ct. getBean ("lina"); sleeper. sleep ();}/*** configuration file spring_aop_02.xml via apsect configuration file * @ author Chen Lina * @ version May 31, 2015 10:09:37 */@ Testpublic void test02 () {ApplicationContext ct = new ClassPathXmlApplicationContext ("spring_aop_02.xml"); Sleepable sleeper = (Sleepable) ct. getBean ("lina"); sleeper. sleep ();}
From the test class, we can see that no matter what method is used to implement aop, there is no difference. The results of these test classes are the same: