Spring 面向切面(AOP)编程,注解
2017-01-20 16:23
676 查看
引入AOP命名空间
两种方式:
1. 基于XML配置方式进行AOP开发。
2. 基于注解方式进行AOP开发。
再xml 下的切面编程
两种方式:
1. 基于XML配置方式进行AOP开发。
2. 基于注解方式进行AOP开发。
/** * Created by nanji on 2017/1/19. */ public class JDKProxyFactory implements InvocationHandler{ private Object targetObject; public Object createProxyIntance(Object targetObject){ this.targetObject=targetObject; return Proxy.newProxyInstance(this.targetObject.getClass().getClassLoader(),this.targetObject.getClass().getInterfaces(),this); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { //环绕通知 Object result=null; PersonServiceBean bean=(PersonServiceBean)this.targetObject; if(bean.getUser()!=null){ //....advice() -->前置通知 try { result=method.invoke(targetObject,args); // afteradvice () -->后置通知 } catch (RuntimeException e){ //exceptionAdvice() -->例外通知 }finally { //finallyadvice() --嘴终通知 } } return result; } }
public class PersonServiceBean implements PersonService{ @Override public void save(String name) { System.out.println("xxxxxxxx"); throw new RuntimeException("wo is exception"); } @Override public void update(String name, Integer id) { System.out.println("我是save()方法"); } @Override public String getPersonName(Integer id) { return "xxxxsss "; } }
@Aspect public class MyInterceptor { @Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))") private void anyMethod(){ System.out.println("anyMethod....."); } @Before("anyMethod() && args(name)") public void doAccessCheck(String name){ System.out.println("my is before "+name); } @AfterReturning(pointcut = "anyMethod()",returning = "result") public void doAfterReturning(String result){ System.out.println("houzhitongzhi"+result); } @After("anyMethod()") public void doAfterCheck(){ System.out.println("my is after"); } @AfterThrowing(pointcut = "anyMethod()",throwing = "e") public void doAfterThrowing(Exception e ){ // throw new RuntimeException("wo is exception "); System.out.println("exception informing "+e); } @Around("anyMethod()") public Object doBasicProfiling(ProceedingJoinPoint pjp)throws Throwable{ System.out.println("into method"); Object result=pjp.proceed(); System.out.println("out method"); return result; } }
public class SpringAOPTest { @Before public void before() throws Exception { } @After public void after() throws Exception { } @Test public void interceptorTest() { ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml"); PersonService personService=(PersonService)cxt.getBean("personService"); personService.save("xxxxxx"); //personService.getPersonName(2); }
再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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" > <aop:aspectj-autoproxy/> <bean id="aspetbean" class="cn.itcast.service.MyInterceptor"/> <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean"/> <aop:config> <aop:aspect id="asp" ref="aspetbean"> <aop:pointcut id="mycut" expression="execution(!void cn.itcast.service.impl.PersonServiceBean.*(..))" /> <aop:before method="doAccessCheck" pointcut-ref="mycut"/> <aop:after-returning method="doAfterReturning" pointcut-ref="mycut"/> <aop:after-throwing method="doAfterThrowing" pointcut-ref="mycut"/> <aop:after method="doAfterCheck" pointcut-ref="mycut"/> <aop:around method="anyMethod" pointcut-ref="mycut"/> </aop:aspect> </aop:config> </beans>
import cn.itcast.service.PersonService; /** * Created by nanji on 2017/1/20. */ public class PersonServiceBean implements PersonService{ @Override public void save(String name) { System.out.println("xxxxxxxx"); } @Override public void update(String name, Integer id) { System.out.println("我是save()方法"); } @Override public String getPersonName(Integer id) { return "xxxxsss "; } }
/** * Created by nanji on 2017/1/20. */ //@Aspect public class MyInterceptor { // @Pointcut("execution (* cn.itcast.service.impl.PersonServiceBean.*(..))") private void anyMethod(){ System.out.println("anyMethod....."); } // @Before("anyMethod() && args(name)") public void doAccessCheck(){ System.out.println("my is before "); } // @AfterReturning(pointcut = "anyMethod()",returning = "result") public void doAfterReturning(){ System.out.println("houzhitongzhi"); } // @After("anyMethod()") public void doAfterCheck(){ System.out.println("my is after"); } // @AfterThrowing(pointcut = "anyMethod()",throwing = "e") public void doAfterThrowing(){ // throw new RuntimeException("wo is exception "); System.out.println("exception informing "); } // @Around("anyMethod()") public Object doBasicProfiling(ProceedingJoinPoint pjp)throws Throwable{ System.out.println("into method"); Object result=pjp.proceed(); System.out.println("out method"); return result; } }
相关文章推荐
- 微服务框架Spring Cloud介绍 Part2: Spring Cloud与微服务
- springmvc实现url路由功能
- spring aop 无法代理父类方法 的解决方法
- Spring MVC过滤器-超类 GenericFilterBean
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- Spring和ThreadLocal
- Spring Boot 开发微服务
- Spring AOP动态代理-切面
- 肯特·贝克:改变人生的代码整理魔法
- 通过构建一个简单的掷骰子游戏去学习怎么用 Python 编程
- 你应该学习哪种编程语言?
- 使用MVC微信支付返回get_brand_wcpay_request:fail
- Spring整合Quartz(JobDetailBean方式)
- Spring整合Quartz(JobDetailBean方式)
- [转]我们需要一种其他人能使用的编程语言
- DB2编程序技巧(1)