The basic conditions for the @Aspect annotation to take effect and the reason why the Controller does not take effect

A few days ago, I learned Spring AOP and wanted to apply it in the project to complete the log display of the control layer.

Capture the request counterpart and the returned result (custom type) of the method annotated with @RequestMapping for log management

used @AfterReturning

The method of obtaining the request in the notification before returning cannot be done through the joinPoint.getArgs() method, but by the following methods

HttpServletRequest  request =  ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();

The object of the request will be recorded in the Spring context

 

Now let's summarize some of the problems encountered.

1. The class of the AOP proxy must be loaded (assembled into the beanFactory) when the program is started, and the objects (new) created in the middle and later stages of the business will not be captured by the aspect. Must pass getBean().

 

2. For the @ReuqestMapping method in the class marked by @Controller of the control layer, when

<context:component-scan base-package="com.wenzy.xx">

        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service">

</context:component-scan>

and

<aop:aspectj-autoproxy proxy-target-class="true"/>

If it is not in the same xml, there will be cases where the methods in @Controller cannot be cut.

 

3. If you want to directly cut the method annotated by @RequestMapping, you can write it as the pointcut definition

@Poincut("@annotation(org.springframework.web.bind.annotation.RequestMapping) && execution(* com .wenzy..*(..))")

&& is followed by the specified package (regular expression) so that the two conditions can be combined.

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326593713&siteId=291194637