Spring 的切面编程AOP如何实现 注入切面程序
2014-12-10 18:02
971 查看
首先定义
一个切面类
用以上注解表示该类为切面类
并在该切面类中自定义两个方法,分别是执行前执行后的
spring配置文件中注入该类
然后通过aop配置切面程序
切面拦截处理程序
切面程序
action
配置文件
启动tomcat服务器,访问action
http://localhost:8080/demo.web/aop/test.do
可以看出切面程序已经执行了。
一个切面类
@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:我是切面处理程序}"; } }
action
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>
启动tomcat服务器,访问action
http://localhost:8080/demo.web/aop/test.do
可以看出切面程序已经执行了。
相关文章推荐
- Spring AOP 面向切面编程 常见通知实现(前置,后置,环绕,异常)
- 重新学习之spring第二个程序,配置AOP面向切面编程
- CGLIB实现AOP代理的测试类,与Spring分离,加深对面向切面编程的理解
- spring aop切面编程实现操作日志步骤
- Spring AOP:使用NameMatchMethodPointcutAdvisor实现切面编程
- spring_由注解实现AOP面向切面编程_实现动态代理
- 利用多态,实现一般处理程序(ashx)中的AOP(切面编程)
- idea 实现Spring讲解(Ioc-控制反转)/Aop(面向切面的编程)
- Spring 通过注解方式实现AOP切面编程
- Spring面向切面编程——Spring实现AOP方式——自定义类实现
- Spring面向切面编程——Spring实现AOP方式——通过注解实现
- Microsoft.Practices.Unity实现代码依赖注入、XML依赖注入和AOP切面编程
- SpringAOP使用入门,如何对自定义的注解进行切面编程
- Spring AOP的基本原理及面向切面编程的实现
- spring aop 面向切面编程 如何来做一个强大的日志记录功能
- 黑马程序员--Spring Aop 面向切面编程,实现前置通知
- spring学习笔记7--使用spring进行面向切面的(AOP)编程(1)注解方式实现
- spring AOP面向切面编程的四种实现方式
- spring_由XML实现AOP面向切面编程_实现动态代理
- Spring面向切面编程——Spring实现AOP方式——通过Spring API实现