首先定义
一个切面类
@Aspect @Component
并在该切面类中自定义两个方法,分别是执行前执行后的
public void before(JoinPoint jp) { …… } public void after(JoinPoint jp) { …… }
spring配置文件中注入该类
然后通过aop配置切面程序
<aop:config> <aop:pointcut id="beforeMethod" expression="execution(public * com.DemoClass.*(..))" /> <aop:aspect id="myAspect" ref="acpectInterceptor"> <aop:pointcut id="afterMethod" expression="execution(public * com.DemoClass.*(..))" /> <aop:before method="before" pointcut-ref="beforeMethod"/> <aop:after-returning method="after" pointcut-ref="afterMethod"/> </aop:aspect> </aop:config>
切面拦截处理程序
package com.tree.common; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; @Aspect @Component public class MyAspectInterceptor { public void before(JoinPoint jp) { Object[] args = jp.getArgs(); for(int i=0;i<args.length;i++) { System.out.println("before:"+args[i].getClass().getName()); } System.out.println("我是在切面类方法[前]执行的"); } public void after(JoinPoint jp) { Object[] args = jp.getArgs(); for(int i=0;i<args.length;i++) { System.out.println("after:"+args[i].getClass().getName()); } System.out.println("我是在切面类方法[后]执行的"); } }
package com.tree.demo.aop; import org.springframework.stereotype.Service; @Service public class AspectProcessor { public String aspectBizProcess() { return "{result:我是切面处理程序}"; } }
package com.tree.aop.action; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.tree.common.Constants; import com.tree.demo.aop.AspectProcessor; @Controller @RequestMapping(value="/aop") public class TestAopAction { private static Log log = LogFactory.getLog(Constants.LOGFILE); private AspectProcessor ap; public void setAp(AspectProcessor ap) { this.ap = ap; } @RequestMapping(value="/test",produces = "application/json") @ResponseBody public String testAop() { log.info("进入aoptest Action"); return ap.aspectBizProcess(); } }
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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" default-autowire="byName"> <aop:config> <aop:pointcut id="beforeMethod" expression="execution(public * com.tree.demo.aop.AspectProcessor.*(..))" /> <aop:aspect id="myAspectProcessor" ref="myAspect"> <aop:pointcut id="afterMethod" expression="execution(public * com.tree.demo.aop.AspectProcessor.*(..))" /> <aop:before method="before" pointcut-ref="beforeMethod"/> <aop:after-returning method="after" pointcut-ref="afterMethod"/> </aop:aspect> </aop:config> </beans>
http://localhost:8080/demo.web/aop/test.do
可以看出切面程序已经执行了。
原文地址:http://blog.csdn.net/simonchi/article/details/39927853