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》 在深入探讨Spring AOP(面向切面编程)的实现机制时,ProxyFactoryBean是至关重要的一个组件。它扮演着生成代理对象的角色,使得我们能够在不修改原有代码的情况下,对...
`ProxyFactoryBean`是Spring中用于创建AOP代理的一种方式,它允许开发者以声明式的方式来实现面向切面编程,例如,可以通过配置`ProxyFactoryBean`来为特定的服务类添加日志或事务处理等方面的切面行为。 #### ...
`ProxyFactoryBean`是Spring AOP实现中的一个重要类,它用于创建代理对象,使我们能够实现动态代理以进行方法拦截。下面将深入分析`ProxyFactoryBean`的源码,探讨其工作原理和使用方式。 `ProxyFactoryBean`继承自...
Spring 基于 ProxyFactoryBean 创建 AOP 代理 ProxyFactoryBean 是 Spring 中用于创建 AOP 代理的重要工具类,它提供了完整的控制能力,可以生成指定的内容。下面将详细介绍 ProxyFactoryBean 的使用方法和 Spring...
在本讨论中,我们将深入探讨Spring AOP如何与`ProxyFactoryBean`结合使用,以及它在Spring Boot中的具体实现。 **Spring AOP基本概念** AOP是一种编程范式,旨在将关注点分离,使代码更具模块化和可维护性。在...
在源码中,`Advisor`、`Pointcut`和`Advice`等接口是描述切面的关键,而`ProxyFactoryBean`或`AspectJAutoProxyCreator`则是创建代理对象的工具。 Spring还提供了大量的模块,如数据访问/集成(DAO)、Web MVC、JMS...
`org.springframework.aop`和`org.springframework.aop.framework`包下的类,如`Advised`、`Advisor`、`Pointcut`和`ProxyFactoryBean`,构成了Spring AOP的基础。通过阅读源码,我们可以学习如何定义切面、切入点...
而`org.springframework.aop.framework.ProxyFactoryBean`则实现了AOP代理,通过动态代理机制,使得可以在不修改原始代码的情况下,添加新的行为。 此外,Spring的事务管理(`org.springframework.transaction`包)...
`org.springframework.aop`包下包含了AOP的核心实现,如`Advised`接口和`ProxyFactoryBean`类。 二、数据访问支持 1. **JDBC抽象层**:Spring提供了对JDBC的简化封装,通过`JdbcTemplate`和`SimpleJdbcTemplate`,...
在源码中,`org.springframework.aop`和`org.springframework.aop.framework`包下的类,如`AspectJExpressionPointcut`、`AdvisedSupport`和`ProxyFactoryBean`等,是用来实现AOP的关键。 **源码注释**: 这个...
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通过组合Advice、Pointcut和Advisor,以及使用`ProxyFactoryBean`生成的代理对象,实现了面向切面的编程。这种机制使得我们可以优雅地解耦关注点,提高代码的可维护性和可扩展性。通过深入理解...
在AOP部分,`org.springframework.aop.framework.ProxyFactoryBean`用于创建代理对象,实现切面功能。 Spring框架还支持注解驱动开发,如`@Autowired`用于自动装配依赖,`@Service`、`@Repository`和`@Controller`...
2. Spring的JDBC DAO实现:在Spring中,我们可以创建一个接口定义数据库操作,然后使用Spring的ProxyFactoryBean或AOP来实现接口,实现事务管理和异常处理。 三、Spring JDBC核心组件 1. DataSource:数据源是连接...
在源码中,`Advisor`和`Advice`接口定义了切面和通知,而`ProxyFactoryBean`和`AspectJAutoProxyCreator`实现了动态代理,用于创建切面代理对象。 3. **核心容器(Core Container)**:包括`beans`、`context`模块...
对于源码分析,Spring的`org.springframework.beans.factory.config.MethodInvokingFactoryBean`和`org.springframework.aop.framework.ProxyFactoryBean`是策略模式的典型应用。前者用于在初始化时调用目标对象的...
在Spring AOP的源码中,`org.springframework.aop.framework.ProxyFactoryBean`是创建代理的主要类,它会根据目标类是否有接口选择JDK动态代理或CGLIB。`org.aopalliance.intercept.MethodInterceptor`接口定义了...
源码中,Advised接口和ProxyFactoryBean扮演着关键角色。 在数据访问/集成方面,Spring提供了多种数据访问技术的抽象,包括JDBC、ORM(Hibernate、MyBatis)、JPA等。其DataAccessException异常层次结构简化了错误...
它包括了ProxyFactoryBean和AspectJ的集成。 5. **spring-expression.jar (SPeL)**:Spring表达式语言,提供了一种强大而灵活的方式来在运行时查询和操作对象图,常用于配置和动态数据绑定。 6. **spring-jdbc.jar...
对于AOP,可以查看`ProxyFactoryBean`和`AdvisedSupport`类,它们负责创建代理对象并实现切面的织入。Spring MVC的部分,如`DispatcherServlet`和`HandlerMapping`,则展示了如何处理HTTP请求并调用合适的处理器方法...