Original: HTTPS://GITHUB.COM/X113773/TESTALL/ISSUES/12
1. Add dependencies First (using the default version of the current springboot)
```
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
```
-refer to the following [official documents] (http://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/htmlsingle/) section of the configuration instructions, visible AOP is enabled by default, @enableaspectjautoproxy annotations are added automatically
```
# AOP
Spring.aop.auto=true # ADD @EnableAspectJAutoProxy.
Spring.aop.proxy-target-class=false # Whether subclass-based (CGLIB) proxies is to being created (true) as opposed to Standa Rd Java interface-based proxies (false).
```
2. Write a slice class, [Aspectadviceconfig.java] (https://github.com/x113773/testall/blob/master/src/main/java/com/ansel/ Testall/aop/aspectadviceconfig.java), which defines a pointcut indicator and various notifications (Advice, also translation enhancements)
```
Package COM.ANSEL.TESTALL.AOP;
Import Java.lang.reflect.Method;
Import Java.util.Arrays;
Import Javax.servlet.http.HttpServletRequest;
Import Org.aspectj.lang.JoinPoint;
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;
Import org.aspectj.lang.annotation.DeclareParents;
Import Org.aspectj.lang.annotation.Pointcut;
Import Org.aspectj.lang.reflect.MethodSignature;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import org.springframework.stereotype.Component;
Import Org.springframework.web.context.request.RequestContextHolder;
Import org.springframework.web.context.request.ServletRequestAttributes;
@Aspect
@Component
public class Aspectadviceconfig {
Private Logger Logger = Loggerfactory.getlogger (This.getclass ());
/**
* Define the Pointcut indicator: a class with @restcontroller annotations under the COM.ANSEL.TESTALL.AOP package.
*/
@Pointcut ("Execution (* COM.ANSEL.TESTALL.AOP). *(..)) and @annotation (Org.springframework.web.bind.annotation.RestController) ")
public void Mypointcut () {
}
/**
* Pre-notification, call notification when the target method is complete, and do not care what the output of the method is
*/
@Before ("Mypointcut ()")
public void Beforeadvice () {
SYSTEM.OUT.PRINTLN ("before--notification method will execute before the target method call");
}
/**
* Post notification, call notification when the target method is complete, and do not care what the output of the method is at this time
*/
@After ("Mypointcut ()")
public void Afteradvice () {
SYSTEM.OUT.PRINTLN ("after--notification method will be called after the target method returns or throws an exception");
}
/**
* Return notification, called after successful execution of the target method, can get the return value of the target method, but cannot be modified (nor affect the return value of the method)
*
* @param JP
* Joinpoint interface, can get some information of connection point
*
* @param retVal
* Target method return value, like JP will be automatically passed in by spring
*/
@AfterReturning (returning = "RetVal", Pointcut = "mypointcut ()")
public void Afterreturningadvice (Joinpoint JP, Object retVal) {
RetVal = RetVal + "(@AfterReturning can read the return value, but it can ' t change the value!)";
SYSTEM.OUT.PRINTLN (The afterreturning--notification method is called after the target method returns; RetVal = "+ RetVal);
System.out.println (Jp.tolongstring ());
}
/**
* Exception notification, call notification after the target method throws an exception
*/
@AfterThrowing ("Mypointcut ()")
public void Afterthrowingadvice () {
SYSTEM.OUT.PRINTLN ("afterthrowing--notification method will be called after the target method throws an exception");
}
/**
* Surround notification, you can customize the execution of content before and after the target method call. You can modify the return value of the target method
*
* @param PJP
*/
@Around ("Mypointcut ()")
Public Object Aroundadvice (Proceedingjoinpoint pjp) {
Object retVal = null;
try {
System.out.println ("Execute before around--target method call");
Servletrequestattributes attributes = (servletrequestattributes) requestcontextholder.getrequestattributes ();
HttpServletRequest request = Attributes.getrequest ();
Methodsignature signature = (methodsignature) pjp.getsignature ();
Method method = Signature.getmethod (); Get the Intercepted method
String methodName = Method.getname (); Gets the method name that was intercepted
Logger.info ("Requset Method name is:" + methodName);
Logger.info ("Request URL is:" + request.getrequesturl (). toString ());
Logger.info ("Request HTTP Method:" + Request.getmethod ());
Logger.info ("Request arguments is:" + arrays.tostring (Pjp.getargs ()));
RetVal = Pjp.proceed ();
RetVal = RetVal + "(@Around can change the return value!)";
System.out.println (called after the around--target method returns);
} catch (Throwable e) {
System.out.println (called after the around--target method throws an exception);
}
return retVal;
}
}
```
3. With advice you can add some functionality to add new methods to an object, use introduction, and write another slice Aspectintroductionconfig.java
```
Package COM.ANSEL.TESTALL.AOP;
Import Org.aspectj.lang.annotation.Aspect;
Import org.aspectj.lang.annotation.DeclareParents;
Import org.springframework.stereotype.Component;
@Aspect
@Component
public class Aspectintroductionconfig {
/**
* The static attributes marked with the Declareparents annotations indicate the interfaces to be introduced;
* The Value property specifies which type of bean to introduce the interface to;
* The Defaultimpl property specifies the class that provides the implementation for the ingest feature.
*/
@DeclareParents (value = "com.ansel.testall.aop.aopservice+", Defaultimpl = Introductionserviceimpl.class)
public static Introductionservice Introductionservice;
}
```
4. Write two interfaces Aopservice, Introductionservice, and 2 implementations Aopserviceimpl, Introductionserviceimpl
```
Package COM.ANSEL.TESTALL.AOP;
Public interface Aopservice {
String Myownmethod ();
}
/***********************************/
Package COM.ANSEL.TESTALL.AOP;
Public interface Introductionservice {
String Introductionmethod ();
}
/***********************************/
Package COM.ANSEL.TESTALL.AOP;
Import Org.springframework.stereotype.Service;
@Service
public class Aopserviceimpl implements Aopservice {
@Override
Public String Myownmethod () {
Return "This method was from Aopservice";
}
}
/***********************************/
Package COM.ANSEL.TESTALL.AOP;
Import Org.springframework.stereotype.Service;
@Service
public class Introductionserviceimpl implements Introductionservice {
@Override
Public String Introductionmethod () {
Return "This method from Introduction.";
}
}
```
5. Write a pointcut instance [Aopcontroller.java] (https://github.com/x113773/testall/blob/master/src/main/java/com/ansel/testall/ Aop/aopcontroller.java) (In fact, it is an ordinary controller)
```
Package COM.ANSEL.TESTALL.AOP;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Org.springframework.web.bind.annotation.RestController;
@RestController
public class Aopcontroller {
/**
* Only inject Aopservice
*/
@Autowired
Aopservice Aopservice;
/**
* Test AOP notifications (Advice, also translation enhancements)
*
* @return
*/
@RequestMapping (value = "/aop", method = Requestmethod.get)
Public String Testaop () {
Return "This is a AOP Advice test.";
}
/**
* Test AOP Introduction (Introduction)
*
* @return
*/
@RequestMapping (value = "/aop/introdution", method = Requestmethod.get)
Public String testaopintroduction () {
System.out.println (Aopservice.myownmethod ());
Interface type conversions
Introductionservice Introductionservice = (introductionservice) aopservice;
System.out.println (Introductionservice.introductionmethod ());
Return "This is a AOP Introduction test.";
}
}
```
4. Start the project, trigger the LOCALHOST:8080/AOP request, the console output is as follows:
"
around--the target method call before executing
2017-07-03 17:33:15.494 INFO 8548---[nio-8443-exec-7] tconfig$$ Enhancerbyspringcglib$$5f4562fb:requset method Name Is:testaop
2017-07-03 17:33:15.495 INFO 8548---[nio-8443-exec -7] tconfig$ $EnhancerBySpringCGLIB $$5f4562fb:request URL is:https://localhost:8443/aop
2017-07-03 17:33:15.495 INFO 8548---[nio-8443-exec-7] tconfig$ $EnhancerBySpringCGLIB $$5f4562fb:request http method:get
2017-07-03 17:33:15.495 INFO 8548---[nio-8443-exec-7] tconfig$ $EnhancerBySpringCGLIB $$5f4562fb:request arguments are: []
The before--notification method is called after the target method call executes the
around--target method returns
after--the notification method calls
when the target method returns or throws an exception. The afterreturning--notification method is called after the target method returns; RetVal = This is a AOP test. (@Around can change the return value!) (@AfterReturning can read the return value, but it can ' t change the value!)
Execution (Public java.lang.String Com.ansel.testall.aop.AOPController.testAOP ())
"'
The results of the page return are as follows:
' This is a ' AOP test. (@Around can change the return value!) `
To trigger the Localhost:8080/aop/introdution request, the console outputs the following results:
! [QQ 20170704124306] (https://user-images.githubusercontent.com/24689696/27815388-6151c8ca-60b6-11e7-86eb-aa5f2772caa5.png)
---
Note:
' @Pointcut ("Execution (* COM.ANSEL.TESTALL.AOP). *(..)) and @annotation (Org.springframework.web.bind.annotation.RestController) ') '
When I use the AND operator to connect the above two pointcut indicators, there is no problem (change the annotation restcontroller to requestmapping)
However, when I use the && operator to connect the above two tangent indicators, there is no trigger aspectj, and the slices are not woven into any connection points (change the annotation restcontroller to requestmapping is fine)
```
/**
* OK
*/
@Pointcut ("Execution (* COM.ANSEL.TESTALL.AOP). *(..)) and @annotation (Org.springframework.web.bind.annotation.RestController) ")
/**
* Not OK
*/
@Pointcut ("Execution (* COM.ANSEL.TESTALL.AOP). *(..)) && @annotation (Org.springframework.web.bind.annotation.RestController) ")
/**
* OK
*/
@Pointcut ("Execution (* COM.ANSEL.TESTALL.AOP). *(..)) and @annotation (org.springframework.web.bind.annotation.RequestMapping) ")
/**
* OK
*/
@Pointcut ("Execution (* COM.ANSEL.TESTALL.AOP). *(..)) && @annotation (org.springframework.web.bind.annotation.RequestMapping) ")
```
Spring Boot1.5.4 AOP Instances