一:通过context:property-placeholder标签实现配置文件加载
在spring的配置文件中添加如下声明
引用值时,注意使用$引用需要的值
1.在datasource.xml中
2.在java代码中
@Value("${targetVal}")private String targetVal; //注意:这里变量不能定义成static
二:通过 util:properties 标签实现配置文件加载
在spring的配置文件中添加如下声明
需要注意几点,这种方式需要在spring配置文件头部进行如下声明
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"
在引用值时,注意使用#
1.在xml中使用
2.在java代码中
@Value(value="#{jdbc['targetVal']}")private String targetVal;
三、通过 @PropertySource 注解实现配置文件加载
在java类文件中使用 PropertySource 注解:
@PropertySource(value={"classpath:jdbc.properties"})public classReadProperties {
@Value(value="${jdbc.username}")privateString username;
}
四、通过 PropertyPlaceholderConfigurer 类读取配置文件
/WEB-INF/mail.properties
classpath:jdbc.properties
取值与方法一相同
五:使用 PropertiesFactoryBean 加载
classpath*:/conf/application.properties
classpath*:/conf/streamserver.properties
取值与方法二相同
以上是常见的取值方式,下一篇将会介绍context:property-placeholder常用的属性
转 : https://blog.csdn.net/Cool_Coding/article/details/90617687