search

Home  >  Q&A  >  body text

java - 在Spring mybatis的maven项目中如何实现动态配置?

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

PHP中文网PHP中文网2804 days ago851

reply all(5)I'll reply

  • 迷茫

    迷茫2017-04-17 17:52:23

    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

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:52:23

    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>

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 17:52:23

    有一个技术,叫做autoconfig的。你可以百度了解一下。
    可以通过配置文件,配置测试环境、开发环境、线上环境,项目会根据不同的情况下,分别加载不同的配置文件。

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-17 17:52:23

    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

    reply
    0
  • 阿神

    阿神2017-04-17 17:52:23

    Keywordsjrebel

    reply
    0
  • Cancelreply