AOP:面向切面编程。
Spring的AOP的存在目的是为了解耦。AOP可以让一组类共享相同的行为。在OOP中只能通过集成类和实现接口,来使代码的耦合度增强。且类继承只能为单继承,阻碍更多行为添加到一组类上。
Spring支持AspectJ的注解式切面编程。
(1)使用@Aspect声明是一个切面。
(2)使用@After、@Before、@Around定义建言(advice),可直接将拦截规则(切点pointcut)作为参数。
(3)可使用@PointCut专门定义拦截规则,然后再@After、@Before、@Around的参数中调用。
(4)复合条件的每一个被拦截处为连接点(JoinPoint)。
拦截有两种方式:基于注解拦截、基于方法规则拦截。
配置pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.jy</groupId>
<artifactId>testProj</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.10.RELEASE</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>
</dependencies>
</project>
一、编写拦截规则的注解
package aop.rule;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Created by JY on 2017/9/1.
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAction1 {
String name();
}
二、编写使用注解的被拦截类
package aop.service;
import aop.rule.MyAction1;
import org.springframework.stereotype.Service;
/**
* Created by JY on 2017/9/1.
*/
@Service
public class MyService {
@MyAction1(name="注解式拦截的add操作")
public void add() {}
}
三、编写使用方法规则被拦截类
package aop.DemoService;
import org.springframework.stereotype.Service;
/**
* Created by JY on 2017/9/1.
*/
@Service
public class DemoService1 {
public void add() {}
}
四、编写切面
package aop.aspect;
import aop.rule.MyAction1;
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;
import java.lang.reflect.Method;
/**
* Created by JY on 2017/9/1.
*/
@Aspect
@Component
public class LogAspect {
@Pointcut("@annotation(aop.rule.MyAction1)")
public void annotationPointCut() {};
@After("annotationPointCut()")
public void after(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
Method method = signature.getMethod();
MyAction1 action = method.getAnnotation(MyAction1.class);
System.out.println("------------------");
System.out.println("注解式拦截 " + action.name());
}
@Before("execution(* aop.DemoService.DemoService1.*(..))")
public void before(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("方法规则式拦截, " + method.getName());
}
}
五、配置类
package aop.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
/**
* Created by JY on 2017/9/1.
*/
@Configuration
@ComponentScan("aop")
@EnableAspectJAutoProxy
public class aopConfig1 {
}
六、运行
package aop.entrance;
import aop.DemoService.DemoService1;
import aop.config.aopConfig1;
import aop.service.MyService;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* Created by JY on 2017/9/1.
*/
public class aopEntrance1 {
public static void main(String args[]) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(aopConfig1.class);
MyService m = context.getBean(MyService.class);
DemoService1 d = context.getBean(DemoService1.class);
m.add();
d.add();
context.close();
}
}
浙公网安备 33010602011771号