aspectj-autoproxy Controller does not take effect

 Do business needs this week, you need to monitor the API response time. The first idea is to allow logs in the APIs that need to be monitored and record care information. The biggest problem is that it is not easy to expand. You need to add almost the same code in each API. At that time, the general practice of Java was slicing. Using the reflection capabilities provided by the Java language, combined with the aop (aspect-oriented programming) idea of ​​one of the three major characteristics of spring, it was possible to completely decouple the addition of slicing. Under the principle of poly programming, it is more elegant, but it is inevitable to sacrifice efficiency. Reflection is a dynamic feature of the object runtime and requires additional performance. Thinking that there was no separate introduction of slicing in the project before, while maintaining the elegance of the code, the decision was to slice.

step

1. Business slice code  


@Aspect
@Component
public class ApiAspect {


@Around("execution (* com.jinxu.api..*.*(..))")
public Object apiLog(ProceedingJoinPoint joinPoint) throws Throwable {
Long startTime = 0l;
Long endTime = 0l;
Object result = null;
try {
startTime = System.currentTimeMillis();
result = joinPoint.proceed();
endTime=System.currentTimeMillis();
} catch (Exception e) {
throw e;
} finally {
try {
Object[] args = joinPoint.getArgs();
Object object = args[0];
if (object instanceof HttpServletRequest) {
object = ((HttpServletRequest) object).getParameterMap();
}
String method = joinPoint.getSignature (). GetName ();
String request = JsonUtils.json (object); // Note the correspondence between json and non-json here
String response = JsonUtils.json (result); //
logger.debug (String.format ("method:% s" + "-request:% s" + "-response:% s" + "-cost:% s", method, request, response, endTime-startTime));
} catch (Exception e) {
logger.error ("com.jinxu.api package slicing exception, but does not affect normal business:" + e);
}
}
return result;
}
}
 The jar packages to be introduced are as follows:

<dependency>
<groupId> org.aspectj </ groupId>
<artifactId> aspectjweaver </ artifactId>
</ dependency>
<dependency>
<groupId> org.springframework </ groupId>
<artifactId> spring-aop </ artifactId>
</ dependency>
<dependency>
<groupId> cglib </ groupId>
<artifactId> cglib-nodep </ artifactId>
</ dependency>
  where cglib is the code that extends the Java class and implements the Java interface at runtime, which is the extension of jdk reflection, generally Use it to replace jdk comes with more available.
2. Configure spring to load files

<!-AOP->
<aop: aspectj-autoproxy proxy-target-class = "true" />
  which can load slices with @Aspect tags in the code, the value of the proxy-target-class attribute determines whether it is an interface or a class The agent was created. If the proxy-target-class attribute value is set to true, then the class proxy will work, such as the cglib library; if the proxy-target-class attribute value is set to false or this attribute is omitted, then the standard JDK interface-based proxy. At that time, if there is no corresponding interface, only the implementation, the class proxy will work.

3. Problems

  Regarding the use of slices, there are indeed many other projects and online materials, so they are directly configured. It was found that the actual Controller slice did not take effect, and the aop slice was not executed. In general, imitating other configurations and putting them in applicationContext.xml will work, so I checked a lot of information and compared with other projects, there is no real gain.

  The aspects and the beans to be applied needs to be in the same ApplicationContext but ApplicationContext is not aware of

 WebApplicationContext . 
  Indeed your controller (annotated by @Controller) and your aspects (annotated by @Aspect) should be in the same Spring context. 

  Usually people define their controllers in the dispatch-servlet.xml or xxx-servlet.xml and their service beans (including the aspects) in the

 main applicationContext.xml. It will not work. 

  When Spring initializes the MVC context, it will create a proxy for your controller but if your aspects are not in the same context, Spring will not create interceptors for them. 

  Translated is that the Controller is defined in the xxx-servlet.xml configuration file, so the aspect slices defined in applicationContext.xml will not take effect, and the definition of aspects needs to be transferred to the xxx-servlet.xml file. The problem is solved after the modification, if you encounter, you can refer to the above simple steps.
————————————————
Copyright Statement: This article is an original article by CSDN blogger "Xiaofeng Canyue xj", following the CC 4.0 BY-SA copyright agreement, please attach the original source link for reprint And this statement.
Original link: https://blog.csdn.net/xj2419174554/java/article/details/52626974

Guess you like

Origin www.cnblogs.com/sidesky/p/12717832.html