一:背景
Spring的AOP的存在目的是为了解耦。AOP可以让一组类共享相同的行为。在OOP中只能通过继承类和实现接口,来是代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类上,AOP弥补了OPP的不足。
二:概述 Spring支持AspectJ的注解方式切面编程
1.使用@Aspect 声明一个切面。
2.使用@After,@Before,@Around 定义建言(advice),可直接将拦截规则(切点)作为参数。
3.其中@After,@Before,@Around参数的拦截规则为切点(PointCut) ,为了使切点复用,可使用@PointCut 专门定义拦截规则,然后在
@After,@Before,@Around的参数中调用。
4.其中符合条件的每一个被拦截处为连接点(JoinPoint)
三:代码实例
1.pom.xml
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-core</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-beans</artifactId>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-context</artifactId>
-
</dependency>
-
-
<dependency>
-
<groupId>org.springframework</groupId>
-
<artifactId>spring-aop</artifactId>
-
</dependency>
-
-
<dependency>
-
<groupId>org.aspectj</groupId>
-
<artifactId>aspectjrt</artifactId>
-
</dependency>
-
-
<dependency>
-
<groupId>org.aspectj</groupId>
-
<artifactId>aspectjweaver</artifactId>
-
</dependency>
2.拦截规则的注解
-
@Target(ElementType.METHOD)
-
@Retention(RetentionPolicy.RUNTIME)
-
@Documented
-
public @interface Action {
-
String name();
-
}
2.注解的被拦截类
-
@Service
-
public class DemoAnnotationService {
-
-
@Action(name = "注解式拦截的add操作")
-
public void add() {
-
System.out.println("======DemoAnnotationService方法add()=========");
-
}
-
}
3.方法规则被拦截类
-
@Service
-
public class DemoMethodService {
-
public String add() throws Exception{
-
System.out.println("======DemoMethodService方法add()=========");
-
int i=100/0;
-
return "SUCCESS";
-
}
-
}
4.编写切面
-
@Aspect
-
@Component
-
public class LogAspect {
-
-
@Pointcut("@annotation(com.gemdale.gmap.spring.boot.demo.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.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))")
-
public void methodBefore(JoinPoint joinPoint) {
-
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
-
Method method = signature.getMethod();
-
System.out.println("before方法规则式拦截 " + method.getName());
-
}
-
-
@After("execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))")
-
public void methodAfter(JoinPoint joinPoint) {
-
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
-
Method method = signature.getMethod();
-
System.out.println("after方法规则式拦截 " + method.getName());
-
}
-
-
@AfterReturning(pointcut = "execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))", returning = "result")
-
public void methodAfterResult(JoinPoint joinPoint, Object result) {
-
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
-
Method method = signature.getMethod();
-
System.out.println("after result方法规则式拦截 " + method.getName() + "result=" + result);
-
}
-
-
@AfterThrowing(pointcut = "execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))", throwing = "e")
-
public void methodAfterException(JoinPoint joinPoint, Exception e) {
-
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
-
Method method = signature.getMethod();
-
System.out.println("after exception方法规则式拦截 " + method.getName() + " e=" + e.getMessage());
-
}
-
-
}
5.配置类
-
@Configuration
-
@ComponentScan("com.gemdale")
-
@EnableAspectJAutoProxy
-
public class AppliactionConfig {
-
-
}
6.执行类
-
public class Start {
-
public static void main(String[] args) throws Exception{
-
-
AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(
-
AppliactionConfig.class);
-
// UseFunctionService
-
// useFunctionService=configApplicationContext.getBean(UseFunctionService.class);
-
// System.out.println(useFunctionService.sayHello("Gengchong"));
-
DemoAnnotationService demoAnnotationService = configApplicationContext.getBean(DemoAnnotationService.class);
-
DemoMethodService demoMethodService = configApplicationContext.getBean(DemoMethodService.class);
-
-
demoAnnotationService.add();
-
-
demoMethodService.add();
-
-
configApplicationContext.close();
-
}
-
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28624388/viewspace-2133594/
,如需转载,请注明出处,否则将追究法律责任。