为不同环境定义Spring bean时的常见策略 [英] Common strategies when defining Spring beans for different environments

查看:228
本文介绍了为不同环境定义Spring bean时的常见策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

定义一堆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参考文档所说的 PropertyPlaceholderConfigurer


PropertyPlaceholderConfigurer不能仅在您指定的属性
文件中查找属性,而且如果找不到您要使用的属性,则还会检查Java系统属性


如上所示,您可以设置一个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支持这种配置


通常优选为这样的绝对位置保持间接,例如通过$ {...}占位符在运行时解析JVM系统属性



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 or distributed.xml depending on environment and grab the resourceSystem 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

The PropertyPlaceholderConfigurer does not look for properties only in the Properties file you specify, but also checks against the Java System properties if it cannot find a property you are trying to use.

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

It is generally preferable to keep an indirection for such absolute locations, for example, through "${...}" placeholders That are resolved against JVM system properties at runtime.

这篇关于为不同环境定义Spring bean时的常见策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆