Jur*_*nka 8 java configuration spring
定义一堆bean的常用策略是什么,这些bean在开发和生产环境中的使用方式不同?
假设我有2个bean,每个bean实现相同的接口.一个bean用作本地文件系统的抽象,另一个bean连接到分布式文件系统.为了使开发尽可能稳定,开发环境应该使用本地文件系统实现,生产版本使用分布式文件系统bean.
目前我正在做的是有两个xml定义.
native.xml
<bean id="resourceSystem" class="com.cust.NativeResourceSystem" />
Run Code Online (Sandbox Code Playgroud)
distributed.xml
<bean id="resourceSystem" class="com.cust.HadoopResourceSystem">
<constructor-arg name="fs" ref="hdfs" />
</bean>
Run Code Online (Sandbox Code Playgroud)
在创建应用程序上下文时,我省略native.xml
或distributed.xml
依赖于环境并获取resourceSystem
bean.
Spring中是否有适当的工具或最佳实践来为不同的环境配置bean定义?
谢谢.
Art*_*ald 11
以下是Spring参考文档中有关PropertyPlaceholderConfigurer的内容
PropertyPlaceholderConfigurer不仅在您指定的Properties文件中查找属性,而且如果找不到您尝试使用的属性,也会检查Java System属性.
如您所见,您可以设置Java System属性
在开发机器上
-Dprofile=development
Run Code Online (Sandbox Code Playgroud)
在生产机器上
-Dprofile=production
Run Code Online (Sandbox Code Playgroud)
因此,您可以定义全局应用程序上下文设置,导入每个分层上下文设置,如下所示
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<context:property-placeholder/>
<import resource="service-${profile}.xml"/>
<import resource="persistence-${profile}.xml"/>
<import resource="security-${profile}.xml"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
请记住,所有位置路径都与执行导入的定义文件相关
因此,Spring支持这种配置
通常最好为这样的绝对位置保持间接,例如,通过"$ {...}"占位符,这些占位符在运行时针对JVM系统属性进行解析.
归档时间: |
|
查看次数: |
4300 次 |
最近记录: |