JAVA中如何重新加载.properties文件,使其他引用实时改变?

JAVA中如何重新加载.properties文件,使其他引用实时改变?,第1张

       * Spring 提供的 PropertiesLoaderUtils 允许您直接通过基于类路径的文件地址加载属性资源

      

         * 最大的好处就是:实时加载配置文件,修改后立即生效,不必重启

        

         */

        private static void springUtil(){

                Properties props = new Properties()

                while(true){

                        try {

                                props=PropertiesLoaderUtils.loadAllProperties("message.properties")

                                for(Object key:props.keySet()){

                                        System.out.print(key+":")

                                        System.out.println(props.get(key))

                                }

                        } catch (IOException e) {

                                System.out.println(e.getMessage())

                        }

                        try {

                Thread.sleep(5000)

            } catch (InterruptedException e) {

                e.printStackTrace()

            }

                }

        }

转: https://blog.csdn.net/HaHa_Sir/article/details/79105951

1、用法示例: 在spring.xml配置文件中添加标签

2、在 spring.xml 中使用配置文件属性:

3、在java文件中使用:

需开启注解注入:

<context:annotation-config/>或 <context:component-scan/>

1、用法示例: 在spring.xml配置文件中添加标签

2、在spring.xml 中使用配置文件属性:

3、在java文件中使用:

1、用法示例:在java类文件中使用 PropertySource 注解:

2、在java文件中使用:

1、用法示例:在 spring.xml 中使用 <bean>标签进行配置

2、 PropertyPlaceholderConfigurer 配置方法,等价于 方式一,用法参考方法一

五、 还可以使用 org.springframework.beans.factory.config.PropertiesFactoryBean 加载,这里不再逐一列举了。

一个Properties只能加载一个问题,如果你需要加载多个的话只能多写几个了。

例如:

Properties prop = new Properties()

prop.load(ConfigUtil.class.getClassLoader().getResourceAsStream("config.properties"))

Properties prop1 = new Properties()

prop1.load(ConfigUtil.class.getClassLoader().getResourceAsStream("config.properties1"))


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/tougao/11424999.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存