加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ProxyFactory的xml配置方式

发布时间:2020-12-16 05:00:58 所属栏目:百科 来源:网络整理
导读:在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
在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");
}
因此文与上篇有衔接,如有参照的朋友请将上篇一起参考。谢谢!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读