Springboot配置AOP的注解切点失效问题

问题描述:在静态方法中调用本类的另一个静态方法,需要在AOP中拦截该方法实现一部分业务逻辑,在aop类中添加了@Aspect@Component@EnableAspectJAutoProxy三个注解,切点通知方式为@Around,但是切点一直无法被拦截。

问题发现:当前类的实例是通过applicaitoncontext.getBean(类名.class)获取的,所以一直无法在切点无法被拦截(代码中的第一种方式)。修改为实现了ApplicationContextAware接口的getBean(类名.class)(代码中的第二种方式),就可以正常被拦截了。(applicaitoncontext和ApplicationContextAware的getBaen()方法的异同点查询文档可知:当一个类实现了这个接口(ApplicationContextAware)之后,这个类就可以方便获得ApplicationContext中的所有bean。换句话说,就是这个类可以直接获取spring配置文件中,所有有引用到的bean对象。)

@Component
public class ApplicationContextUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
public static <T> T getBean(Class<T> tClass){
return applicationContext.getBean(tClass);
    }
public static <T> T getBean(String name , Class<T> tClass){
return applicationContext.getBean(name,tClass);
    }
@Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextUtils.applicationContext = applicationContext;
    }
}
@Component
public class SendSmm {
private static SendSmm sendSmm=null;
public static void test1(){
if(null == sendSmm){
sendSmm = Const.applicationContext.getBean(SendSmm.class);//第一种
            sendSmm = ApplicationContextUtils.getBean(SendSmm.class);//第二种
        }
sendSmm.test2();
    }
public void test2(){
    }
}
public class Const {
public static ApplicationContext applicationContext = null ;
}
@Aspect
@Component
@EnableAspectJAutoProxy
public class TestAopUtils {
@Pointcut("execution( public * com.example.demo.service.*(..))")
public void testAop(){}
@Around("testAop()")
public Object Around (ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
Object object = null;
Object[] args = proceedingJoinPoint.getArgs();//获取切点的请求参数
        String st = (String) args[0];//第一个参数
        proceedingJoinPoint.proceed();//调用原方法
        return object ;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot配置AOP(面向切面编程),可以通过以下步骤进行: 1. 首先,在你的Spring Boot项目中添加所需的依赖。在`pom.xml`文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> ``` 2. 创建一个切面类,用于定义你的切面逻辑。切面类需要使用`@Aspect`注解进行标记,并且通常需要在类中定义多个切点和通知。例如: ```java @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.demo.service.*.*(..))") public void beforeAdvice(JoinPoint joinPoint) { System.out.println("Before method: " + joinPoint.getSignature()); } @After("execution(* com.example.demo.service.*.*(..))") public void afterAdvice(JoinPoint joinPoint) { System.out.println("After method: " + joinPoint.getSignature()); } } ``` 上述切面类中,`@Before`注解表示在目标方法执行之前执行,`@After`注解表示在目标方法执行之后执行。`execution(* com.example.demo.service.*.*(..))`表示匹配`com.example.demo.service`包下的所有类的所有方法。 3. 在Spring Boot应用程序的主类上添加`@EnableAspectJAutoProxy`注解,启用AOP代理。例如: ```java @SpringBootApplication @EnableAspectJAutoProxy public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` 4. 运行你的Spring Boot应用程序,并观察控制台输出。你应该能够看到在匹配的方法执行前后打印的日志信息。 这就是在Spring Boot配置AOP的基本步骤。你可以根据需要定义更多的切点和通知,以实现更复杂的切面逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sheclockes

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值