码迷,mamicode.com
首页 > 编程语言 > 详细

Spring配置AOP实现定义切入点和织入增强

时间:2014-12-22 19:21:18      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

经过AOP的配置后,可以切入日志功能、访问切入、事务管理、性能监测等功能。

首先实现这个织入增强需要的jar包,除了常用的

com.springsource.org.apache.commons.logging-1.1.1.jar,

com.springsource.org.apache.log4j-1.2.15.jar,

spring-beans-3.2.0.RELEASE.jar,

spring-context-3.2.0.RELEASE.jar,

spring-core-3.2.0.RELEASE.jar,

spring-expression-3.2.0.RELEASE.jar之外还需要

spring-aop-3.2.0.RELEASE.jar,

com.springsource.org.aopalliance-1.0.0.jar,(aop联合jar)

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar(织入方向jar)三个包。

然后我有一个接口

public interface Hello {

public void say();

}

和一个实现类

public class HelloImpl implements Hello{

public void say(){

System.out.println("执行say方法");

}

}

和两个增强类

前置增强:

public class LogBefor implements MethodBeforeAdvice{

@Override

public void before(Method method, Object[] args, Object target)

throws Throwable {

System.out.println(method);

System.out.println(args);

System.out.println(target);

}

}

后置增强:

public class LogAfter implements AfterReturningAdvice{

@Override

public void afterReturning(Object returnValue, Method method,

Object[] args, Object target) throws Throwable {

System.out.println("返回值"+returnValue);

System.out.println("方法:"+method);

System.out.println("参数:"+args);

System.out.println("被代理对象:"+target);

}

}

接下来还需要在Spring配置文件中beans元素中需要添加aop的名称空间,以导入与AOP相关的标签:如:

<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">   

这里比之前的多了:xmlns:aop="http://www.springframework.org/schema/aop"

在xsi:schemaLocation里多了两个

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd;

接下来在Spring配置文件中实现AOP配置,如我的xml

<?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="hello" class="cn.cnti.aop.HelloImpl"></bean>

 

<!-- 增强类 -->

<bean id="before" class="cn.cnti.aop.LogBefor">

</bean>

<bean id="after" class="cn.cnti.aop.LogAfter">

</bean>

 

<aop:config>

<!-- 定义切入点 -->

         <aop:pointcut id="pointcut"

                    expression="execution(public void say())"/>

         <aop:advisor advice-ref="before" pointcut-ref="pointcut"/>

         <aop:advisor advice-ref="after" pointcut-ref="pointcut"/>

   </aop:config>

 </beans> 

如果记不清楚的话,spring-framework-3.2.0.RELEASE-distspring-framework-3.2.0.RELEASEdocsspring-framework-referencehtmlaop.html打开后搜索aop会有模板。

其中execution是切入点指示符,它的括号中是一个切入点表达式,可以配置要切入的方法,切入点表达式支持模糊匹配:

Public * say(entity.User):”*”表示匹配所有类型的返回值

Public void *(entity.User):”*”表示匹配所有方法名。

Public void say(..):”..”表示匹配所有参数个数和类型。

* cn.jnti.service.*.*(..):这个表达式匹配cn.jnti.service包下所有类的所有方法。

* cn.jnti.service..*.*(..):这个表达式匹配cn.jnti.service包及其子包下所有类的所有方法。

等等,还有许多。

最后我们测试一下:

@Test

public void mtest(){

BeanFactory bf=new  ClassPathXmlApplicationContext("appContext.xml");

Hello h = (Hello) bf.getBean("hello");

h.say();

}

 ******************************************************************************************************

如果想实现日志输出的话,在这个原有jar包的基础上需要

cglib-nodep-2.2.3.jarorg.springframework.asm-3.1.1.RELEASE.jar

然后再前置增强和后置增强里各加一个静态常量:

Private static final Logger log=Logger.getLogger(LogBefor.class);

Private static final Logger log=Logger.getLogger(LogAfter.class);

然后在方法里直接用lo.info(“调用”+target6”的”+method.getName()+”方法”+”参数是:”+Arrays.toString(args));就行了。

 

 

Spring配置AOP实现定义切入点和织入增强

标签:

原文地址:http://www.cnblogs.com/345214483-qq/p/4178638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!