Spring接口和类代理实例

来源:互联网 发布:软件自主开发销售收入 编辑:程序博客网 时间:2024/06/06 01:14

Spring通过接口代理毫无疑问能够提高程序代码的灵活性,接口代理的四个步骤:
①:配置被代理的目标类
②:配置通知
③:配置通知的作用切入点
④:配置接口的代理对象(Spring默认为接口代理)

配置文件:

<?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.xsd                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- 配置目标对象 targetObject --><bean id="targetObject" class="com.zhiwei.target.TargetObject">    <property name="name" value="name"/></bean><!-- 配置通知 --><bean id="myAdvice" class="com.zhiwei.advice.MyAdvice"/><!-- 配置通知的切入点:NameMatchMethodPointcutAdvisor:按照名称匹配切入点(RegexpMethodPointcutAdvisor可以使用正则式匹配)mappedNames:通知作用的切入点:支持ant格式的表达:sys*注意:一个通知匹配一个切入点通知器 --> <bean id="beforAdvicePacket" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"> <property name="advice" ref="myAdvice"/> <property name="mappedNames">     <list>        <value>sayHello</value>     </list> </property> </bean><!-- 配置代理对象:  proxyInterfaces:代理接口集:目标对象实现的接口  target:被代理目标对象  interceptorNames:拦截器名称集合  proxyTargetClass:是否开启类代理   --><bean id="proxyObject" class="org.springframework.aop.framework.ProxyFactoryBean"><property name="proxyInterfaces"> <!-- 被代理接口 -->    <list>        <value>com.zhiwei.target.TargetInter</value>        <value>com.zhiwei.target.TargetInter01</value>    </list></property><property name="target" ref="targetObject"/>  <!-- 目标对象 --><property name="interceptorNames"> <!-- 目标对象注入的通知 -->    <list>        <value>beforAdvicePacket</value>    </list></property><property name="proxyTargetClass" value="true"/></bean></beans>

目标对象:

package com.zhiwei.target;public class TargetObject implements TargetInter ,TargetInter01{    private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    @Override    public void sayHello() {        System.out.println("Hello "+name);    }    @Override    public void sayBye() {        System.out.println("Bye "+name);    }}

目标对象实现接口:

package com.zhiwei.target;public interface TargetInter01 {  public void sayBye();}
package com.zhiwei.target;public interface TargetInter {    public void sayHello();}

测试类:

package com.zhiwei.target;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.zhiwei.target.TargetInter;import com.zhiwei.target.TargetInter01;import com.zhiwei.target.TargetObject;public class TestAOP {    public static void main(String[] args) {        ApplicationContext ac=new ClassPathXmlApplicationContext("com/zhiwei/target/applicationContext.xml");        //使用IOC容器里里面的代理对象,默认接口代理        TargetInter ti=(TargetInter) ac.getBean("proxyObject");        System.out.println("默认接口代理");        ti.sayHello();        //因为是接口代理,接口指向同一个目标对象:  TargetInter01 ti01=(TargetInter01) ac.getBean("proxyObject");        TargetInter01 ti01=(TargetInter01) ti;        ti01.sayBye();        //IOC配置开启类代理        TargetObject to=(TargetObject) ac.getBean("proxyObject");        System.out.println("\n\nIOC配置为类代理");        to.sayHello();        to.sayBye();    }}

结果:

这里写图片描述

0 0
原创粉丝点击