Spring AOP配置 动态代理
来源:互联网 发布:淘宝客服网上操作流程 编辑:程序博客网 时间:2024/05/21 14:41
ProxyInter借口:
package com.aspect;public interface ProxyInter {//租房public void rent();//退房public void cancelHouse();}
Owner类实现ProxyInter接口
package com.aspect;public class Owner implements ProxyInter {public void rent() {System.out.println("租房");}public void cancelHouse(){System.out.println("退房");}}
代理类Proxy也实现ProxyInter接口
package com.aspect;public class Proxy implements ProxyInter {public void rent() {ProxyInter proxy = new Owner();proxy.rent();}public void cancelHouse(){ProxyInter proxy = new Owner();proxy.cancelHouse();}}
客户Client
package com.aspect;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Client {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//给ProxyInter接口的所有方法+通知ProxyInter proxy = (ProxyInter)context.getBean("proxyFactoryBean");//只给rent方法+通知//ProxyInter proxy = (ProxyInter)context.getBean("owner");proxy.rent();//proxy.cancelHouse();}}
环绕通知SurroundAdvice
package com.aspect;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class SurroundAdvice implements MethodInterceptor{public Object invoke(MethodInvocation invocation) throws Throwable {System.out.println("欢迎您,请喝茶");Object object = invocation.proceed();System.out.println("加钱");return object;}}
前置通知BeforeAdvice
package com.aspect;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class BeforeAdvice implements MethodBeforeAdvice{public void before(Method arg0, Object[] arg1, Object arg2)throws Throwable {System.out.println("租房之前");}}
后置通知AfterAdvice
package com.aspect;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;public class AfterAdvice implements AfterReturningAdvice{public void afterReturning(Object arg0, Method arg1, Object[] arg2,Object arg3) throws Throwable {System.out.println("租房之后");}}
web.xml如下
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"><context-param><param-name>contextConfigLoaction</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list></web-app>
applicationContext.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" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="surroundAdvice" class="com.aspect.SurroundAdvice"/><bean id="beforeAdvice" class="com.aspect.BeforeAdvice"/><bean id="afterAdvice" class="com.aspect.AfterAdvice"/><bean id="owner" class="com.aspect.Owner"/><bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="proxyInterfaces" value="com.aspect.ProxyInter"/><property name="target" ref="owner"/><property name="interceptorNames"><list><value>surroundAdvice</value><value>beforeAdvice</value><value>afterAdvice</value></list></property></bean></beans>
在Client中
ProxyInter proxy = (ProxyInter)context.getBean("proxyFactoryBean");得到代理对象执行
结果如下:
欢迎您,请喝茶租房之前租房租房之后加钱
如果想要只给接口的rent方法加环绕通知,如下操作:
修改applicationContext.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" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="surroundAdvice" class="com.aspect.SurroundAdvice"/><bean id="beforeAdvice" class="com.aspect.BeforeAdvice"/><bean id="afterAdvice" class="com.aspect.AfterAdvice"/><bean id="owner" class="com.aspect.Owner"/><aop:config><aop:pointcut id="serviceMethod"expression="execution(* com.aspect.ProxyInter.rent(..))"/><aop:advisor advice-ref="surroundAdvice" pointcut-ref="serviceMethod"/></aop:config></beans>
在Client中
ProxyInter proxy = (ProxyInter)context.getBean("owner");得到代理对象执行即可
结果如下在Client中
ProxyInter proxy = (ProxyInter)context.getBean("proxyFactoryBean");得到代理对象执行
结果如下:
欢迎您,请喝茶租房加钱
而执行proxy.cancelHouse()方法则没有环绕通知
上面这种配置需要导入的4个jar包如下:
aspectjrt.jar
aspectjweaver.jar
commons-logging-1.0.4.jar
spring.jar
- Spring AOP配置 动态代理
- Spring Aop 动态代理
- Spring AOP动态代理
- Spring AOP 动态代理
- spring aop动态代理
- Spring(AOP动态代理)
- spring aop切面动态代理xml配置实现
- Spring AOP动态代理 /声明式事务管理与配置介绍
- Spring--AOP--基本配置(Intruductions-- Advice 动态代理)
- Spring AOP JDK动态代理
- 动态代理与spring AOP
- Spring AOP与动态代理
- Spring AOP动态代理初学
- Spring AOP技术--动态代理
- 7.Spring AOP动态代理
- Java动态代理-->Spring AOP
- spring AOP(动态代理)解析
- spring aop,java动态代理
- Ping命令的使用大全(windows)
- 10 GB到200 GB整10 GB递增的精确计算结果
- 分享11个超棒的移动应用(mobile apps)开发解决方案
- 【Java名字重用的术语表】
- android asmack 注册 登陆 聊天 多人聊天室 文件传输
- Spring AOP配置 动态代理
- 显示或隐藏日历中的农历、天干地支符号或六曜标记 (zz)
- oracle 进行内存设置导致无法正常启动(假启动)为题
- Eclipse快捷键大全
- Git_git的创建与管理远程分支
- 技术转载:android listview 连续调用 getview问题分析及解决
- C#中两个日期之间的间隔天数
- Mac上安装Git
- Android模拟器网络连接问题解决