About spring aop's @RequestMapping method aspect method for spring mvc, the solution is not executed

1. The phenomenon of the problem

1. Using Spring MVC, create a Controller class and @RequestMapping method

@Controller
@RequestMapping("/product")
public class ProductController {
    
    
    @Autowired
    ProductServiceImpl productService;

    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name = "pageNum")Integer pageNum, @RequestParam(name = "pageSize")Integer pageSize) throws MyException {
    
    
        System.out.println("执行findall......");

2. Use the method in the Spring AOP aspect Controller class

@Component("agentController")
@Aspect
public class AgentController {
    
    
    private Date visitTime;
    private Date endTime;
    private int executionTime;   //执行时长
    private Class clazz;         //执行的类
    private Method method;       //执行的方法
    private SysLog sysLog = new SysLog();

    @Pointcut("execution(* com.mediacomm.controller.*.*(..))")
    public void service(){
    
    }
    @Autowired
    private SysLogService sysLogService;
    @Autowired
    private HttpServletRequest request;

    @Before("service()")
    public void beforeAdvice(JoinPoint pro) throws NoSuchMethodException, ParseException {
    
    

        System.out.println("前置通知......"+sysLog.getId());

3. Relying on the idea to view the goal of the pre-notification method aspect is the success of the proxy.
insert image description here
4. However, the actual situation is that the pre-notification does not cut in at all, and the console does not print out: pre-notification...

Two, the solution

Reasons:
1. I configure aop based on annotations, so I need to add the
opening statement of <aop:aspectj-autoproxy></aop:aspectj-autoproxy> in the spring configuration file
. 2. But I am in the spring configuration file Annotation scanning is added to ignore

<context:component-scan base-package="com.mediacomm">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

3. Therefore, there is no Controller class bean in the spring container
4. Therefore, no corresponding method can be found for the aop entry point
Solution:
configure <aop:aspectj-autoproxy></aop:aspectj-autoproxy> in the responsible scanning @ In the configuration file of springmvc annotated by Controller, the console output after re-running:

前置通知......null

Conclusion:
You need to add <aop:aspectj-autoproxy></aop:aspectj-autoproxy> according to the configuration file of the bean entity created by the target object of your aspect method

Guess you like

Origin blog.csdn.net/weixin_42717117/article/details/119759182