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

从Spring重新加载属性

  •  0
  • theAnonymous  · 技术社区  · 5 年前

    <bean id="properties" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
        <property name="location">
            <value>WEB-INF/classes/applicationContext.properties</value>
            <value>WEB-INF/classes/myProperties.properties</value>
        </property>
    </bean>
    

    2 回复  |  直到 5 年前
        1
  •  0
  •   Renzo Avila    5 年前

    只需在可刷新bean上添加@RefreshScope注释,在主/配置中添加@EnableConfigServer。

    如果你想知道更多的配置 https://projects.spring.io/spring-cloud/spring-cloud.html

    当做!

        2
  •  0
  •   Katy    5 年前

    commons-configuration 图书馆:

        <dependency>
            <groupId>commons-configuration</groupId>
            <artifactId>commons-configuration</artifactId>
            <version>1.10</version>
        </dependency>
    

    然后,创建一个 PropertiesConfiguration 豆子:

        @Bean
        @ConditionalOnProperty(name = "spring.config.location", matchIfMissing = false)
        public PropertiesConfiguration propertiesConfiguration(
          @Value("${spring.config.location}") String path) throws Exception {
            String filePath = new File(path.substring("file:".length())).getCanonicalPath();
            PropertiesConfiguration configuration = new PropertiesConfiguration(
              new File(filePath));
            configuration.setReloadingStrategy(new FileChangedReloadingStrategy());
            return configuration;
        } 
    

    这个 FileChangedReloadingStrategy 设置为具有默认刷新延迟的重新加载策略。这意味着 属性配置 如果最后一次检查是在5000ms之前,则检查文件修改日期。

    FileChangedReloadingStrategy#setRefreshDelay .