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();
}
}