一 <context:property-placeholder/>
或 PropertyPlaceholderConfigurer
说明:
-(property-placeholder)属性占位符,就是加载properties资源文件
在xml文件中可以用 ${属性key}获取value;
- 在spring管理的javabean中也可以获取-> @Value(“属性key”) String xx;
- localtioin属性可以配置多个文件,用“,”进行分开
1. spring 配置<context:property-placeholder/>
作用
spring.xml 配置如下
<context:property-placeholder ignore-unresolvable="true" location="classpath*:/base.properties,classpath:database.properties" />
<!-- SpringMVC上传文件时,需要配置MultipartResolver处理器 -->
<bean id="multipartResolver" class="com.hesvit.common.upload.IntelCommonsMultipartResolver">
<!-- 请求总文件大小 -->
<property name="maxUploadSize" value="${file.upload.allMaxUploadSize}" />
<!-- 单个文件大小 -->
<property name="fileMaxUploadSize" value="${file.upload.perFileMaxUploadSize}" />
</bean>
base.properties文件如下
file.upload.perFileMaxUploadSize=10485760
file.upload.allMaxUploadSize=52428800
woshi=123121
${file.upload.allMaxUploadSize}
和 ${file.upload.perFileMaxUploadSize}
就是通过可以获取base.properties文件中的对应的value的
@Component //此spring的注解一定需要添加
public class WebConstant {
// 可以在配置文件中获取常量
@Value(value="${file.upload.allMaxUploadSize}")
private String allMax;
@Value(value="${file.upload.perFileMaxUploadSize}")
private String perFileMax;
public static String WOSHI;
@Value(value="${woshi}")
public void setWoshi(String WOSHIt) {
WOSHI= WOSHIt;
}
// 这种方式也是可以拿到
@Value(value="${woshi}")
public String WOSHI;
}
2. <context:property-placeholder/>
简化了PropertyPlaceholderConfigurer
原来老的配置
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:base.properties</value>
<value>classpath:database.properties</value>
</list>
</property>
</bean>
由此可见<context:property-placeholder/>
更加简洁
3.<context:property-placeholder/>
中的属性解析
- location:表示属性文件位置,多个之间通过如逗号/分号等分隔;
- file-encoding:文件编码;
- ignore-resource-not-found:如果属性文件找不到,是否忽略,默认false,即不忽略,找不到将抛出异常
- ignore-unresolvable:是否忽略解析不到的属性,如果不忽略,找不到将抛出异常
- properties-ref:本地java.util.Properties配置
- local-override:是否本地覆盖模式,即如果true,那么properties-ref的属性将覆盖location加载的属性
- system-properties-mode:系统属性模式,ENVIRONMENT(默认),NEVER,OVERRIDE
- ENVIRONMENT:将使用Spring 3.1提供的PropertySourcesPlaceholderConfigurer,其他情况使用Spring 3.1之前的PropertyPlaceholderConfigurer
- OVERRIDE: PropertyPlaceholderConfigurer使用,因为在spring 3.1之前版本是没有Enviroment的,所以OVERRIDE是spring 3.1之前版本的Environment
- NEVER:只查找properties-ref、location;
- order:当配置多个时的查找顺序
使用注意:
1.location中的加载文件的顺序
如果location中有多个文件:
classpath:db.properties,classpath:default.properties,classpath:default3.properties,classpath:default2.properties
将依次加载,值得注意的是如果后一个文件中有和前面某一个文件中属性名是相同的,最终取的值是后加载的值
举例来说:
default.properties文件中有个属性名userId,其对应的值为-1
default2.properties文件中也有一个属性名userId,其对应的值为-2
default3.properties文件中特有一个属性名userId,其对于那个的值为-3
default.properties文件先加载,此时userId的值为-1,当default3.properties文件加载时将更新原来的值,此时userId的值为-3,同理,最后加载default2.properties文件,所以userId最终值为-2
所以需要避免不同属性文件中的属性名称重名
二、通过ReloadableResourceBundleMessageSource加载属性文件
说明:用来做国际化等
属性文件如图:
spring配置文件如下:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messages/return_message</value>
<value>classpath:resource</value>
<value>classpath:message_info</value>
</list>
</property>
<property name="cacheSeconds" value="60" />
</bean>
java 文件中获取属性的方式
public class PropertisUtil {
/**
* getMessage(String arg0, Object[] arg1, Locale arg2) 这里, 第一
* 个参数是文件中的字段名,第二个参数是文件中占位符的参数(后面讲),第三个
* 参数是语言种类,可以通过Locale.种类获取
*/
public static String getMessage(String key) {
// 获取上下文
ApplicationContext ctx = ContextLoader.getCurrentWebApplicationContext();
String value = ctx.getMessage(key, new Object[0], Locale.getDefault());
return value;
}
public void xx () {
// key 为属性文件中的 键
String key = "client.send.verifycode.mail";
String value = getMessage(key);
System.out.println(value);
//输出结果为:Dear {userName}:<br/>Thanks for chosing Hesvit! Pl.....
}
}
ResourceBundle获取属性文件
ResourceBundle resourceBundle = ResourceBundle.getBundle("message_info_en_US");
System.out.println(resourceBundle.getString("client.send.verifycode.mail"));
//输出结果为:Dear {userName}:<br/>Thanks for chosing Hesvit!