问题描述
定义一堆bean的常见策略是什么,它们在开发和生产环境中使用的不同?
假设我有两个bean,接口。一个bean用作本地文件系统的抽象,另一个bean连接到分布式文件系统。为了使开发尽可能稳定,开发环境应该使用本地文件系统实现,生产版本使用分布式文件系统bean。
目前我在做的是两个xml定义。
native.xml
< bean id =resourceSystemclass =com.cust.NativeResourceSystem/>
distributed.xml
< bean id =resourceSystemclass =com.cust.HadoopResourceSystem>
< constructor-arg name =fsref =hdfs/>
< / bean>
创建应用程序上下文时,省略 native.xml
或 distributed.xml
,取决于环境并抓取 resourceSystem
bean。
有没有适当的工具或最佳实践来配置不同环境的bean定义?
谢谢。
这里是Spring参考文档所说的
如上所示,您可以设置一个Java系统属性
p>
-Dprofile = development
在生产机器上
-Dprofile = production
pre>
因此,您可以定义全局应用程序上下文设置,其中导入每个分层上下文设置如下
<?xml version =1.0encoding =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>
请记住,所有位置路径都是相对于执行导入的定义文件
因此,Spring支持这种配置
What are common strategies for defining a bunch of beans, which are used differently in development and production environments?
Let's say I have 2 beans, each implementing the same interface. One bean serves as abstraction for local filesystem, the other connects to a distributed filesystem. To keep the development as steady, as possible, the development environment should use local filesystem implementation, the production release uses distributed filesystem bean.
Currently what I'm doing is having two xml definitions.
native.xml
<bean id="resourceSystem" class="com.cust.NativeResourceSystem" />
distributed.xml
<bean id="resourceSystem" class="com.cust.HadoopResourceSystem"> <constructor-arg name="fs" ref="hdfs" /> </bean>
When creating application context I omit either
native.xml
ordistributed.xml
depending on environment and grab theresourceSystem
bean.Are there any proper tools or best practices in Spring to configure bean definitions for different environments?
Thanks.
解决方案Here goes what Spring reference documentation says about PropertyPlaceholderConfigurer
As you can see above you can set up a Java System property
On The development machine
-Dprofile=development
On The production machine
-Dprofile=production
So you can define a global application context settings which import each layered context settings as follows
<?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>
Keep in mind all location paths are relative to the definition file doing the importing
Therefore, This kind of configuration is supported by Spring
这篇关于为不同环境定义Spring bean时的常见策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!