SpringBoot2-spring基础-AOP

xiaoxiao2021-02-27  295

AOP:面向切面编程,相对于oop面向对象编程    spring的Aop的存在是为了解耦,AOP可以让一组类共享相同的行为。在OOP中只能通过基础类和实现接口,来使代码的耦合度增强,且继承只能为单继承,阻碍更多行为添加到一组类上,    AOP补充了OOP的不足。    Spring支持Aspect的注解式切面编程    1)使用Aspect声明式一个切面    2)使用@After,@Before,@Around定义建言(advice),可直接将拦截规则(切点)作为参数    3)其中@After,@Before,@Around参数的拦截规则为切点(PointCut),为了使切点复用,可使用@PointCut专门拦截规则,然后在@After,@Before,@Around的参数中调用    4)其中符合条件的每一个被拦截处为连接点(JoinPoint)    下面就演示基于注解拦截和基于方法规则拦截两种,演示一种模拟记录操作的日子系统。其中注解式拦截能很多的控制拦截的粒度和获得更丰富的信息,spring本身在事务处理

   (@Transactional)和数据缓存(@Cacheable)等上都使用了此种方式的拦截

   添加spirng aop支持和AspectJ依赖

<!--spring的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>

     编写拦截规则的注解

package jack.ch1.aop; import java.lang.annotation.*; /** * Created by wj on 2017/7/8. */ /** *自定义注解,注解本身是没有功能的,就和xml一样。注解和xml都是一种元数据,元数据就是解释数据的数据,这就是所谓的配置 * 注解的功能来自调用这个注解的地方 */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Action { String name(); }

  编写使用注解的被拦截类:

  

package jack.ch1.aop; import org.springframework.stereotype.Service; /** * Created by wj on 2017/7/8. * 使用注解的被拦截类 */ @Service public class DemoAnnotationService { @Action(name = "注解式拦截的add操作") public void add(){} }

  编写使用方法规则被拦截类:

  

package jack.ch1.aop; import org.springframework.stereotype.Service; /** * Created by wj on 2017/7/8. * 使用方法的规则的被拦截类 */ @Service public class DemoMethodService { public void add(){} }

   编写切面:

   

package jack.ch1.aop; 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 wj on 2017/7/8. * 切面 * @Aspect声明切面 * @Component让此切面成为spring容器管理的bean */ @Aspect @Component public class LogAspect { //@Pointcut注解声明切点 @Pointcut("@annotation(jack.ch1.aop.Action)") public void annotationPointCut(){} /** * 使用注解式的拦截 * @After声明一个建言,并使用 @Pointcut定义的切点 * @param joinPoint */ @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.name()); } /** * @Before注解声明一个建言,此建言直接使用拦截规则作为参数 * @param joinPoint */ @Before("execution(* jack..ch1.aop.DemoMethodService.*(..))") public void before(JoinPoint joinPoint){ MethodSignature signature = (MethodSignature) joinPoint.getSignature(); Method method =signature.getMethod(); System.out.println("方法规则拦截: "+method.getName()); } }

   配置类:

   

package jack.ch1.aop; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.EnableAspectJAutoProxy; /** * Created by wj on 2017/7/8. * @EnableAspectJAutoProxy注解开启spring对aspectj的支持 */ @Configuration @ComponentScan("jack.ch1.aop") @EnableAspectJAutoProxy public class AopConfig { }

  测试代码如下:

  

package jack.ch1.aop; import jack.ch1.bean.JavaConfig; import jack.ch1.bean.UseFunctionService; import org.springframework.context.annotation.AnnotationConfigApplicationContext; /** * Created by jack on 2017/6/25. */ public class MainTest3 { public static void main(String [] args){ //AnnotationConfigApplicationContext作为spring容器,接受一个配置类作为参数 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AopConfig.class); DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class); DemoMethodService demoMethodService = context.getBean(DemoMethodService.class); demoAnnotationService.add(); demoMethodService.add(); context.close(); } }

  测试结果如下:

  

   下面是完整的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>com.jack</groupId> <artifactId>springstudy1</artifactId> <version>1.0-SNAPSHOT</version> <!--定义属性--> <properties> <java.version>1.8</java.version> </properties> <!--添加依赖--> <dependencies> <!--添加spring框架依赖包--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.6.RELEASE</version> </dependency> <!--spring的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> <!--增加commons-io可简化文件相关操作--> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.3</version> </dependency> </dependencies> <!--添加插件--> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>${java.version}</source> <target>${java.version}</target> </configuration> </plugin> </plugins> </build> </project>

转载请注明原文地址: https://www.6miu.com/read-8402.html

最新回复(0)