建设宠物食品网站的功能定位,免费网站空间 推荐,微信小程序开发零基础入门,网站设计深圳要联系方式吗?一、什么是AOP
AOP#xff0c;Aspect Oriented Programming#xff0c;面向切面编程
举个例子来理解 如果我们的业务需要额外做三件事情#xff0c;判断是否已经登录#xff0c;记录日志#xff0c;统计业务执行时长
传统的做法是这样子的#xff1a; 而apo的实现是这…一、什么是AOP
AOPAspect Oriented Programming面向切面编程
举个例子来理解 如果我们的业务需要额外做三件事情判断是否已经登录记录日志统计业务执行时长
传统的做法是这样子的 而apo的实现是这样的
区别在于 原本的做法是需要什么功能就调用什么功能的方法需要我们主动去调用
aop的做法是需要什么功能就把业务交给相应的代理人由代理人帮我们去完成是被动完成的
aop做法的好处是 1、代码解耦 把不相关的功能都抽取出来需要使用哪个功能就把业务交给专业的代理人由代理人去完成。需要新功能的时候直接添加新的代理业务这样不会对其它的业务造成影响
2、提高开发效率 当需要使用某个功能时我们不需要关心代理是怎么做的只需要简单地使用就好了就像事务注解Transactional只要添加了该注解方法就会代理获得了事务的功能简化了开发
二、AOP的实现示例
maven依赖
dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-aop/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependenciesLogger 注解
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,即ControllerResponseBody,将返回结果转为json字符串响应到客户端*/
RestController
public class HelloController {/*** Logger,打印日志* Login,判断登录* Time,记录运行时间* RequestMapping,请求路径**/LoggerLoginTimeRequestMapping(/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?pwdwk0i 提取码wk0i