AOP,Aspect Oriented Programming,面向切面编程
举个例子来理解
如果我们的业务需要额外做三件事情,判断是否已经登录,记录日志,统计业务执行时长
区别在于:
原本的做法是,需要什么功能,就调用什么功能的方法,需要我们主动去调用
aop的做法是,需要什么功能,就把业务交给相应的代理人,由代理人帮我们去完成,是被动完成的
aop做法的好处是:
1、代码解耦
把不相关的功能都抽取出来,需要使用哪个功能就把业务交给专业的代理人,由代理人去完成。需要新功能的时候,直接添加新的代理业务,这样不会对其它的业务造成影响
2、提高开发效率
当需要使用某个功能时,我们不需要关心代理是怎么做的,只需要简单地使用就好了,就像事务注解@Transactional,只要添加了该注解,方法就会代理,获得了事务的功能,简化了开发
maven依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Logger 注解:
package com.aop.annotation;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Logger {
}
Logger 注解代理类:
package com.aop.aop;
import com.aop.annotation.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
/**
* LoggerAop代理类
*/
@Component
@Aspect
public class LoggerAop {
@Around(value = "@annotation(logger)")
public Object around(ProceedingJoinPoint pjp, Logger logger) throws Throwable {
System.out.println("开始记录日志");
Object proceed = pjp.proceed();
return proceed;
}
}
Loggin 注解:
package com.aop.annotation;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Login {
boolean login() default true;
}
Loggin 注解代理类:
package com.aop.aop;
import com.aop.annotation.Login;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
/**
* LoginAop代理类
*/
@Component
@Aspect
public class LoginAop {
@Around("@annotation(login)")
public Object around(ProceedingJoinPoint pjp, Login login) throws Throwable {
System.out.println("开始判断登录");
if(login.login()){
System.out.println("已经登录,可执行方法");
Object proceed = pjp.proceed();
return proceed;
}else{
System.out.println("未登录,不执行方法");
return "请先登录!";
}
}
}
Time注解:
package com.aop.annotation;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Time {
}
Time注解代理类:
package com.aop.aop;
import com.aop.annotation.Time;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* TimeAop代理类
*/
@Component
@Aspect
public class TimeAop {
@Around("@annotation(time)")
public Object around(ProceedingJoinPoint pjp, Time time) throws Throwable {
long time1 = new Date().getTime();
Object proceed = pjp.proceed();
long time2 = new Date().getTime();
System.out.println("运行了"+((Double.valueOf(time2)-time1)/1000) + "秒");
return proceed;
}
}
测试controller类:
package com.aop.controller;
import com.aop.annotation.Logger;
import com.aop.annotation.Login;
import com.aop.annotation.Time;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* RestController,即Controller+ResponseBody,将返回结果转为json字符串响应到客户端
*/
@RestController
public class HelloController {
/**
* Logger,打印日志
* Login,判断登录
* Time,记录运行时间
* RequestMapping,请求路径
*
*/
@Logger
@Login
@Time
@RequestMapping("/hello")
public String hello() throws InterruptedException {
/**
* sleep,模拟业务时间
*/
Thread.sleep(500);
System.out.println("hello");
return "hello";
}
}
启动类:
package com.aop;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AopApplication {
public static void main(String[] args) {
SpringApplication.run(AopApplication.class, args);
}
}
如果把注解Login的login属性改为false,则验证没有登录,方法不会执行
需要自取
链接:https://pan.baidu.com/s/1imCrOs_DYWT3aO7qdQqoFA?pwd=wk0i
提取码:wk0i