Spring面向切面的简单示例(基于XML文件配置)

来源:互联网 发布:推荐淘宝男装店铺 编辑:程序博客网 时间:2024/06/07 00:09

1.  建立工程,导入Spring AOP所需的jar包。


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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://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);}@Testpublic void testAop() throws Exception {helloService.sayHello("sugar");}}

6. 结果输出:

Around Advice 在目标方法之前织入Before AdviceHello sugarAround Advice 在目标方法之后织入After AdviceAfterReturning Advice目标方法的返回值:null


原创粉丝点击