Spring AOP 学习笔记 【Spring in action】

1、DI:对象与对象之间的解耦;AOP:功能与对象之间的解耦;

2、通知(Advice):定义切面的工作是什么、何时被调用【what、when】

3、切点(Ponitcut):定义切面的功能在何处执行【where】

4、切面(Aspect):通知+切点=切面,定义功能是什么,在何时何地完成其功能【what、when、where】

5、在spring文件中如果使用aop标签,需要在<beans>中加上

          xmlns:aop="http://www.springframework.org/schema/aop"

          xsi:schemaLocation=”http://www.springframework.org/schema/aop  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd”


6、在spring配置文件中定义切面类:与其他bean写法相同

<bean id="behavior" class="com.hqq1007.myspring.aspect.Behavior">

7、在XML中声明切面,都需要定义在<aop:config>中

<aop:config>
    	<aop:pointcut id="Animalbehavior" expression="execution(* com.hqq1007.myspring.inter.Animal.yell())"/>
    	
    	<aop:aspect ref="behavior">
    		<aop:before pointcut-ref="Animalbehavior" method="beforeYell"/>
    		<aop:after pointcut-ref="Animalbehavior" method="afterYell"/>
    		<aop:around pointcut-ref="Animalbehavior" method="aroundYell"/>
    	</aop:aspect>
    		
    	
    </aop:config>

<aop:pointcut>定义切点, 表达式中的 * 表示不关心返回值的类型,后面是全限定类名。注意:类名与*号之间有空格。yell()表示当执行该方法时触发通知。<aop:pointcut>可放在<aop:config>作用域下,这样<aop:config>下的多个切面都可调用同一个切点。

<aop:before>、<aop:around>定义通知,method表示触发时执行切面类的哪个方法,pointcut-ref表示关联哪个切点

<aop:aspect>定义切面。

通知有5个类型:<aop:before>、<aop:after>、<aop:after-returning/>、<aop:after-throwing/>、<aop:around>。分别表示调用前,调用后,成功执行后,抛出异常后以及调用前和调用后执行


8、为通知传递参数(xml方式)

切点中做如下修改:

<aop:pointcut id="Animalbehavior" expression="execution(* com.hqq1007.myspring.inter.Animal.yell(String,String)) and args(str1,str2)"/>
在方法后加入参数类型,在表达式中加入参数列表args。若有多个参数,用逗号分隔

通知中做如下修改:

<aop:before pointcut-ref="Animalbehavior" method="favoriteFood" arg-names="str1,str2"/>
加上arg-names属性,【注意】通知中的参数名应该与切点中的参数名一致


9、注解切面

需要引入jar包:aspectj-1.6.11.jar。【应该与jdk版本一致,如版本差距大可能会出现问题】

在spring配置文件中加入:<aop:aspectj-autoproxy/>

@Aspect
public class Behavior {

	@Pointcut("execution(* com.hqq1007.myspring.inter.Animal.yell())")
	//@Pointcut("execution(* com.hqq1007.myspring.inter.Animal.yell2(String,String)) && args(str1,str2)")
	
	/*定义切点*/
	public void dogyell() {
		
	}
	
	/*public void dogyell2(String str1,String str2) {
		
	}*/
	
	/*@Before("dogyell()")*/
	public void beforeYell() {
		System.out.println("Shark its tail");
	}
	
	/*@AfterReturning("dogyell()")*/
	public void afterYell() {
		System.out.println("Spit out its tongue");
	}
	
	@Around("dogyell()")
	public void aroundYell(ProceedingJoinPoint joinpoint) throws Throwable {
		System.out.println("Before!Shark its tail");
		joinpoint.proceed();
		System.out.println("After!Spit out its tongue");
		System.out.println("Start again!");
		joinpoint.proceed();
		System.out.println("Over again!");
	}
	
	/*@Around("dogyell2(str1,str2)")
	public void favoriteFood(String str1,String str2) {
		System.out.println("My Favorite food is "+ str1 + " and " + str2);
	}*/
}

用@Aspect来声明这是一个切面类

用@Pointcut来声明切点,用法和xml方式大致相同

用@Before、@Around等来声明通知

【注意dogyell()方法,其实只是一个标识,供@Pointcut注解依附,可以为空,作为参数赋给通知注解,但是不能不写!】


10、注解传参

例子参考上面代码,需要注意的是:切点(@Pointcut)、切点方法(dogyell2)、通知(@Around)、切面方法(favoriteFood)中的参数名字应该一致,否则会报错


11、Around通知

将ProceedingJoinPoint对象作为参数。它可以在通知中调用被通知的方法:ProceedingJoinPoint.proceed();

代码参考上面例子中的aroundYell();

proceed方法应该至少调用一次,否则会中断操作;proceed方法可以调用一次或者多次。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值