Spring Aspectj
maven jar
<aspectj-version>1.8.2</aspectj-version>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj-version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj-version}</version>
</dependency>
测试感觉spring-aop 和 spring-aspects 貌似没什么用
aspectjweaver 1.8.10 和 spring 4.3.2.RELEASE 有冲突
spring xml
<aop:aspectj-autoproxy proxy-target-class="true" />
如果针对Controller层,需要在spring-mvc中配置,否则不会起作用
配置在spring.xml 中会对service和dao层起作用
aspectJ
@Around 修改传入参数
aspectJ 基于 @Annotation的使用
- 定义注解
@Retention(RetentionPolicy.RUNTIME)
@Target(value = ElementType.METHOD)
public @interface WriteLog {
public String value() default "";
}
- 在需要拦截的地方加入注解
@WriteLog(value = "用户{}做了什么操作")
public String init(HttpServletRequest request, Model model) {
//TODO 业务处理
return "tierOneManager/project/list";
}
- AspectJ 编织处理
@Before(value = "execution(* com.xxx.web.xxx..*(..))&&args(request)&&@annotation(writeLog)", argNames = "joinPoint,request,writeLog")
public void insertLog(JoinPoint joinPoint, HttpServletRequest request, WriteLog writeLog)
throws NoSuchMethodException {
if (null == writeLog) {
return;
}
String messageTpl = writeLog.value();
System.out.println(messageTpl);
System.out.println("xxx");
}