1.PropertyPlaceholderConfigurer & <context:property-placeholder />
--单个PropertyPlaceholderConfigurer
ApplicationContext.xml
<context:property-placeholder location="classpath:test.properties,classpath:test1.properties"/>
test.properties
student.id=1000 student.name=siyuan
test1.properties
student.id=10001 student.name=siyuan1
注入(占位符${...})
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component("stu") public class Student { @Value("${student.id}") private int id; @Value("${student.name}") private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Test.java
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.siyuan.test.spring.Student; public class Test { public static void main(String[] args) { ApplicationContext ctxt = new ClassPathXmlApplicationContext("ApplicationContext.xml"); Student stu = (Student) ctxt.getBean("stu"); System.out.println(stu.getId() + ":" + stu.getName()); } }
输出结果
10001:siyuan1
结论:多个properties按先后顺序加载,重复则覆盖
--多个PropertyPlaceholderConfigurer
ApplicationContext.xml
<context:property-placeholder location="classpath:test.properties"/> <context:property-placeholder location="classpath:test1.properties"/>
输出结果
1000:siyuan
结论:按先后顺序加载,重复则忽略,即先加载的优先级高
改变优先级
ApplicationContext.xml
<context:property-placeholder location="classpath:test.properties" order="2"/> <context:property-placeholder location="classpath:test1.properties" order="1"/>
输出结果
10001:siyuan1
结论:可通过设置order属性改变优先级,优先级按order从小到大排序,即order小的优先级高
2.PropertiesFactoryBean & <util:properties/>
配置:
<util:properties id="studentProp" location="classpath:test.properties"/>
注入(SpEL):
@Value("#{studentProp['student.id']}")
相关推荐
log4j.properties配置文件
properties文件获取工具类:静态加载properties配置文件,有根据key获取值的方法
java如何读取properties配置文件
主要介绍了Python实现读取Properties配置文件的方法,结合实例形式分析了Python读取Properties配置文件类的定义与使用相关操作技巧,需要的朋友可以参考下
利用java的反射解析Properties文件转成对象 /** * 解析properties文件为对象 * @param * @param propPath * @param cls * @return * @throws InstantiationException * @throws ...
java读写properties配置文件
Log4j.jar包,和log4j.properties属性文件打包下载.
activiti和springboot整合只使用application.properties配置文件,解决了jdbc长时间待机连接被收回报错。使用springProcessEngineConfiguration对activiti管理
大家都喜欢把配置文件放在src目录下,如果有10个以上的配置文件为什么不考虑在WEB-INF目录下新建一个文件夹,专门放配置文件;这样即好管理,文件安全性又高。亲问题已经解决,把源代码共享给大家,已经通过测试;...
IDEA WEB项目启动不加载application.properties配置文件,yml配置文件是否也会不加载未尝试,如果有遇到的可以尝试一下。
Liferay 6.0.5 portal.properties 配置文件详解 详细说明了 portal.properties 配置文件可以配置的所有属性
log4j中使用的jar包,版本为1.2.17,下载即可使用,满足idea和eclipse使用,并附有配置文件
主要为大家详细介绍了SpringBoot获取yml和properties配置文件的内容,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
NULL 博文链接:https://bijian1013.iteye.com/blog/2307358
写好的读取properties配置文件的PropertiesConfig.java类,只需new个对象,PropertiesConfig config = new PropertiesConfig(); config.setPropertiesDataSource("/jdbc.properties"); config.getString(...
SSM框架下的几种加载properties配置文件方式,博客内含介绍https://blog.csdn.net/weixin_42803662
java对properties配置文件的读和写
NULL 博文链接:https://hnzhoujunmei.iteye.com/blog/769177
【Java 语言】读取 properties 配置文件 ( Java 语言中的 properties 配置文件 | 使用 properties 配置文件 ) https://hanshuliang.blog.csdn.net/article/details/134435350 博客源码
struts2的struts.properties配置文件详解