简体   繁体   English

Spring加载基于tomcat servlet上下文定义的application.properties

[英]Spring loading application.properties based on tomcat servlet context definition

I need to have a development and production settings for our spring project. 我需要为我们的春季项目进行开发和生产设置。 I understand that you can use profiles for spring but that is not something that we can do. 我知道您可以使用弹簧配置文件,但这不是我们可以做的事情。

What I want to do is place on the development environment a test-application.properties file and on production a prod-application.properties file. 我想要做的是在开发环境中放置一个test-application.properties文件,并在生产中放置一个prod-application.properties文件。 In the tomcat context definition we sent the following: 在tomcat上下文定义中,我们发送了以下内容:

<Context>
    <context-param>
        <param-name>properties_location</param-name>
        <param-value>file:C:\Users\Bill\test-application.properties</param-value>
    </context-param>
</Context>

And we can have the value changed for the production servers. 我们可以为生产服务器更改值。 In the spring config we have something like this: 在spring配置中我们有这样的东西:

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>${properties_location}</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="false" />
</bean>

But we keep getting errors like: 但我们不断收到如下错误:

org.springframework.beans.factory.BeanInitializationException: Could not load properties; org.springframework.beans.factory.BeanInitializationException:无法加载属性; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/${properties_location}] 嵌套异常是java.io.FileNotFoundException:无法打开ServletContext资源[/ $ {properties_location}]

Any ideas on how to solve? 关于如何解决的任何想法?

One feature of PropertyPlaceholder is that you can define multiple resource locations. PropertyPlaceholder的一个功能是您可以定义多个资源位置。 So for example you can define your-production-config.properties along with file:C:/Users/${user.name}/test-application.properties 例如,您可以定义your-production-config.properties以及文件:C:/ Users / $ {user.name} /test-application.properties

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:your-production-config.properties</value>
            <value>file:C:/Users/${user.name}/test-application.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="ignoreResourceNotFound" value="true"/>
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>        
</bean>

for production you need to place prod configuration into classpath somewhere(really not important where exactly, just classpath) - for local env you can use convension like this file:C:/Users/${user.name}/test-application.properties 对于生产,你需要将prod配置放在某个地方的classpath中(确切地说,确切地说,这并不重要,只是类路径) - 对于本地环境,你可以使用像这个文件的对应:C:/ Users / $ {user.name} /test-application.properties

I ended up solving it by not using context params. 我最终通过不使用上下文参数来解决它。 Instead we have defined 相反,我们已定义

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:application.properties</value>
            <value>file:C:\Users\Bill\prod-application.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
    <property name="ignoreResourceNotFound" value="true"/>
</bean>

This way tries to load both files. 这种方式尝试加载两个文件。 On test servers we do not have the prod file so it is not loaded. 在测试服务器上,我们没有prod文件,因此未加载。 On prod server the prod-application.properties file exists and overrides the test which is in the classpath. 在prod服务器上,prod-application.properties文件存在并覆盖类路径中的测试。 Cumbersome but works! 繁琐但有效!

<context:property-placeholder location="file:${catalina.home}/conf/myFirst.properties" ignore-unresolvable="true" />
<context:property-placeholder   location="classpath:second.properties"  ignore-unresolvable="true"  />

I do it like above. 我是这样做的。 The catalina.home variable allows the properties file to be lcoated in the tomcat home conf directory. catalina.home变量允许在tomcat home conf目录中显示属性文件。

Personally, try to avoid specify locations. 就个人而言,尽量避免指定位置。 I think best thing for you is to use JNDI to achieve this. 我认为最好的办法是使用JNDI来实现这一目标。

In tomcat/conf/server.xml 在tomcat / conf / server.xml中

<Resource name="jdbc/prod" auth="Container"
          type="javax.sql.DataSource" driverClassName="${database.driverClassName}"
          url="${database.url}"
          username="${database.username}" password="${database.password}"
          maxActive="20" maxIdle="10"
          maxWait="-1"/>

and In tomcat catalina.properties (If using Oracle XE otherwise change it accordingly): 并在tomcat catalina.properties中(如果使用Oracle XE,则相应地更改它):

database.driverClassName=oracle.jdbc.driver.OracleDriver
database.url=jdbc:oracle:thin:@//localhost:1521/XE
database.username=user
database.password=password

In your application create properties file in your classpath named jdbc.properties and put followings (If using Oracle XE otherwise change it accordingly) 在您的应用程序中,在名为jdbc.properties的类路径中创建属性文件并添加以下内容(如果使用Oracle XE,则相应地更改它)

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:user/password@//localhost:1521/XE

then In Spring applicationContext.xml 然后在Spring applicationContext.xml中

<context:property-placeholder location="classpath:jdbc.properties" />

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/jdbc/prod" />
    <property name="defaultObject" ref="dataSourceFallback" />
</bean>
<bean id="dataSourceFallback" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="poolPreparedStatements">
        <value>true</value>
    </property>
    <property name="maxActive">
        <value>4</value>
    </property>
    <property name="maxIdle">
        <value>1</value>
    </property>
</bean>

use : 使用 :

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>C:/Users/Bill/test-application.properties</value>
        </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="false" />
</bean>

Remove below code from web.xml web.xml删除以下代码

<Context>
    <context-param>
        <param-name>properties_location</param-name>
        <param-value>file:C:\Users\Bill\test-application.properties</param-value>
    </context-param>
</Context>

if you are using Tcserver and server.xml to configure Resources like database,queues etc you can using the com.springsource.tcserver.properties.SystemProperties 如果您使用Tcserver和server.xml配置数据库,队列等资源,您可以使用com.springsource.tcserver.properties.SystemProperties

Delcare this listener in server.xml like below 在server.xml中Delcare这个监听器,如下所示

 <Listener className="com.springsource.tcserver.properties.SystemProperties"
            file.1="${catalina.base}/conf/password.properties"
            file.2="${catalina.base}/conf/server.properties"
            immutable="false"
            trigger="now"/>

Now you can externalize the properties to the two files password.properties and server.properties. 现在,您可以将属性外部化为两个文件password.properties和server.properties。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Boot未加载application.properties - Spring Boot not loading application.properties Spring 引导环境后处理器未加载 application.properties - Spring Boot EnvironmentPostProcessor not loading application.properties 基于application.properties的Spring Boot和EntityManager - Spring boot and EntityManager based on application.properties 在插件中通过Tomcat覆盖Spring Boot application.properties属性? - Overwriting Spring Boot application.properties properties in plugin and through Tomcat? 在Tomcat中为Spring Boot应用程序访问externalize application.properties? - access externalize application.properties in Tomcat for Spring boot application? Spring Application Context未从application.properties文件获取属性 - Spring Application Context Not getting Property from application.properties file 在创建 Spring 应用程序上下文之前阅读 application.properties - Read application.properties before creating Spring application context 弹簧加载基于子模块中的application.properties的bean - Spring loading beans which are based on application.properties from a sub-module 如何在 Spring 的 Tomcat Web 服务器中外部化 application.properties? - How to externalize application.properties in Tomcat webserver for Spring? 如何将Spring Boot application.properties外部化到tomcat / lib文件夹 - How to externalize Spring Boot application.properties to tomcat/lib folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM