WebSphere和PropertyPlaceholderConfigurer

时间:2009-08-11 15:01:22

标签: java spring websphere

我是属性的大用户(使用PropertyPlaceholderConfigurer),使我的应用程序尽可能“动态”。几乎所有的常量都是这样定义的。无论如何,我目前正在定义default.properties附带默认WAR。

在其他环境(验收/生产)中,我需要覆盖配置。我这样做如下:

<bean id="propertyManager"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:com/company/default.properties</value>
                <value>file:${COMPANY_PROPERTIES_LOCATION}\kbo-select-settings.properties</value>
            </list>
        </property>
    </bean>

通过这种方式,我可以为每个环境使用可升级的构建。

但是,我不喜欢这样一个事实:我无法从WebSphere内部更改任何属性。相反,我必须去每个服务器(我们有8个集群)并相应地更改属性。如果我可以从WebSphere内部更改那些并且之后只是执行重新启动,那将会更加用户友好...

任何人都知道如何进行这样的可推广构建?我已经为datasources / java mail / etc定义了JNDI配置。

谢谢!

5 个答案:

答案 0 :(得分:2)

我们通过在每个环境(local,dev,int,tst ...)的属性文件上使用扩展来解决此问题,并且每个文件都包含这些环境的特定值。您需要的唯一添加是服务器上的VM参数设置-Druntime.env = X.

您在配置文件中的查找将如下所示

<bean id="propertyManager"
 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
       <list>
          <value>classpath:com/company/default.properties.${runtime.env}</value>
          <value>file:${COMPANY_PROPERTIES_LOCATION}\kbo-select-settings.properties</value>
        </list>
    </property>
 </bean>

当然,这仅适用于相当静态的环境,因为它仍然不适合在运行时更改它,但它确实使应用程序的升级变得简单。如果您希望能够在不重新部署应用程序的情况下更改值,则必须将它们存储在应用程序之外,您似乎已经为 kbo-select-settings.properties < / p>

答案 1 :(得分:1)

一个潜在的问题是您正在硬编码属性文件的位置。您可以将属性文件的位置指定为JNDI资源,并回退到类路径上指定的默认值:

<!--  try to lookup the configuration from a URL, if that doesn't work, fall back to the properties on the classpath -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <bean class="org.springframework.core.io.UrlResource">
            <constructor-arg>
                <jee:jndi-lookup 
                    jndi-name="url/config" 
                    default-value="file:///tmp" /> <!--  dummy default value ensures that the URL lookup doesn't fall over if the JNDI resource isn't defined -->
            </constructor-arg>
        </bean>
    </property>
    <property name="properties">
        <bean class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="locations">
                <list>
                    <value>classpath:com/company/default.properties</value>
                </list>
            </property>
        </bean>
    </property>
    <property name="ignoreResourceNotFound" value="true"/> 
</bean>

这样,您可以使用Resources&gt;中的WAS控制台为不同的环境指定不同的文件名。网址&gt; URL通过使用JNDI名称“url / config”创建资源并将其指向正确的文件(file:/// your / path / to / properties)。

作为替代解决方案,如果您想通过控制台管理单个属性,您可以使用jee:jndi-lookup从web.xml env-entries获取值,而不是使用PropertyPlaceholderConfigurer(您可以使用它来管理) WAS控制台)。见this answer

答案 2 :(得分:0)

如果配置在EAR文件中,那么我知道没有简单的方法可以在没有后门作弊或重新部署应用程序的情况下传播更改。

我认为配置,特别是在推广应用时发生变化的配置不应该 应用。

Keys Botzum描述了一种方法here

请注意,您实际上可以使用标准WebSphere同步将不属于任何特定应用程序的文件传播到节点。

另一种选择是使用数据库进行配置。这些天将XML弹出到DB2这样的数据库中并不是很难。

答案 3 :(得分:0)

将指向配置文件的URL资源添加到您的websphere服务器,然后在您的应用程序中查找它是一种可行的方法。然后,您可以将URL配置为指向管理所有配置文件的中心位置 - 如果您使用svn并且您的svn具有只读访问权限,您甚至可以直接从svn(通过http)读取它们。

Spring为此提供了一些内置工具,这也意味着您可以优先考虑各种配置文件。

有关详细信息,请查看how-to-differentiate-between-test-and-production-properties-in-an-application

答案 4 :(得分:0)

我处理这个问题的方法是在JVM上使用属性值,然后将它们引用到在cluser或cell级别定义的WebSphere变量。例如,假设您想要在spring配置中的param1中设置一个名为value1的值,您将执行以下操作:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" />

然后如下所示引用变量:

<bean id="id" class="com.blah.class">
        <property name="value1" value="${param1}" />
</bean>

然后在测试中,您可以按如下方式设置测试:

/**
     * @see org.springframework.test.AbstractSingleSpringContextTests#prepareApplicationContext(org.springframework.context.support.GenericApplicationContext)
     */
    @Override
    protected void prepareApplicationContext(GenericApplicationContext context) {
        System.setProperty("param1", "myvalue");
        }

然后,从websphere配置中,如果您创建一个JVM变量并将其链接到WebSphere变量,您只需要更改WebSphere变量,它将自动更新每台机器上的所有JVM变量。

为此,请创建一个名为:

的JVM变量

参数1

,值为$ {webspherevar.param1}

然后创建一个名为:

的WebSphere变量

webspherevar.param1

它包含您需要放入的任何值。这样您就不必为每个环境提供值,而是可以将它们加载到环境中并使用它们。

我希望这会有所帮助。

相关问题