spring-aop的使用
来源:互联网 发布:淘宝客服物流用语 编辑:程序博客网 时间:2024/05/21 16:57
beans.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:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- 配置被代理的对象 --><bean id="testService" class="com.aop.TestService"><property name="name" value="韩逸俊"/></bean><!-- 配置前置通知 --><bean id="MyMethodBeforeAdvice" class="com.aop.MyMethodBeforeAdvice"/><!-- 配置后知通知 --><bean id="MyAfterRetruningAdvice" class="com.aop.MyAfterRetruningAdvice"/><!-- 配置环绕童通知 --><bean id="MyMethodInterceptor" class="com.aop.MyMethodInterceptor"></bean><!-- 配置异常通知 --><bean id="MyThrowsAdvice" class="com.aop.MyThrowsAdvice"></bean><!-- 定义前置通知的切入点 --><bean id="MyMethodBeforeAdviceFilter" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"><property name="advice" ref="MyMethodBeforeAdvice"/><property name="mappedNames"><list><value>sayHello</value></list></property></bean><!-- 配置代理对象 --><bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"><!-- 代理接口集 --><property name="proxyInterfaces"><list><value>com.aop.TestServiceInter</value><value>com.aop.TestServiceInter2</value></list></property><!--把通知织入到代理对象 --><property name="interceptorNames"><list><!-- 相当于把MyMethodBeforeAdvice前置通知和代理对象关联起来,我们也可以把通知看成拦截器 --><value>MyMethodBeforeAdviceFilter</value><!-- 织入后知通知 --><value>MyAfterRetruningAdvice</value><!-- 织入环绕通知 --><value>MyMethodInterceptor</value><!-- 织入异常通知 --><value>MyThrowsAdvice</value></list></property><!-- 配置被代理对象,可以指定 --><property name="target" ref="testService"/></bean> </beans>MyAfterRetruningAdvice:
package com.aop;import java.lang.reflect.Method;import org.springframework.aop.AfterReturningAdvice;public class MyAfterRetruningAdvice implements AfterReturningAdvice {@Overridepublic void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {// TODO Auto-generated method stubSystem.out.println("关闭资源...");}}MyMethodBeforeAdvice:
package com.aop;import java.lang.reflect.Method;import org.springframework.aop.MethodBeforeAdvice;public class MyMethodBeforeAdvice implements MethodBeforeAdvice {/** * method:被调用的方法 * arg1:给method传递的参数 * arg2:目标对象 */@Overridepublic void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {// TODO Auto-generated method stubSystem.out.println("记录日志..."+arg0.getName());}}MyMethodInterceptor:
package com.aop;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;public class MyMethodInterceptor implements MethodInterceptor {@Overridepublic Object invoke(MethodInvocation arg0) throws Throwable {// TODO Auto-generated method studSystem.out.println("调用方法前。。。");Object a=arg0.proceed();System.out.println("调用方法后。。。");return a;}}MyThrowsAdvice:
package com.aop;import java.lang.reflect.Method;import org.springframework.aop.ThrowsAdvice;public class MyThrowsAdvice implements ThrowsAdvice {public void afterThrowing(Method m,Object[] os,Object target,Exception e){System.out.println("出事了"+e.getMessage());}}TestServiceInter
package com.aop;public interface TestServiceInter {public void sayHello();}
TestServiceInter2
package com.aop;public interface TestServiceInter2 {public void sayBye();}TestService
package com.aop;public class TestService implements TestServiceInter,TestServiceInter2 {private String name;@Overridepublic void sayHello() {// TODO Auto-generated method stubSystem.out.println("HI,"+name);}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic void sayBye() {// TODO Auto-generated method stubSystem.out.println("Bye,"+name);}}
测试:
package com.aop;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.inter.ChangeLetter;public class App1 {public static void main(String []args){ApplicationContext ac=new ClassPathXmlApplicationContext("com/aop/beans.xml");TestServiceInter t=(TestServiceInter) ac.getBean("proxyFactoryBean");t.sayHello();((TestServiceInter2)t).sayBye();}}
阅读全文
0 0
- Spring AOP的使用
- Spring AOP的使用
- spring-aop的使用
- Spring AOP的使用
- SPRING AOP的使用
- spring的aop的使用
- spring与AOP的使用
- 关于spring的AOP使用
- spring的Aop使用问题
- Spring中AOP的使用
- Spring aop 通知的使用
- Spring的AOP使用实例
- spring boot-aop的使用
- spring boot aop的使用
- spring aop(三)--aop在spring的使用
- AOP、Spring的AOP
- 【Spring AOP】基于AspectJ的AOP基本使用
- Spring AOP 一、AOP的概念与简单使用
- android fragment 刷新功能
- 在MvvmLight下使用{x:Bind}
- 关于http在servlet以及jsp中的一些理解
- java数据结构与算法-有向图的拓扑排序
- 【javascript笔记】prototype again!
- spring-aop的使用
- Android保存图片到本地
- Single Element in a Sorted Array问题及解法
- List Accounts
- Color argb()方法的参数
- forward 和 redirect 区别
- CodeFroces 817C Really Big Numbers(二分)
- 将lnmp 自带的 php 5.4升级为 5.6
- Centos系统下Docker安装