java - 在Spring mybatis的maven项目中如何实现动态配置?
PHP中文网
PHP中文网 2017-04-17 17:50:23
0
5
826

比如在项目中,我们会把数据库连接信息和redis连接信息放在properties文件中,我想把这个文件里的连接信息变成动态,也不需要去重启服务就可以切换,如何实现?

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(5)
迷茫

There is a configuration called Profile in Maven, which can be configured differently for different environments.

<profiles>
     <profile>
          <id>dev</id>
          <properties>
               <db.driver>com.mysql.jdbc.Driver</db.driver>
               <db.url>jdbc:mysql://192.168.1.100:3306/test</db.url>
               <db.username>dev</db.username>
               <db.password>dev-pwd</db.password>
          </properties>
     </profile>
     //可定义多个profile,针对不同环境的不同id
     <profile>
     ...
     <profile>
</profiles>

Use the command line to specify different configurations based on different Profile Ids

mvn clean install -P dev
Peter_Zhu
FYI

http://stackoverflow.com/questions/14117117/dynamically-loading-properties-file-using-spring
    <bean id="myProperties" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <!-- check property file(s) every 1 second -->
        <property name="cacheSeconds" value="1"/>
        <property name="basenames">
            <list>
                <value>myApp/myApp</value>
            </list>
        </property>
    </bean>
Ty80
有一个技术,叫做autoconfig的。你可以百度了解一下。
可以通过配置文件,配置测试环境、开发环境、线上环境,项目会根据不同的情况下,分别加载不同的配置文件。
Peter_Zhu

It is impossible to change the connected database without restarting the server. You can obtain the connection information value, but it is impossible to dynamically switch to another database

阿神

Keywordsjrebel

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!