Java 面向切面编程AOP

一:背景
Spring的AOP的存在目的是为了解耦。AOP可以让一组类共享相同的行为。在OOP中只能通过继承类和实现接口,来是代码的耦合度增强,且类继承只能为单继承,阻碍更多行为添加到一组类上,AOP弥补了OPP的不足。

二:概述  Spring支持AspectJ的注解方式切面编程

1.使用@Aspect 声明一个切面。

2.使用@After,@Before,@Around 定义建言(advice),可直接将拦截规则(切点)作为参数。

3.其中@After,@Before,@Around参数的拦截规则为切点(PointCut) ,为了使切点复用,可使用@PointCut 专门定义拦截规则,然后在 @After,@Before,@Around的参数中调用。

4.其中符合条件的每一个被拦截处为连接点(JoinPoint)

三:代码实例

1.pom.xml


点击(此处)折叠或打开

  1. <dependency>
  2.             <groupId>org.springframework</groupId>
  3.             <artifactId>spring-core</artifactId>
  4.         </dependency>
  5.         <dependency>
  6.             <groupId>org.springframework</groupId>
  7.             <artifactId>spring-beans</artifactId>
  8.         </dependency>

  9.         <dependency>
  10.             <groupId>org.springframework</groupId>
  11.             <artifactId>spring-context</artifactId>
  12.         </dependency>

  13.         <dependency>
  14.             <groupId>org.springframework</groupId>
  15.             <artifactId>spring-aop</artifactId>
  16.         </dependency>

  17.         <dependency>
  18.             <groupId>org.aspectj</groupId>
  19.             <artifactId>aspectjrt</artifactId>
  20.         </dependency>

  21.         <dependency>
  22.             <groupId>org.aspectj</groupId>
  23.             <artifactId>aspectjweaver</artifactId>
  24.         </dependency>
2.拦截规则的注解


点击(此处)折叠或打开

  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface Action {
  5.     String name();
  6. }
2.注解的被拦截类


点击(此处)折叠或打开

  1. @Service
  2. public class DemoAnnotationService {

  3.     @Action(name = "注解式拦截的add操作")
  4.     public void add() {
  5.        System.out.println("======DemoAnnotationService方法add()=========");
  6.     }
  7. }
3.方法规则被拦截类


点击(此处)折叠或打开

  1. @Service
  2. public class DemoMethodService {
  3.     public String add() throws Exception{
  4.         System.out.println("======DemoMethodService方法add()=========");
  5.         int i=100/0;
  6.         return "SUCCESS";
  7.     }
  8. }

4.编写切面


点击(此处)折叠或打开

  1. @Aspect
  2. @Component
  3. public class LogAspect {

  4.     @Pointcut("@annotation(com.gemdale.gmap.spring.boot.demo.Action)")
  5.     public void annotationPointCut() {
  6.     }

  7.     @After("annotationPointCut()")
  8.     public void after(JoinPoint joinPoint) {
  9.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  10.         Method method = signature.getMethod();
  11.         Action action = method.getAnnotation(Action.class);

  12.         System.out.println("注解式拦截 " + action.name());
  13.     }

  14.     @Before("execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))")
  15.     public void methodBefore(JoinPoint joinPoint) {
  16.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  17.         Method method = signature.getMethod();
  18.         System.out.println("before方法规则式拦截 " + method.getName());
  19.     }

  20.     @After("execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))")
  21.     public void methodAfter(JoinPoint joinPoint) {
  22.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  23.         Method method = signature.getMethod();
  24.         System.out.println("after方法规则式拦截 " + method.getName());
  25.     }

  26.     @AfterReturning(pointcut = "execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))", returning = "result")
  27.     public void methodAfterResult(JoinPoint joinPoint, Object result) {
  28.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  29.         Method method = signature.getMethod();
  30.         System.out.println("after result方法规则式拦截 " + method.getName() + "result=" + result);
  31.     }

  32.     @AfterThrowing(pointcut = "execution(* com.gemdale.gmap.spring.boot.demo.DemoMethodService.*(..))", throwing = "e")
  33.     public void methodAfterException(JoinPoint joinPoint, Exception e) {
  34.         MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  35.         Method method = signature.getMethod();
  36.         System.out.println("after exception方法规则式拦截 " + method.getName() + " e=" + e.getMessage());
  37.     }

  38. }
5.配置类


点击(此处)折叠或打开

  1. @Configuration
  2. @ComponentScan("com.gemdale")
  3. @EnableAspectJAutoProxy
  4. public class AppliactionConfig {

  5. }

6.执行类


点击(此处)折叠或打开

  1. public class Start {
  2.     public static void main(String[] args) throws Exception{

  3.         AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(
  4.                 AppliactionConfig.class);
  5.         // UseFunctionService
  6.         // useFunctionService=configApplicationContext.getBean(UseFunctionService.class);
  7.         // System.out.println(useFunctionService.sayHello("Gengchong"));
  8.         DemoAnnotationService demoAnnotationService = configApplicationContext.getBean(DemoAnnotationService.class);
  9.         DemoMethodService demoMethodService = configApplicationContext.getBean(DemoMethodService.class);

  10.         demoAnnotationService.add();

  11.         demoMethodService.add();

  12.         configApplicationContext.close();
  13.     }
  14. }


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28624388/viewspace-2133594/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/28624388/viewspace-2133594/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值