Spring AOP介绍

AOP介绍

    AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。OOP引入封装、继承、多态等概念来建立一种对象层次结构,用于模拟公共行为的一个集合。OOP允许开发者定义纵向的关系,但并不适合定义横向的关系,例如日志功能。日志代码往往横向地散布在所有对象层次中,而与它对应的对象的核心功能毫无关系,对于其他类型的代码如安全性、异常处理和透明的持续性也都是如此,这种散布在各处的无关的代码被称为横切(cross cutting),在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用。

AOP技术恰恰相反,它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为"Aspect",即切面。所谓"切面",简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。

使用"横切"技术,AOP把软件系统分为两个部分:核心关注点横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点。横切关注点的一个特点是,他们经常发生在核心关注点的多处,而各处基本相似,比如权限认证、日志、事物。AOP的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。

AOP核心概念

1、横切关注点

对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点

2、切面(aspect)

类是对物体特征的抽象,切面就是对横切关注点的抽象

3、连接点(joinpoint)

被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

4、切入点(pointcut)

对连接点进行拦截的定义

5、通知(advice)

所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

6、目标对象

代理的目标对象

7、织入(weave)

将切面应用到目标对象并导致代理对象创建的过程

8、引入(introduction)

在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

Spring对AOP的支持

    1.默认使用java动态代理来创建AOP代理,这样就可以为任何接口实例创建代理了

    2.当需要代理的类不是代理接口的时候,Spring会切换为使用CGLIB代理

基于Spring AOP的简单实现

public interface HelloWorld {
    void printHelloWorld();
    void doPrint();
}

public class HelloWorldImpl1 implements HelloWorld{
    @Override
    public void printHelloWorld() {
        System.out.println("enter helloworldimpl1.printHelloWorld()");
    }

    @Override
    public void doPrint() {
        System.out.println("enter helloWorldImpl1.doPrint()");
    }
}

public class HelloWorldImpl2 implements HelloWorld{
    @Override
    public void printHelloWorld() {
        System.out.println("enter helloworldimpl2.printHelloWorld()");
    }

    @Override
    public void doPrint() {
        System.out.println("enter helloWorldImpl2.doPrint()");
    }
}

public class TimeHandler {

    public void printTime(){
        System.out.println("currentTime=" + System.currentTimeMillis());
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <bean id="helloWorldImpl1" class="aspectj.start.HelloWorldImpl1"/>
    <bean id="helloWorldImpl2" class="aspectj.start.HelloWorldImpl2"/>
    <bean id="timeHandler" class="aspectj.start.TimeHandler"/>
    <aop:config>
        <aop:aspect id="time" ref="timeHandler">
            <aop:pointcut id="addAllMethod" expression="execution(* aspectj.start.HelloWorld.*(..))"/>
            <aop:before method="printTime" pointcut-ref="addAllMethod"/>
            <aop:after method="printTime" pointcut-ref="addAllMethod"/>
        </aop:aspect>
    </aop:config>
</beans>
public static void main(String[] args){
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("aop.xml");
    HelloWorld hw1 = (HelloWorld) applicationContext.getBean("helloWorldImpl1");
    HelloWorld hw2 = (HelloWorld) applicationContext.getBean("helloWorldImpl2");
    hw1.printHelloWorld();
    System.out.println();
    hw1.doPrint();
    System.out.println();
    hw2.printHelloWorld();
    System.out.println();
    hw2.doPrint();
}

切入点表达式

         execution:用于匹配方法执行的连接点;

         within:用于匹配指定类型内的方法执行;

         this:用于匹配当前AOP代理对象类型的执行方法;注意是AOP代理对象的类型匹配,这样就可能包括引入接口也类型匹配;

         target:用于匹配当前目标对象类型的执行方法;注意是目标对象的类型匹配,这样就不包括引入接口也类型匹配;

         args:用于匹配当前执行的方法传入的参数为指定类型的执行方法;

         @within:用于匹配所以持有指定注解类型内的方法;

         @target:用于匹配当前目标对象类型的执行方法,其中目标对象持有指定的注解;

         @args:用于匹配当前执行的方法传入的参数持有指定注解的执行;

         @annotation:用于匹配当前执行方法持有指定注解的方法;

其中execution 是用的最多的,其格式为:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern)throws-pattern?)
returning type pattern,name pattern, and parameters pattern是必须的.
ret-type-pattern:可以为*表示任何返回值,全路径的类名等.
name-pattern:指定方法名,*代表所以,set*,代表以set开头的所有方法.
parameters pattern:指定方法参数(声明的类型),(..)代表所有参数,(*)代表一个参数,(*,String)代表第一个参数为任何值,第二个为String类型.
举例说明:
任意公共方法的执行:
execution(public * *(..))
任何一个以“set”开始的方法的执行:
execution(* set*(..))
AccountService 接口的任意方法的执行:
execution(* com.xyz.service.AccountService.*(..))
定义在service包里的任意方法的执行:
execution(* com.xyz.service.*.*(..))
定义在service包和所有子包里的任意类的任意方法的执行:
execution(* com.xyz.service..*.*(..))
定义在pointcutexp包和所有子包里的JoinPointObjP2类的任意方法的执行:
execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")
***> 最靠近(..)的为方法名,靠近.*(..))的为类名或者接口名,如上例的JoinPointObjP2.*(..))

pointcutexp包里的任意类.
within(com.test.spring.aop.pointcutexp.*)
pointcutexp包和所有子包里的任意类.
within(com.test.spring.aop.pointcutexp..*)
实现了Intf接口的所有类,如果Intf不是接口,限定Intf单个类.
this(com.test.spring.aop.pointcutexp.Intf)
***> 当一个实现了接口的类被AOP的时候,用getBean方法必须cast为接口类型,不能为该类的类型.

带有@Transactional标注的所有类的任意方法.
@within(org.springframework.transaction.annotation.Transactional)
@target(org.springframework.transaction.annotation.Transactional)
带有@Transactional标注的任意方法.
@annotation(org.springframework.transaction.annotation.Transactional)
***> @within和@target针对类的注解,@annotation是针对方法的注解

参数带有@Transactional标注的方法.
@args(org.springframework.transaction.annotation.Transactional)
参数为String类型(运行是决定)的方法.
args(String)

<aop:config>
	<aop:aspectref="aspectDef">
		<aop:pointcutid="pointcut1"expression="execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))"/>
		<aop:before pointcut-ref="pointcut1" method="beforeAdvice" />
	</aop:aspect>
</aop:config>

@Component
@Aspect
public class AspectDef {
	//@Pointcut("execution(* com.test.spring.aop.pointcutexp..JoinPointObjP2.*(..))")
	//@Pointcut("within(com.test.spring.aop.pointcutexp..*)")
	//@Pointcut("this(com.test.spring.aop.pointcutexp.Intf)")
	//@Pointcut("target(com.test.spring.aop.pointcutexp.Intf)")
	//@Pointcut("@within(org.springframework.transaction.annotation.Transactional)")
	//@Pointcut("@annotation(org.springframework.transaction.annotation.Transactional)")
	@Pointcut("args(String)")
	public void pointcut1() {
	}
	@Before(value = "pointcut1()")
	public void beforeAdvice() {
		System.out.println("pointcut1 @Before...");
	}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值