首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

spring 配置文件兑现AOP-学习笔记

2012-09-15 
spring 配置文件实现AOP-学习笔记?Spring 对AOP的支持I:Aspect默认情况下不用实现接口,但对于目标对象,在

spring 配置文件实现AOP-学习笔记

?

Spring 对AOP的支持I:

Aspect默认情况下不用实现接口,但对于目标对象,在默认的情况下必须实现接口 如果没有实现接口必须引入CGLIB库,我们可以通过Advice中添加一个JointPoint参数,这个值会由spring自动传入,从JointPoint中可以取得参数值,方法名等等。

Spring对AOP的支持II:

1、如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP。

2、如果目标对象实现了接口,可以强制使用CGLIB实现AOP。

3、如果目标对象没有实现接口,必须采用CGLIB库,spring会自动在JDK动态代理和CGLIB之间转换

在实现接口的情况下, 如果强制使用CGLIB实现AOP?

添加CGLIB库,SPRING_HOME/cglib/*.jar

在spring配置文件中添加<aop:aspectj-autoproxy proxy-target-encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"><!-- 打开配置项,这个配置项是对@Aspectj这个注解进行支持 注解本身是不能干活的,注解之所以能干活是因为后面有处理器对其进行处理这个配置相当于我们将要使用的@Aspectj注解提供了解析的功能 --><aop:aspectj-autoproxy /><context:component-scan base-package="com.sample.*"/><bean id="apectBean" ref="apectBean"><aop:pointcut id="mycut" expression="execution (void com.sample.service.impl.PersonServiceBean*.*(String))"/><aop:before pointcut-ref="mycut" method="doAccessCheck" arg-names="name" /><aop:after pointcut-ref="mycut" method="doAfter"/><aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/><aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/><aop:around pointcut-ref="mycut" method="doBasicProfiling"/></aop:aspect></aop:config><aop:config><aop:aspect id="myAOP" ref="check"><aop:pointcut id="target" expression="execution(* com.sample.service.Common.execute2(..))"/><aop:before method="checkValidator" pointcut-ref="target"/><aop:after method="addLog" pointcut-ref="target"/></aop:aspect></aop:config></beans>

?

package com.sample.service;import org.aspectj.lang.JoinPoint;import org.springframework.stereotype.Service;@Service("check")public class Check {public void checkValidator() {System.out.println("------------------验证合法性----------------");}public void addLog(JoinPoint jp) {System.out.println("------------------添加日志----------------");Object[] objs = jp.getArgs();for (Object o : objs) {Stock stock = (Stock)o;System.out.println(stock.getLId());}System.out.println("========checkSecurity=="+jp.getSignature().getName());//这个是获得方法名}}
?

?

热点排行