Spring的AOP

一 点睛

AOP:面向切面编程,相对于OOP面向对象编程。

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

Spring支持AspectJ的注解式切面编程。

(1)使用@Aspect声明是一个切面。

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

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

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

二 实战

拦截方式:演示基于注解的拦截和基于方法规则的拦截。

项目实现:模拟记录操作的日志系统。

说明:注解式拦截能够很好地控制要拦截的粒度和获得更丰富的信息。

1 添加spring aop支持及Aspectj依赖

<!-- spring aop支持 -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>${spring-framework.version}</version>
</dependency>
<!-- aspectj支持 -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.8.6</version>
</dependency>

2 编写拦截规则注解

package com.wisely.highlight_spring4.ch1.aop;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//注解本身是没有功能的,就和xml一样。
//注解和xml都是一种元数据。
//元数据即解释数据的数据,这就是所谓的配置
//注解的功能来自用这个注解的地方
//该注解用在方法上
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
    //该注解有一个name属性
    String name();
}

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

package com.wisely.highlight_spring4.ch1.aop;

import org.springframework.stereotype.Service;

@Service
public class DemoAnnotationService {
    @Action(name="注解式拦截的add操作")
    public void add(){}
   
}

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

package com.wisely.highlight_spring4.ch1.aop;

import org.springframework.stereotype.Service;

@Service
public class DemoMethodService {
    public void add(){}
}

5 编写切面

package com.wisely.highlight_spring4.ch1.aop;

import java.lang.reflect.Method;

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;


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

6 配置类

package com.wisely.highlight_spring4.ch1.aop;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch1.aop")
@EnableAspectJAutoProxy //使用该注解开启Spring对AspectJ的支持
public class AopConfig {
}

7 主类

package com.wisely.highlight_spring4.ch1.aop;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
    public static void main(String[] args) {
         AnnotationConfigApplicationContext context =
                    new AnnotationConfigApplicationContext(AopConfig.class); //1
        
         DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
        
         DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);
        
         demoAnnotationService.add();
        
         demoMethodService.add();
        
         context.close();
    }

}

三 测试

注解式拦截 注解式拦截的add操作

方法规则式拦截,add

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值