前置增强和其他几种增强和增强顾问等

来源:互联网 发布:桌面主题软件哪个好 编辑:程序博客网 时间:2024/06/08 18:28
//以一个增强的配置文件为例  <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:p="http://www.springframework.org/schema/p"       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.xsd">    <!--目标对象-->    <bean id="someService" class="cn.happy.AfterAdvicce.SomeSevice"></bean>    <!--通知-->    <bean id="afterAdvice" class="cn.happy.AfterAdvicce.MyBeforeAdvice"></bean>    <!--aop-->    <bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean">        <property name="target" ref="someService"></property>        <property name="interceptorNames" value="afterAdvice"></property>    </bean></beans>

1.前置增强

//创建一个接口类public interface ISome { public void One(); public void Serend(); public void Thire();}//前置增强实现的方法MethodBeforeAdvicepublic class MyBeforeAdvice implements MethodBeforeAdvice{ public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("==================before=================="); }}//创建接口实现类public class SomeSevice implements ISome{ public void One() { System.out.println("One=================="); } public void Serend() { System.out.println("Send=============="); } public void Thire() { System.out.println("Third============"); }} @org.junit.Test //aop前置增强 public void beforeAdvice(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextMyBeforeAdvice.xml"); ISome servic= (ISome)ctx.getBean("someProxy"); servic.One(); servic.Serend(); servic.Thire(); }

2.后置增强

//创建一个接口类public interface ISome { public void One(); public String Serend(); public void Thire();}//创建接口实现类public class SomeSevice implements ISome { public void One() { System.out.println("One=================="); } public String Serend() { System.out.println("Send=============="); return "返回abc"; } public void Thire() { System.out.println("Third============"); }}//后置增强的一个方法AfterReturningAdvicepublic class MyBeforeAdvice implements AfterReturningAdvice{ public void afterReturning(Object returnValue, Method method, Object[] objects, Object target) throws Throwable { System.out.println("============After===========returnValue=" ); if(returnValue!=null){ System.out.println(returnValue.toString().toUpperCase()); } }} @org.junit.Test //aop后置增强 public void afterAdvice(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextMyAfterAdvice.xml"); cn.happy.AfterAdvicce.ISome servic= (cn.happy.AfterAdvicce.ISome)ctx.getBean("someProxy"); servic.One(); String serend = servic.Serend(); System.out.println(serend); servic.Thire(); }//输出台结果One==============================After===========returnValue=Send==========================After===========returnValue=返回ABC返回abcThird========================After===========returnValue=

3.aop异常增强

//创建一个接口类public interface ISomeService { public void doSome() throws UserException; public String doSecond(); public void doThird();}//异常增强的一个方法ThrowsAdvicepublic class MyThrowsAdvice implements ThrowsAdvice { public void afterThrowing(UserException ex){ System.out.println("我是增强中的错误提示"+ex.getMessage()); }}//目标对象public class SomeService implements ISomeService { public void doSome() throws UserException { System.out.println("希望大家将来越来越好"); throw new UserException("错误了,呵呵!!!!!"); } public String doSecond() { System.out.println("希望大家将来越来越好2"); return "我是返回值abc"; } public void doThird() { System.out.println("希望大家将来越来越好3"); }} @org.junit.Test //aop异常增强 public void throwingAdvice() { //异常:01.运行时异常 02.检查异常 //01.运行时异常 虚拟机直接down掉 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextThrowing.xml"); ISomeService servic= (ISomeService)ctx.getBean("someProxy"); try { servic.doSome(); } catch (UserException e) { e.printStackTrace(); } }//输出台结果cn.happy.Throwing.UserException: 错误了,呵呵!!!!!希望大家将来薪资我是增强中的错误提示错误了,呵呵!!!!!

4.aop环绕增强

//环绕增强的一个方法MethodInterceptorpublic class MyBeforeAdvice implements MethodInterceptor{ public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("============之前==========="); Object proceed = methodInvocation.proceed(); String result=null; if(proceed!=null){ result=(String)proceed; result=result.toUpperCase(); } System.out.println("=============之后============"); return result; }} @org.junit.Test //aop环绕增强 public void methodAdvice(){ ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextMethodAdvice.xml"); cn.happy.MenthodAdvice.ISome servic= (cn.happy.MenthodAdvice.ISome)ctx.getBean("someProxy"); servic.One(); String serend = servic.Serend(); System.out.println(serend); servic.Thire(); }//输出结果============之前===========One===============================之后========================之前===========Send===========================之后============返回ABC============之前===========Third=========================之后============

5.增强顾问

//增强顾问的一个方法MethodBeforeAdvicepublic class MyBeforeAdvice implements MethodBeforeAdvice{ public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("==================before=================="); }} @org.junit.Test //增强顾问b public void beforeAdvisor() { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextMyBeforeAdvisor.xml"); cn.happy.BeforeAdvisor.ISome servic= (cn.happy.BeforeAdvisor.ISome)ctx.getBean("someProxy"); servic.One(); servic.Serend(); servic.Thire(); }//输出结果==================before==================One====================================before==================Send================================before==================Third============

6.正则

//定义一个接口public interface ISome { public void One(); public void Serend(); public void Thire();}//接口的实现类public class SomeSevice implements ISome { public void One() { System.out.println("One=================="); } public void Serend() { System.out.println("Send=============="); } public void Thire() { System.out.println("Third============"); }}//MethodBeforeAdvicepublic class MyBeforeAdvice implements MethodBeforeAdvice{ public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("==================before=================="); }}@org.junit.Test //正则 public void registerAdvisor() { ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContextRegisterAdvisor.xml"); cn.happy.RegisterAdvisor.ISome servic= (cn.happy.RegisterAdvisor.ISome)ctx.getBean("someProxy"); servic.One(); servic.Serend(); servic.Thire(); }

原创粉丝点击