AspectJ's implementation of AOP and AspectJAOP
I. knowledge you should understand
1. Many frameworks have implemented the idea of AOP. Spring is one of them and can complete cross-section programming. However, AspectJ also implements the function of AOP, Which is simpler and easier to use, and supports annotation-based development. Therefore, Spring has introduced AspectJ's implementation of AOP to its own framework. When using AOP for Spring development, AspectJ is generally used.
2. Spring's classic AOP configuration Scheme
01. The third-party framework of Aspectj is used to implement the idea of AOP.
02. AOP for annotation Configuration
03. Pure POJO <aop: config>
3. Entry Point expression
Execution ([modifiers-pattern ?] Access Modifier
Ret-type-pattern Return Value type
[Declaring-type-pattern ?] Fully Qualified Class Name
Name-pattern (param-pattern) method name (parameter name)
[Throws-pattern ?]) Throw exception type
The object to be matched by the entry point expression is the method name of the target method. Therefore, the execution expression is obviously the signature of the method. Note: The [] Part in the expression indicates that the part can be omitted, and the parts are separated by spaces. The following symbols can be used:
Symbolic Meaning
* 0 up to any character
.. Used in method parameters to indicate any number of parameters
After the package name is used, it indicates the current package and its sub-package path
+ Used after the class name to indicate the current class and its subclass
Indicates the current interface and its implementation class.
Case:
Execution (public ** (...) specifies the entry point to be any public method.
Execution (* set * (...) specifies the start point to any method starting with "set ".
2. Use the AOP enhancement configured by the third-party framework annotation of Aspectj
Source code introduction:
1. ISomeService. java
Package entity; // business interface public interface ISomeService {// 1.1 execute the transaction public void doTransaction (); // 1.2 book write log public String doLog ();}View Code
2. SomeServiceImpl. java
Package entity; // interface implementation class public class SomeServiceImpl implements ISomeService {// method in the implementation interface @ Override public void doTransaction () {System. out. println ("---- enable transaction ----") ;}@ Override public String doLog () {// int I = 5/0; // creates an error for testing exception enhancement System. out. println ("--- book log -----"); return "abc ";}}View Code
3. MyAspect. java (use the AOP enhancement configured by the third-party framework annotation of Aspectj)
Package aop; // aspectj annotation to implement aop enhanced import org. aspectj. lang. proceedingJoinPoint; import org. aspectj. lang. annotation. after; import org. aspectj. lang. annotation. afterReturning; import org. aspectj. lang. annotation. afterThrowing; import org. aspectj. lang. annotation. around; import org. aspectj. lang. annotation. aspect; import org. aspectj. lang. annotation. before; // This class is the section @ Aspectpublic class MyAspect {// pre-notification @ Before (value = "execution (public **.. ISomeService. doLog (..)) ") public void myBefore () {System. out. println ("This is pre-enhancement");} // post notification @ AfterReturning (value = "execution (public **.. ISomeService. doLog (..)) ") public void myAfterReturning () {System. out. println ("this is post enhancement");} // surround enhancement @ Around (value = "execution (public **.. ISomeService. doLog (..)) ") public void myAround (ProceedingJoinPoint pjp) throws Throwable {System. out. println ("this is the surround enhancement"); pjp. proceed (); System. out. println ("this is surround post enhancement");} // exception enhancement @ AfterThrowing (value = "execution (public **.. ISomeService. doLog (..)) ") public void myAfterThrowing () {System. out. println ("this is an exception enhancement");} // final enhancement @ After (value = "execution (public **.. ISomeService. doLog (..)) ") public void myAfter () {System. out. println ("this is the final enhancement ");}}View Code
4. applicationContext. xml (Spring configuration file)
<? 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.xsd http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <! -- Target object --> <bean id = "someService" class = "entity. SomeServiceImpl"> </bean> <! -- Aspect: --> <bean id = "myAspect" class = "aop. MyAspect"> </bean> <! -- Automatic proxy --> <aop: aspectj-autoproxy/> </beans>View Code
5. MyTest. java
Package test; // test class import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; import entity. ISomeService; public class MyTest {public static void main (String [] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext. xml "); ISomeService biz = (ISomeService) ctx. getBean ("someService"); biz. doLog (); biz. do Transaction (); System. out. println ("success! ");}}View Code
6. log4j. properties (log configuration file)
### Direct log messages to stdout ### log4j. appender. stdout = org. apache. log4j. leleappenderlog4j. appender. stdout. target = System. outlog4j. appender. stdout. layout = org. apache. log4j. patternLayoutlog4j. appender. stdout. layout. conversionPattern = % d {ABSOLUTE} % 5 p % c {1}: % L-% m % n ### direct messages to file mylog. log ### log4j. appender. file = org. apache. log4j. fileAppenderlog4j. appender. file. file = c \: mylog. loglog4j. appender. file. layout = org. apache. log4j. patternLayoutlog4j. appender. file. layout. conversionPattern = % d {ABSOLUTE} % 5 p % c {1 }: % L-% m % n ### set log levels-for more verbose logging change 'info' to 'debug' ### log4j. rootLogger = info, stdoutView Code
7. Of course, do not forget to introduce the jar package!
8. The test result is displayed as follows:
Iii. Use a third-party Aspectj framework pure POJO (<aop: config>)
Source code introduction:
1. MyAspect. java
Package aop; import org. aspectj. lang. joinPoint; import org. aspectj. lang. proceedingJoinPoint; public class MyAspect {// pre-notification public void myBefore () {System. out. println ("this is a pre-enhancement");} public void before (JoinPoint jp) {System. out. println ("pre-notification method before () jp =" + jp);} // post-Notification public void myAfterReturning () {System. out. println ("this is post enhancement");} // public void afterReturing (String result) {System. out. println ("post notification method result =" + result);} // surround notification public Object around (ProceedingJoinPoint pjp) throws Throwable {System. out. println ("surround notification method, before the target method is executed"); // execute the target method Object result = pjp. proceed (); System. out. println ("surround notification method, after the target method is executed"); return (String) result ). toUpperCase ();} // public void afterThrowing () {System. out. println ("Exception notification method");} public void afterThrowing (Exception ex) {System. out. println ("exception notification method ex =" + ex. getMessage ();} // The public void after () {System. out. println ("final notification method ");}}View Code
2. applicationContext. xml (Spring configuration file)
<? 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.xsd http://www.springframework.org/schema/context Http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <! -- Target object --> <bean id = "someService" class = "entity. SomeServiceImpl"> </bean> <! -- Aspect: --> <bean id = "myAspect" class = "aop. MyAspect"> </bean> <aop: config> <! -- Expression: pointcut expression --> <aop: pointcut expression = "execution (public **.. ISomeService. doLog (..)) "id =" beforePointcut "/> <aop: aspect ref =" myAspect "> <! -- Ref: Specify the cut section --> <! -- Method: Specifies the method in the partition class; pointcut-ref: Specifies the defined cut point --> <! -- Pre-enhancement --> <aop: before method = "myBefore" pointcut-ref = "beforePointcut"/> <! -- Pre-enhancement with parameters --> <aop: before method = "before (org. aspectj. lang. JoinPoint)" pointcut-ref = "beforePointcut"/> <! -- Post enhancement --> <aop: after-returning method = "myAfterReturning" pointcut-ref = "beforePointcut"/> <! -- Post-enhancement with parameters --> <aop: after-returning method = "afterReturing (java. lang. string) "pointcut-ref =" beforePointcut "returning =" result "/> <! -- Surround enhancement --> <aop: Und und method = "around" pointcut-ref = "beforePointcut"/> <! -- Exception enhancement --> <aop: after-throwing method = "afterThrowing" pointcut-ref = "beforePointcut"/> <! -- Exception enhancement with parameter --> <aop: after-throwing method = "afterThrowing (java. lang. exception) "pointcut-ref =" beforePointcut "throwing =" ex "/> <! -- Final enhancement --> <aop: after method = "after" pointcut-ref = "beforePointcut"/> </aop: aspect> </aop: config> </beans>View Code
Among them, other classes and files are exactly the same as in the above example. We will not introduce it here.
3. The test result is displayed as follows: