`

Spring ProxyFactoryBean

阅读更多
There are several discussion about swapping objects during runtime. For example, we may have a case where we need to swap databases depending on the configuration settings or runtime variables, such as user info.

For database related, Spring 2.0 added two classes to short-circuit this case, AbstractRoutingDataSource and its subclass. These two classes are suitable when the choices of swappables are known and listable,  i.e., not based on some formula to deduct the  URL.  The references are:

http://blog.interface21.com/main/2007/01/23/dynamic-datasource-routing/

A more general approach using ProxyfactoryBean is discussed in here:

http://blog.arendsen.net/index.php/2005/07/06/spring-instance-management-part-i-pooling/
http://blog.arendsen.net/index.php/2005/07/06/spring-instance-management-part-ii-pooling/

Recently, I have a case in the distributed computing. I have an ejb-based app which has quite a few ejb services running on different machines. The ejb services are located through a standard service locator. If a service A needs another service B, then A has a piece of code using the locator to find B. This piece of code is pretty ugly because:
1. It results about several hundreds on references on the service locator, which is a singleton, like a wild running cat herd.
2. I can't mock service B for testing.
3. The error handling is wild too.

A better solution is to let A depend on B's interface and inject B from somewhere. we could use Spring's SimpleRemoteStatelessSessionProxyFactoryBean class, however, we have a problem with this. The servers where B is setting on are not fixed, we may move the clusters for several reasons. So even Spring's class is out, with the failover feature, which works only on the fixed cluster. This means the setter during startup time sets something that could be expired and can't be used later on. We have to use the service locator to look for B right before we trigger B's method, i.e., we need a real-time setter.

Another problem, we could refresh the service location settings in real-time. Spring is a little bit hard to do this. So I decide to reuse this.

Spring ProxyFactoryBean can be used for runtime configurations:

1. Switch DataSource depending on the runtime user
2. Delay calling setters which works in real time.
分享到:
评论

相关推荐

    Spring AOP 源码系列:ProxyFactoryBean

    NULL 博文链接:https://czj4451.iteye.com/blog/2051461

    SpringBootAopInterceptor:Spring Boot ProxyFactoryBean

    SpringBootAopInterceptor Spring Boot ProxyFactoryBean

    Spring基于ProxyFactoryBean创建AOP代理

    主要介绍了Spring基于ProxyFactoryBean创建AOP代理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    spring-aop-ProxyFactoryBean 源码分析

    NULL 博文链接:https://wangxinchun.iteye.com/blog/2079585

    Spring-Reference_zh_CN(Spring中文参考手册)

    7.5. 使用ProxyFactoryBean创建AOP代理 7.5.1. 基础 7.5.2. JavaBean属性 7.5.3. 基于JDK和CGLIB的代理 7.5.4. 对接口进行代理 7.5.5. 对类进行代理 7.5.6. 使用“全局”advisor 7.6. 简化代理定义 7.7. 使用...

    Spring

    --这里是自定义的拦截器的实现,在Spring的ProxyFactoryBean类中必须 指定interceptorNames的值 --> class="org.nitpro.aop.TransactionInterceptor"> <!--bizImpl为我们自己的业务处理实现 --> ...

    Spring 2.0 开发参考手册

    7.5. 使用ProxyFactoryBean创建AOP代理 7.5.1. 基础 7.5.2. JavaBean属性 7.5.3. 基于JDK和CGLIB的代理 7.5.4. 对接口进行代理 7.5.5. 对类进行代理 7.5.6. 使用“全局”advisor 7.6. 简化代理定义 7.7. ...

    using-polymorphism-insteadof-ifs:如何在 Spring Framework 中创建运行时注入的简单示例

    我已经创建了基于 spring ProxyFactoryBean 的简单机制。 在运行时调用服务的方法时,我们正在做出我想要使用的决策女巫实现。 这张图片或多或少地描述了概念: 主要条件 使用多态而不是 IFS 我的朋友 尽可能简单...

    Spring中文帮助文档

    7.5. 使用ProxyFactoryBean创建AOP代理 7.5.1. 基础 7.5.2. JavaBean属性 7.5.3. 基于JDK和CGLIB的代理 7.5.4. 对接口进行代理 7.5.5. 对类进行代理 7.5.6. 使用“全局”通知器 7.6. 简化代理定义 7.7. 使用...

    spring chm文档

    7.5. 使用ProxyFactoryBean创建AOP代理 7.5.1. 基础 7.5.2. JavaBean属性 7.5.3. 基于JDK和CGLIB的代理 7.5.4. 对接口进行代理 7.5.5. 对类进行代理 7.5.6. 使用“全局”advisor 7.6. 简化代理定义 7.7. ...

    spring aop 实现源代码--xml and annotation(带lib包)

    在这里,我们分别定义了一个MessageSender对象(messageSenderImpl)和一个Before Advice对象(logBeforeAdvice),并定义了一个 org.springframework.aop.framework.ProxyFactoryBean对象(messageSender),...

    Spring API

    7.5. 使用ProxyFactoryBean创建AOP代理 7.5.1. 基础 7.5.2. JavaBean属性 7.5.3. 基于JDK和CGLIB的代理 7.5.4. 对接口进行代理 7.5.5. 对类进行代理 7.5.6. 使用“全局”通知器 7.6. 简化代理定义 7.7. 使用...

    spring in action英文版

     3.5 使用ProxyFactoryBean  3.6 自动代理  3.6.1 BeanNameAutoProxyCreator  3.6.2 DefaultAdvisorAutoProxyCreator  3.6.3 元数据自动代理  3.7 小结 第二部分 Spring在业务层的应用  第4章...

    Spring in Action(第2版)中文版

    4.2.3使用proxyfactorybean 4.3自动代理 4.3.1为spring切面创建自动代理 4.3.2自动代理@aspectj切面 4.4定义纯粹的pojo切面 4.5注入aspectj切面 4.6小结 第二部分企业spring 第5章使用数据库 5.1spring的...

    Spring in Action(第二版 中文高清版).part2

    4.2.3 使用ProxyFactoryBean 4.3 自动代理 4.3.1 为Spring切面创建自动代理 4.3.2 自动代理@AspectJ切面 4.4 定义纯粹的POJO切面 4.5 注入AspectJ切面 4.6 小结 第二部分 企业Spring 第5章 使用数据库 5.1...

    Spring in Action(第二版 中文高清版).part1

    4.2.3 使用ProxyFactoryBean 4.3 自动代理 4.3.1 为Spring切面创建自动代理 4.3.2 自动代理@AspectJ切面 4.4 定义纯粹的POJO切面 4.5 注入AspectJ切面 4.6 小结 第二部分 企业Spring 第5章 使用数据库 5.1...

    Spring实现AOP的四种方式

    1. 配置ProxyFactoryBean,显式地设置advisors, advice, target等(基于代理的AOP ) 2. 配置AutoProxyCreator,这种方式下,还是如以前一样使用定义的bean,但是从容器中获得的其实已经是代理对象 3. 通过来配置...

    spring-xmemcached

    <bean id="cacheProxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"> <value>cacheBeforeAdvice <value>cacheAfterAdvice <value>cacheInterceptor</value> ...

    SPRING API 2.0.CHM

    ProxyFactoryBean ProxyMethodInvocation QuartzJobBean QuickTargetSourceCreator RadioButtonTag RdbmsOperation ReaderContext ReaderEventListener RecordCreator RecordExtractor ...

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

Global site tag (gtag.js) - Google Analytics