前置增强和其他几种增强和增强顾问等
来源:互联网 发布:桌面主题软件哪个好 编辑:程序博客网 时间: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(); }
阅读全文
0 0
- 前置增强和其他几种增强和增强顾问等
- Spring 顾问 代理 增强
- Sping 顾问代理增强
- Spring 顾问 代理增强
- Sping 顾问代理增强
- AOP-方法顾问增强
- 前置增强的实现
- spring(四)前置增强
- spring中的增强:注解和schema配置几种情况
- springAOP前置增强、后置增强、环绕增强(编程式)
- SpringAOP 前置、后置、环绕增强
- 前置增强 ,后置增强,异常增强,环绕增强 静态代理,动态代理
- 如何增强体质和免疫力
- 优雅降级和渐进增强
- 渐进增强和平稳退化
- CMFCPropertySheet和CMFCPropertyPage增强用法
- 字节码增强和AOP
- 渐进增强和优雅降级
- 顺序容器
- faster-RCNN
- Oil Deposits (dfs)【HUD】-1241
- java-守护进程
- 170801 逆向-RouterScan(5)
- 前置增强和其他几种增强和增强顾问等
- HDU 2563 统计问题 (递推 思维)
- ccf认证画图
- Mysql常用命令详解
- win10安装iis及配置iis
- Linux防火墙的概念与企业中的应用
- 顺序容器
- final数据
- ubnutu32 ftp操作