Aop切面到具体方法失效
做了一个aop切面小demo,但是突然发现aop失灵了
以下是小demo的源码
网上查了一下原因,基本就是:不是通过代理对象调用的,而是本身调用的,而aop的切入是需要使用动态代理对象,但是咱们调用的是本身对象。
所以改正方法,就是通过代理对象调用一下方法就可以了
this.serverTest(new HashMap()); 改成以下代码
解决方案
AopService aopService=SpringContextUtils.getBean(AopService.class);
aopService.serverTest(new HashMap());
这样就是属于通过代理对象调用了
package com.example.demo.aspect; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterThrowing; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Aspect @Component @Slf4j public class AspectTest { @Pointcut("execution(* com.example.demo.service.AopService.serverTest(..))") public void test(){} @AfterThrowing(pointcut = "test()",throwing = "exceptio") public void deEx(JoinPoint joinPoint , Exception exceptio){ log.error("=============================执行切面"); } }
package com.example.demo.service; import lombok.extern.slf4j.Slf4j; import org.apache.poi.ss.formula.functions.T; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.ui.context.support.UiApplicationContextUtils;
package com.example.demo; import com.example.demo.service.AopService; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.support.DefaultSingletonBeanRegistry; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletRegistrationBean; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor; import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor; import org.springframework.context.annotation.ConfigurationClassPostProcessor; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; @SpringBootApplication public class DemoApplication1 { public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException { //查看类图 C+A+u ConfigurableApplicationContext context=SpringApplication.run(DemoApplication1.class, args); AopService AopService=(AopService)context.getBean("aopService"); AopService.transa(); //AopService.serverTest(new HashMap()); //context.getMessage(); //context.getResources(); /* Field field = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects"); field.setAccessible(true); ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); Map<String,Object> o = (Map<String,Object>)field.get(beanFactory); o.forEach((k,v)->{ System.err.println(k+"="+v); });*/ //MapperScannerConfigurer } }
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Service @Slf4j public class AopService { public void transa(){ /* 解决方案 AopService aopService=SpringContextUtils.getBean(AopService.class); aopService.serverTest(new HashMap());*/ this.serverTest(new HashMap()); } public void serverTest(Map map){ List list=new ArrayList<>(); list.get(10); log.error("=======进入service"); } }