springAOP生效不需要加@EnableAspectJAutoProxy注解.springframework.stereotype.Service;/***@date2022/8/915:28*/@ServicepublicclassUserServiceimplementsUs{@OverridepublicvoidsaveUser(Useruser){System.out.println("保存用户对象到数据库:"+user);方面类,LogAspect.javapackagecom.my.template.aop;importorg.aspectj.lang.JoinPoint;importorg.aspectj.lang.annotation.AfterReturning;importorg.aspectj.lang.annotation.Aspect;importorg.aspectj.lang.annotation.Before;importorg.aspectj.lang.annotation.Pointcut;importorg.springframework.stereotype.Component;/***@date2022/8/1114:12*/@Component@Aspectpublic类LogAspect{@Pointcut("execution(*com.my.template.service.UserService.*(..))")publicvoidpointCut(){}@Before(value="pointCut()")publicvoidbefore(JoinPointjoinPoint){System.out.println("方法执行前-20220816");}@AfterReturning(value="pointCut()")publicvoidafter(JoinPointjoinPoint){System.out.println("方法执行后-20220816");}}测试的controllerUserController.javapackagecom.my.template.controller;importcom.my.template.entity.User;importcom.my.template.service.Us;importcom.my.template.service.UserService;importorg.springframework.beans.factory.annotation.Autowired;导入org.springframework.web.bind.annotation.RequestMapping;导入org.springframework.web.bind.annotation.RestController;/***@date2022/8/915:35*/@RestControllerpublicclassUserController{@Autowiredprivate我们;@RequestMapping("/saveUser")publicStringsaveUser(){用户user=newUser();user.setId("1");user.setName("张三");us.saveUser(用户);返回“成功”;}}sprinboot的启动类,BootServer.javapackagecom.my.template;导入org.springframework.boot.SpringApplication;导入org.springframework.boot.autoconfigure.SpringBootApplication;导入org.springframework.context.annotation.EnableAspectJAutoProxy;导入org.springframework.context.annotation.ImportResource;/***启动类*@date2022/6/321:32*/@SpringBootApplication()publicclassBootServer{publicstaticvoidmain(String[]args){try{SpringApplication.run(BootServer.class);}catch(异常e){e.printStackTrace();}}}测试结果如下,2022-08-1622:30:44.082INFO25716---[nio-9099-exec-1]o.s.web.servlet.DispatcherServlet:Completedinitializationin6ms之前的方法是executed-20220816将用户对象保存到数据库中:User{name='张三',id='1'}方法执行后-20220816从上面的测试结果看,没有@EnableAspectJAutoProxy注解,但是AOP是起作用的,为什么?2、为什么不加@EnableAspectJAutoProxy切面生效。这个问题我排查了好久,终于在dependencies中找到了原因。查看pom文件pom.xml
