Spring面向切面的简单示例(基于XML文件配置)
2013-10-14 14:35
676 查看
1. 建立工程,导入Spring AOP所需的jar包。
2. Spring配置文件applicationContext.xml:
3. 定义切面Bean:
4. 目标类:
5. 测试用例:
6. 结果输出:
2. Spring配置文件applicationContext.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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!-- 自动扫描指定包及其子包下的所有Bean类 --> <context:component-scan base-package="com.huey" /> <aop:config> <!-- 定义一个切入点 --> <aop:pointcut id="myPointcut" expression="execution(* com.huey.service.*.*(..))"/> <!-- 定义切面,ref属性指定所引用的普通Bean作为切面Bean --> <aop:aspect id="logAspect" ref="logAspect"> <!-- 定义前置通知 --> <aop:before pointcut-ref="myPointcut" method="doBefore"/> <!-- 定义后置通知 --> <aop:after-returning pointcut-ref="myPointcut" method="doAfterReturning" returning="rvt"/> <!-- 定义异常通知 --> <aop:after-throwing pointcut-ref="myPointcut" method="doAfterThrowing" throwing="ex"/> <!-- 定义最终通知 --> <aop:after pointcut-ref="myPointcut" method="doAfter"/> <!-- 定义环绕通知 --> <aop:around pointcut-ref="myPointcut" method="doAround"/> </aop:aspect> </aop:config> </beans>
3. 定义切面Bean:
package com.huey.aspect; import org.aspectj.lang.ProceedingJoinPoint; import org.springframework.stereotype.Component; /** * 切面类 * @author huey2672 * */ @Component("logAspect") public class LogAspect { /** * 前置通知 */ public void doBefore() { System.out.println("Before Advice"); } /** * 后置通知 */ public void doAfterReturning(Object rvt) { System.out.println("AfterReturning Advice"); System.out.println("目标方法的返回值:" + rvt); } /** * 异常通知 */ public void doAfterThrowing(Throwable ex) { System.out.println("AfterReturning Advice"); System.out.println("目标方法中抛出的异常:" + ex); } /** * 最终通知 */ public void doAfter() { System.out.println("After Advice"); } /** * 环绕通知 */ public Object doAround(ProceedingJoinPoint jp) throws Throwable { System.out.println("Around Advice 在目标方法之前织入"); // 获取目标方法的参数,可以对其进行修改 Object[] args = jp.getArgs(); // 执行目标方法,并得到返回值,可以对其进行修改 Object rvt = jp.proceed(args); System.out.println("Around Advice 在目标方法之后织入"); return rvt; } }
4. 目标类:
package com.huey.service; import org.springframework.stereotype.Component; /** * * @author Huey * */ @Component("helloService") public class HelloService { public void sayHello(String name) { System.out.println("Hello " + name); } }
5. 测试用例:
package com.huey.test; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.huey.service.HelloService; /** * * @author huey2672 * */ public class AopTest { private HelloService helloService; { // 创建Spring容器 ApplicationContext appCtx = new ClassPathXmlApplicationContext("applicationContext.xml"); // 通过容器获得EntityServ的实例 helloService = appCtx.getBean("helloService", HelloService.class); } @Test public void testAop() throws Exception { helloService.sayHello("sugar"); } }
6. 结果输出:
Around Advice 在目标方法之前织入 Before Advice Hello sugar Around Advice 在目标方法之后织入 After Advice AfterReturning Advice 目标方法的返回值:null
相关文章推荐
- java基础数据类型
- 拓扑排序 (java)
- java Collections学习
- Java泛型的一段代码
- java AOE网与关键路径
- java 图的深度优先与广度优先排序
- javap -c命令详解
- java 图的拓扑排序(利用Vector存储)
- struts2升级到2.3.15.1版本
- java四舍五入,保留四位小数
- java学习后续阶段,学习路线的参考
- Spring管理session的一些认识和用法心得
- spring 事务配置五种方式
- Spring3.0的Junit测试
- 使用javamail发送邮件
- java实现大数加法、乘法(BigDecimal)
- Struts2 S标签 数目字格式化成金额输出(保留两位小数)
- spring mvc 上传图片
- Spring Ioc实现机制——简析
- [转]Struts标签库详解