热门
最新
红包
立Flag
投票
同城
我的
发布
CSDN App 扫码分享
评论
1
打赏
- 复制链接
- 举报
下一条:
Spring中用来加载properties文件的相关:XML标签:<context:properties-placeholder>上面的标签实际是对【注册一个PropertyPlaceholderConfigurer对象到Spring容器】的简化,效果相同。使用注解的话,采用@PropertySource参考:https://www.cnblogs.com/javahr/p/8376742.html其中的value设置为 "classpath:db.properties" 则只能加载本项目classpath下的db.properties设置为 "classpath*:db.properties" 则能加载本项目所有依赖jar包中classpath下的db.properties参考:https://www.jb51.net/article/192586.htm注意:@PropertySource在Spring解析后,会把对应properties文件中的属性注入到Spring的Environment对象中,当多个类上带有@PropertySource注解,加载了多个properties文件时,若这些properties文件中有同名变量,则后加载的会覆盖掉先前加载的,此时在其他类中通过 ${} 获取变量时,获取到的都是同一份数据。比如,有一个db.properties中有个db.url=123,另一个db2.properties中有个db.url=456,有2个类 DbProperties 和 Db2Properties,分别用 @PropertySource加载了 db.properties 和 db2.properties,在这两个类中,用${db.url} 获取到的值可能都是 456,这里假设db2.properties在db.properties之后加载。这个情况要特别注意。用@PropertySource注入的属性,统一存放在Spring的Environment中,会发生同名变量覆盖的问题