Spring AOP的配置可以采用两种方式:XML和Annotation(需要JDK 5.0+)。
1.以XML方式配置:
业务逻辑类BusinessServiceImpl .java:
package org.wh.tech.spring.aop;
import org.springframework.beans.factory.annotation.Autowired;
import org.wh.tech.spring.sample.SimpleBean;
/**
* @Author:wh007
*
* @Date:2011-7-4
*
* @TODO:业务逻辑类
*/
public class BusinessServiceImpl implements BusinessService {
@Autowired
private SimpleBean simpleBean;
@Override
public void addProcess(String processName) {
simpleBean.print(processName + " is added !") ;
}
@Override
public boolean updateProcess(String processName) {
simpleBean.print(processName + " is updated !") ;
return true;
}
}
XML切面类XMLExampleAspect.java:
package org.wh.tech.spring.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
/**
* @Author:wh007
*
* @Date:2011-7-4
*
* @TODO:XML配置切面类
*/
public class XMLExampleAspect {
/*
* Around advice: 执行在join point这个方法执行之前与之后的advice。
*/
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
long time = System.currentTimeMillis();
Object retVal = pjp.proceed();
time = System.currentTimeMillis() - time;
System.out.println("process time: " + time + " ms");
return retVal;
}
/*
* After advice: 执行在join point这个方法执行之后的advice。
*/
public void doAfter(JoinPoint jp) {
System.out.println("log Ending method: "
+ jp.getTarget().getClass().getName() + "."
+ jp.getSignature().getName());
}
/*
* Before advice: 执行在join point这个方法执行之前的advice。
*/
public void doBefore(JoinPoint jp) {
System.out.println("log Begining method: "
+ jp.getTarget().getClass().getName() + "."
+ jp.getSignature().getName());
}
/*
* Throwing advice: 执行在join point这个方法抛出异常之后的advice。
*/
public void doThrowing(JoinPoint jp, Throwable ex) {
System.out.println("method " + jp.getTarget().getClass().getName()
+ "." + jp.getSignature().getName() + " throw exception");
System.out.println(ex.getMessage());
}
}
测试类TestXmlAop.java
package org.wh.tech.spring.aop.test;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.wh.tech.spring.aop.BusinessService;
/**
* @Author:wh007
*
* @Date:2011-7-4
*
* @TODO:以XML方式配置Aop测试
*/
@ContextConfiguration(locations={"classpath:applicationContext-*.xml"})
public class TestXmlAop extends AbstractJUnit4SpringContextTests{
@Autowired
private BusinessService businessService;
@Test
public void testAddProcess(){
businessService.addProcess("1st,");
}
}
applicationContext.xml需要配置:
<!-- 以XML方式配置Aop -->
<aop:config>
<aop:aspect id="xmlAspect" ref="xmlAspectBean">
<aop:pointcut id="businessService"
expression="execution(* org.wh.tech.spring.aop.*.*(..))" />
<aop:after pointcut-ref="businessService" method="doAfter" />
<aop:before pointcut-ref="businessService" method="doBefore" />
<aop:around pointcut-ref="businessService" method="doAround" />
</aop:aspect>
</aop:config>
<bean id="xmlAspectBean" class="org.wh.tech.spring.aop.XMLExampleAspect" />
<bean id="businessServiceImpl" class="org.wh.tech.spring.aop.BusinessServiceImpl"></bean>
2.以Annotation方式配置:
切面类AnnotationExampleAspect.java:
package org.wh.tech.spring.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.wh.tech.spring.sample.SimpleBean;
/**
* @Author:wh007
*
* @Date:2011-7-4
*
* @TODO:通过annotation注解配置aop
*/
@Aspect
public class AnnotationExampleAspect {
@Autowired
private SimpleBean simpleBean;
/*
* 定义pointcunt
* */
@Pointcut("execution(* org.wh.tech.spring.aop.*.*(..))")
public void aPointcut() {
}
/*
* After-Returning advice: 执行在join point这个方法抛出异常之后的advice。
*/
@AfterReturning(pointcut = "aPointcut()", returning="r")
public void doAfterReturning(boolean r) {
if(r){
simpleBean.print("return is :" + r);
}else{
simpleBean.print("r is null");
}
}
}
测试类TestAnnotationAop.java:
package org.wh.tech.spring.aop.test;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.wh.tech.spring.aop.BusinessService;
/**
* @Author:wh007
*
* @Date:2011-7-4
*
* @TODO:以Annotation方式配置Aop测试
*/
@ContextConfiguration(locations={"classpath:applicationContext-*.xml"})
public class TestAnnotationAop extends AbstractJUnit4SpringContextTests {
@Autowired
private BusinessService businessService;
@Test
public void testUpdateProcess(){
businessService.updateProcess("update,");
}
}
applicationContext.xml只需添加:
<!-- 以annotation方式配置Aop -->
<aop:aspectj-autoproxy />
<bean id="annotationExampleAspect" class="org.wh.tech.spring.aop.AnnotationExampleAspect" />
3.小结:
两种方式各有优势:
1)XML方式灵活性更高,一旦出现配置变更,只需要修改配置文件而不需要程序进行改动,但配置较为复杂。
2)Annotation方式配置较为简洁,但变更成本较高,需要对程序进行修改。
分享到:
相关推荐
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
Spring之AOP配置文件详解.txt Spring之AOP配置文件详解.txt 帮助你更快张握Java的学习!
开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...
使用Spring配置文件实现AOP
赠送jar包:spring-aop-5.2.0.RELEASE.jar; 赠送原API文档:spring-aop-5.2.0.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.2.0.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.2.0.RELEASE.pom;...
Spring AOP配置的实例,最基本的Spring AOP配置
最近使用了springAOP编程,文档里面包含了springAOP的代码示例及详细注释说明,使用的是注解配置模式
spring中AOP中标签加载通知,spring中AOP中标签加载通知spring中AOP中标签加载通知spring中AOP中标签加载通知,spring中AOP中标签加载通知
springIOC与AOP配置
spring的aop配置,很全的,配置在方法前方法后,都可以的
赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....
赠送jar包:spring-aop-5.0.8.RELEASE.jar; 赠送原API文档:spring-aop-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.8.RELEASE.pom;...
aopalliance.jar、spring-aop-4.1.6.RELEASE.jar、spring-aspects-4.1.6.RELEASE.jar
SpringAop面向切面编程
spring-aop-2.0.8.jar
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
赠送jar包:spring-aop-5.3.12.jar; 赠送原API文档:spring-aop-5.3.12-javadoc.jar; 赠送源代码:spring-aop-5.3.12-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.3.12.pom; 包含翻译后的API文档:spring...
Spring AOP:通过配置管理特性,Spring AOP 模块直接将面向方面的编程功能集成到了 Spring 框架中。所以,可以很容易地使 Spring 框架管理的任何对象支持 AOP。Spring AOP 模块为基于 Spring 的应用程序中的对象提供...
spring3 spring-aop chm 类图
Spring_AOP_学习小结 Spring_AOP_学习小结 Spring_AOP_学习小结 Spring_AOP_学习小结