關於spring的aop應用,其實用好了或者會用了對解決一些比較複雜的問題非常有幫助。
還是同樣上代碼:
在昨天的項目中新建一個包來實戰這個案例即可
1.添加 spring aop 支持及AspectJ 依賴
<!-- 添加 spring aop 支持及AspectJ 依賴 start -->
<!--spring aop支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<!--aspectj支持 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.5</version>
</dependency>
<!-- 添加 spring aop 支持及AspectJ 依賴 end -->
2.編寫攔截規則的註解
package com.amarsoft.springboot.aop;
import java.lang.annotation.Documented;
import java.lang.annotation.Target;
import java.lang.annotation.Retention;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String name();
}
package com.amarsoft.springboot.aop;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("com.amarsoft.springboot.aop")
@EnableAspectJAutoProxy
public class AopConfig {
}
3.編寫使用註解的被攔截類
package com.amarsoft.springboot.aop;
import org.springframework.stereotype.Service;
@Service
public class DemoAnnotationService {
@Action(name=">>>註解式攔截的add操作")
public void add(){
};
}
4.編寫使用方法規則被攔截類
package com.amarsoft.springboot.aop;
import org.springframework.stereotype.Service;
@Service
public class DemoMethodService {
public void add(){
System.out.println("-------Method方法規則式攔截------------");
}
}
5.編寫切面 。注意配置切點表達式,我運行的時候有報錯,主要是
Pointcut is not well-formed: expecting 'name pattern' at character position
錯誤原因就在於通配符*要有空格
package com.amarsoft.springboot.aop;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LogAspect {
@Pointcut("@annotation(com.amarsoft.springboot.aop.Action)")
public void annotationPointCut(){
};
@After("annotationPointCut()")
public void after(JoinPoint joinPoint){
MethodSignature signature=(MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Action action = method.getAnnotation(Action.class);
System.out.println("註解式攔截"+action.name());
}
@Before("execution(* com.amarsoft.springboot.aop.DemoMethodService.add(..))")
public void before(JoinPoint joinPoint){
MethodSignature signature=(MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("方法規則攔截"+method.getName());
}
}
6.配置類。這個類的作用就是@EnableAspectJAutoProxy開啓spring對AspectJ的支持
package com.amarsoft.springboot.aop;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("com.amarsoft.springboot.aop")
@EnableAspectJAutoProxy
public class AopConfig {
}
7.測試入口類
package com.amarsoft.springboot.aop;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AopApplication {
public static void main(String[] args) {
AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(AopConfig.class);
DemoAnnotationService demoAnnotationService =context.getBean(DemoAnnotationService.class);
DemoMethodService demoMethodervice= context.getBean(DemoMethodService.class);
demoAnnotationService.add();
demoMethodervice.add();
context.close();
}
}
運行結果截圖:
這只是一個簡單的AOP的例子,以後大家要打印日誌,或者權限校驗,或者其他的一些操作,具有共性的都可以參考這種方式老開發。