【发布时间】:2017-12-26 00:08:08
【问题描述】:
我正在尝试通过以下方式拦截带有方面的休息服务调用
package mypackage.services.Service;
@Component
public class Service {
@Override
public Response helloService() {
return handleResult("Hello test " + new Date());
}
}
@Component
@Aspect
public class AuditLog {
@Before("execution(* mypackage.services.Service.*(..))")
public void beforeServcie(JoinPoint jp){
log.info("Before ",jp.getSignature().getName());
}
}
我正在使用以下 maven 依赖项
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.6</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.10</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
这个maven插件
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.0</version>
</plugin>
而我的配置 xml 包含
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="mypackage"/>
<aop:aspectj-autoproxy proxy-target-class="true" />
我还在 Application 类中添加了以下注释
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class Configuration{
...
}
在启动时,通过在 ApplicationContext 中记录 bean,我可以看到正在创建方面类“AuditLog”。
我设置了 2 个断点,但调试器不会在“beforeServcie”方法处停止,而是在“helloService”处停止。
我错过了什么?
【问题讨论】:
-
试过了,没用
-
你不需要所有这些 aspectj 依赖项
-
为什么会有单独的xml配置?
-
proxyTargetClass=true 不需要
-
哪些依赖是多余的?我已经删除了 targetClass=True,但它仍然没有从 Aspect 方法传递
标签: java maven spring-boot spring-aop aspect