ProxyFactory的xml配置方式

来源:互联网 发布:软件开发毕业论文题目 编辑:程序博客网 时间:2024/05/29 08:47
在spring中配置,将所有的xml贴出来了,以及proxyFactory 所使用的一些属性解释:
<?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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-4.0.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util-4.0.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
 
 <bean id="greetingAdvice" class="com.web.spring.aop.example.beforadvice.GreetingBeforAdvice"/>
 <bean id="target" class="com.web.spring.aop.example.beforadvice.NaiveWaiter"/>
 <!--ProxyFactoryBean 是FactoryBean接口的实现类 -->
 <bean id="waiter" class="org.springframework.aop.framework.ProxyFactoryBean"
  p:proxyInterfaces="com.web.spring.aop.example.beforadvice.Waiter"
  p:interceptorNames="greetingAdvice"
  p:target-ref="target"
  p:proxyTargetClass="true"
 />
<!--
 1.proxyInterfaces代理所要实现的接口,可以是多个接口。该属性还有一个别名interfaces
 2.interceptorNames需要织入目标对象的bean列表,采用Bean的名称指定。这些Bean必须实现了
  org.aopalliance.intercept.MethodInterceptor或
  org.springframework.aop.Advisor 的Bean,配置中的顺序对应调用的顺序。
 3.singleton 返回的代理是否是简单实例,默认为单实例;
 4.optimize 当设置成true 时,强制使用Cglib代理。
 5.proxyTargetClass 是否对类进行代理(而不是对接口进行代理),设置为true时,使用CGLib代理。
 -->
</beans>

此外,在xml配置文件中proxyTargetClass 设置为true后,无需再设置proxyInterfaces属性,即使设置也会被ProxyFactoryBean忽略。

main方法测试:

 public static void main(String[] args) {
  ApplicationContext applicationContext=new ClassPathXmlApplicationContext("proxyFcatory.xml");
 
  Waiter waiter = (Waiter) applicationContext.getBean("waiter");
 
  waiter.greetTo("John");
 }
0 0
原创粉丝点击