简体   繁体   English

Spring Web应用程序:如何外部化属性文件?

[英]Web app with Spring: how externalize properties file?

I've built a J2EE web app using Spring framework on server Tomcat, and my application needs to read some properties (like jdbs connection parameters, paths of folders etc...) which I need to be external. 我已经在服务器Tomcat上使用Spring框架构建了一个J2EE Web应用程序,并且我的应用程序需要读取一些属性(例如jdbs连接参数,文件夹路径等),这些属性必须是外部的。 What I mean is that today I put the jar on the server. 我的意思是说今天我将jar放在服务器上。 If in 2 years the connection DB parameters or the folders I want to use are going to change I don't want to redeploy the app, but just change the properties file. 如果两年后我要使用的连接数据库参数或文件夹将要更改,我不想重新部署该应用程序,而只需更改属性文件。

What I have by now is a .properties file in the classpath, and a bean that reads it: 我现在所拥有的是类路径中的.properties文件,以及读取它的bean:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:database.properties"/>
</bean>

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="url" value="${jdbc.url}" />
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

database.properties: database.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/cheExport?useSSL=false
jdbc.username=root
jdbc.password=root

How can I accomplish my purpose? 我该如何实现自己的目标?

If I remember well there is something I can do in the server xml, where I can specify that a certain folder contains properties file. 如果我还记得的话,可以在服务器xml中做一些事情,在其中可以指定某个文件夹包含属性文件。 But then how can I put the things together? 但是那我怎么把它们放在一起呢?

In this post Read properties file outside JAR file is asked the same question, but without using Spring, so I think it's not useful in my case 在这篇文章中, JAR文件之外读取属性文件也被问到相同的问题,但是没有使用Spring,所以我认为这对我来说没有用

I also read that it's common to use spring boot in this cases, but I'm using Spring MVC 我还读到在这种情况下使用Spring Boot是很常见的,但我使用的是Spring MVC

You use util properties. 您使用util属性。 just show for you my sample code. 只是给你看我的示例代码。

root-context.xml 根的context.xml

<util:properties id="config"
    location="classpath:config/config.properties" />

<bean id="officeDataSource"
    class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
    <property name="driverClass" value="#{config['driverClass']}" />
    <property name="url" value="#{config['commonUrl']}" />
    <property name="username" value="#{config['username']}" />
    <property name="password" value="#{config['password']}" />
</bean>

config.properties config.properties

driverClass=org.mariadb.jdbc.Driver
commonUrl=jdbc:mariadb://123.345.563.16:3306/FACE_BOOK_DB?useUnicode=yes&amp;characterEncoding=UTF-8

username=id
password=pw

for you project structure image. 为您的项目结构图。 exist resource(web-inf-classes-config) in your project. 项目中存在资源(web-inf-classes-config)。

在此处输入图片说明

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM