1、用ResourceBundle
ResourceBundle rb=ResourceBundle.getBundle("prop");//加载名为prop为配置文件(prop.properties)
rb.getString("prodInstAcctEnable").trim();//通过对象获取属性值
这种加开关的方式虽然能够解决问题,但是每次获取开关的时候都要创建对象,然后获取开关;如果有很多处代码都要用到这个开关,那么就会创建很多次这个对象,这是对内存资源的浪费;也没有充分利用spring对配置属性的统一管理,一旦开关的名称发生改变,那么代码也要做相应的修改,不利于代码维护
2、通过PropertyPlaceholderConfigurer
<context:property-placeholder location="classpath:db.properties"/>
上面的配置和下面配置等价,是对下面配置的简化(从上到下依次加载)
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="fileEncoding" value="UTF-8"></property>
<property name="locations">
<list>
<value>classpath:applicationContext.properties</value>
<value>classpath:bankConfig.properties</value>
<value>classpath:bank/*.properties</value>
<value>classpath:payclient.properties</value>
</list>
</property>
</bean>
proterties文件中属性值的使用
<!-- C3P0 配置文件中value值的使用-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="driverClass" value="${jdbc.driverClassName}"></property>
</bean>
//java代码中value值的使用
@Component("configInfo")
public class ConfigInfo {
@Value("${jdbc.user}")
private String user;
@Value("${jdbc.password}")
private String password;
}
3、用PropertiesFactoryBean 和 PreferencesPlaceholderConfigurer
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath*:application.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties" />
</bean>
在这个配置文件中我们配置了注解扫描,和configProperties
实例和propertyConfigurer
实例。这样我们就可以在java类中自动注入配置了,我们看下java类中如何做:
//自动注入需要使用@Value注解,这个注解的格式#{configProperties['mysql.url']}
//其中configProperties是我们在appContext.xml中配置的beanId,mysql.url是在properties文件中的配置项。
@Component
public class MySQLConnectionInfo {
@Value("#{configProperties['mysql.url']}")
private String url;
@Value("#{configProperties['mysql.userName']}")
private String userName;
@Value("#{configProperties['mysql.password']}")
private String password;
}
4、用util命名
<util:properties id="sys" location="classpath:sys.properties"/>
//创建java文件,让Spring注入从资源文件中读取到的属性的值,如下
@Component
public class SysConf {
@Value("#{sys['jdbc.url']}")
private String test;
@Value("#{sys['jdbc.pwd']}")
public void setTest(String test){
test = test;
}
}
5、单独将proterties文件中的属性注入到java代码中
//让spring管理bean,IOC容器创建时加载这个类,将属性文件注入到方法参数Resource中,在程序中new TestUtil则行不通
@Controller
public class TestPUtil {
private static JSONObject propertyMap;
@Value("classpath:test.properties")// 注入文件资源
public void setTestFile(Resource resource) throws Exception {
String propertities = IOUtils.toString(resource.getInputStream());
String[] arr = propertities.split("\r\n");
propertyMap = new JSONObject();
for (String str : arr) {
if (str.startsWith("#")) continue;
if (!str.contains("=")) continue;
propertyMap.put(str.substring(0, str.indexOf("=")), str.substring(str.indexOf("=") + 1));
}
}
}