代码之家  ›  专栏  ›  技术社区  ›  ptomli

使用sysadmin定义的属性配置基于Spring的servlet

  •  1
  • ptomli  · 技术社区  · 15 年前

    我有一个基于Spring和应用程序上下文的Web应用程序,applicationContext.xml和myservlet-servlet.xml包含部署中sysadmin应该配置的设置。

    在不需要编辑战争内容的情况下,允许更改[数据库服务器详细信息、远程WebService端点等]设置的最佳方法是什么?

    弹簧提供 PropertyPlaceholderConfigurer 这可以在be an s配置中使用,但是我认为这需要一个属性文件的绝对路径,我希望避免这种情况,如果没有其他原因,只允许同一servlet的多个实例在同一台机器上运行。

    也可以选择使用JNDI配置的资源,尽管似乎没有 BeanFactoryPostProcessor 实现这一点是现成的,因此它可能不是一个很好的方法来处理它。

    如果有这样的要求,那么标准的最佳实践是什么?

    相关销售订单条目:

    How can I specify system properties in Tomcat configuration on startup?

    3 回复  |  直到 15 年前
        1
  •  1
  •   Community CDub    8 年前

    您还可以使用基于文件的解决方案来实现这一点。在每个环境上定义一个环境名称系统属性。然后使用此名称加载外部属性文件。下面的示例加载一个默认集,然后用特定于环境的集覆盖该默认集。

    <bean id="propertyPlaceholderConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
        <property name="locations">
                <list>
                        <value>classpath:site/properties/default/placeholder.properties
                        </value>
                        <value>file:///site/properties/${env.name}/placeholder.properties
                        </value>
                </list>
        </property>
    </bean>
    

    改编自,我的答案 here

        2
  •  1
  •   duffymo    15 年前

    “在不需要编辑战争内容的情况下,允许更改设置(如[数据库服务器详细信息、远程WebService端点等])的最佳方法是什么?”

    唯一的方法是将配置外部化。您可以分解war文件,将.properties文件移出war(只要它在类路径中,Spring就会找到它),或者将可修改的值放入数据库中。

        3
  •  0
  •   sourcerebels    15 年前

    如果不想外部化属性文件:

    我使用的前缀表示我的属性中的部署环境。例子:

    #Test url
    test.url=http://test.url.com
    
    #Production URL
    prod.url=http://prod.url.com
    

    我在每个环境中定义了一个名为“entorn”的系统属性(在应用服务器启动脚本中是jvm调用的参数)。此属性的值在我的测试环境中为“test”,在我的生产环境中为“prod”。

    然后我定义了我的“属性配置器”bean:

    <bean id="propertyConfigurer" class="es.indra.ccma.config.EnvironmentPropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:ccma.properties</value>
            </list>
        </property>
    </bean>
    

    环境属性占位符配置程序代码:

    package es.indra.ccma.config;
    
    import java.util.Properties;
    
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
    
    public class EnvironmentPropertyPlaceholderConfigurer extends
            PropertyPlaceholderConfigurer {
    
        private String environment;
        final private static String ENV_SYSTEM_PROPERTY = "entorn";
    
        public EnvironmentPropertyPlaceholderConfigurer() {
    
            environment = System.getProperty(ENV_SYSTEM_PROPERTY);
            if (environment == null) {
                //default environment
                environment = "test";
            }
        }
        protected String resolvePlaceholder(String placeholder, Properties props) {
    
            final String envPlaceholder = environment + "." + placeholder;
            if (props.containsKey(envPlaceholder)) {
                return props.getProperty(envPlaceholder);
            } else {
                return props.getProperty(placeholder);
            }
        } 
    }
    

    如果您在“测试”环境中运行代码,并且要检索“url”属性的值,则属性配置程序会在属性文件中查找“test.url”,如果未找到“test.url”属性,则会查找“url”属性。

    这不是我的主意 I followed this tutorial 完成这个。