AOP
面向切片编程,Spring中使用切片编程的目的是为了实现解耦,AOP可以实现一组类共享相同行为。
使用注解式拦截代码
maven依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!--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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
- 编写注解
package day0123.AOP01;
import java.lang.annotation.*;
/**
* @Author Braylon
* @Date 2020/1/23 20:40
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String info();
}
- 被拦截类
package day0123.AOP01;
import org.springframework.stereotype.Service;
/**
* @Author Braylon
* @Date 2020/1/23 20:43
*/
@Service
public class DemoAnnotationService {
@Action(info = "注解式拦截的业务操作")
public void service1() {
//业务操作
System.out.println("注解式拦截的业务逻辑");
}
}
方法规则拦截代码
被拦截类
package day0123.AOP01;
import org.springframework.stereotype.Service;
/**
* @Author Braylon
* @Date 2020/1/23 20:47
*/
@Service
public class DemoMethodService {
public void service1() {
//业务逻辑
System.out.println("方法规则拦截业务逻辑");
}
}
切面
package day0123.AOP01;
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;
/**
* @Author Braylon
* @Date 2020/1/23 20:49
*/
@Aspect
@Component
public class LogAspect {
@Pointcut("@annotation(day0123.AOP01.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.info());
}
@Before("execution(* day0123.AOP01.DemoMethodService.*(..))")
public void before(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println("方法规则拦截" + method.getName());
}
}
配置类
package day0123.AOP01;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
/**
* @Author Braylon
* @Date 2020/1/23 20:58
*/
@Configuration
@ComponentScan("day0123.AOP01")
@EnableAspectJAutoProxy //开启Spring对AspectJ的支持
public class conf {}
测试
package day0123.AOP01;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @Author Braylon
* @Date 2020/1/23 20:47
*/
public class actionTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(conf.class);
DemoAnnotationService demoannotationservice = context.getBean(DemoAnnotationService.class);
DemoMethodService demomethodservice = context.getBean(DemoMethodService.class);
demoannotationservice.service1();
demomethodservice.service1();
context.close();
}
}