基于springMvc+spring的@Aspect不生效的小问题

故事是这样的,想记录一个controller里面的部分数据到数据库中(因为只有三行代码就犯懒没有再写service),controller在SpringMVC的配置文件扫描,其他应该扫描的在spring配置文件扫描

实现如下:

@Aspect

@Component

public class XXXLogRecorder {

    @Around("execution(* XXX.controller.XXXSearchController.availXXX(..))")

    public Object logHotelAvailStatusByMonth(ProceedingJoinPoint joinPoint) throws Throwable {

        //...

    }

}


结果增么都进入不了这个方法中。

原因如下:spring和springMVC是两个不同的容器,因为controller是springMVC容器加载的,而XXXLogRecorder是spring容器加载的,是无法互通的,所以AOP未生效。

更改方式:把XXXLogRecorder的生成管理交给springMVC容器。

即去掉@Component注解,然后在springMVC的配置文件中手动创建XXXLogRecorder

@Aspect

public class XXXLogRecorder {

    @Around("execution(* XXX.controller.XXXSearchController.availXXX(..))")

    public Object logHotelAvailStatusByMonth(ProceedingJoinPoint joinPoint) throws Throwable {

        //...

    }

}

在springMVC配置文件中添加:

<bean id="xxxLogRecorder" class="xxx.log.XXXLogRecorder"/>

猜你喜欢

转载自blog.csdn.net/roy324/article/details/79406434