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

	
//以一个增强的配置文件为例  
<?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(); } //前置增强实现的方法MethodBeforeAdvice public 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============"); } } //后置增强的一个方法AfterReturningAdvice public 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 返回abc Third============ ============After===========returnValue=

3.aop异常增强

//创建一个接口类 public interface ISomeService { public void doSome() throws UserException; public String doSecond(); public void doThird(); } //异常增强的一个方法ThrowsAdvice public 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环绕增强

//环绕增强的一个方法MethodInterceptor public 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.增强顾问

//增强顾问的一个方法MethodBeforeAdvice public 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============"); } } //MethodBeforeAdvice public 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(); }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值