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

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

问题描述

定义一堆 bean 的常见策略是什么,它们在开发和生产环境中的使用方式不同?

What are common strategies for defining a bunch of beans, which are used differently in development and production environments?

假设我有 2 个 bean,每个 bean 实现相同的接口.一个 bean 作为本地文件系统的抽象,另一个连接到分布式文件系统.为了保持开发的稳定,开发环境应该使用本地文件系统实现,生产版本使用分布式文件系统 bean.

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.

目前我正在做的是有两个 xml 定义.

Currently what I'm doing is having two xml definitions.

native.xml

<bean id="resourceSystem" class="com.cust.NativeResourceSystem" />

分布式.xml

<bean id="resourceSystem" class="com.cust.HadoopResourceSystem">
    <constructor-arg name="fs" ref="hdfs" />
</bean>

在创建应用程序上下文时,我会根据环境省略 native.xmldistributed.xml 并获取 resourceSystem bean.

When creating application context I omit either native.xml or distributed.xml depending on environment and grab the resourceSystem bean.

Spring 中是否有合适的工具或最佳实践来为不同的环境配置 bean 定义?

Are there any proper tools or best practices in Spring to configure bean definitions for different environments?

谢谢.

推荐答案

这里是 Spring 参考文档中关于 PropertyPlaceholderConfigurer

Here goes what Spring reference documentation says about PropertyPlaceholderConfigurer

PropertyPlaceholderConfigurer 不会只在 Properties 中寻找属性您指定的文件,而且还会检查 Java 系统属性是否找不到您尝试使用的属性.

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.

如上所示,您可以设置 Java 系统属性

As you can see above you can set up a Java System property

在开发机器上

-Dprofile=development

在生产机器上

-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>

记住所有位置路径都相对于进行导入的定义文件

因此,Spring 支持这种配置

Therefore, This kind of configuration is supported by Spring

通常最好为这种绝对位置保留一个间接地址,例如,通过${...}"占位符在运行时根据 JVM 系统属性解析.

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天全站免登陆