properties读取的几种方法

第一种:
private static Properties prop = new Properties();
    static{
        try {
            prop.load(MyContext.class.getClassLoader().getResourceAsStream("interface.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 根据 keyCode 获取properties 文件的值
     * @param keyCode
     * @return
     */
    public static String getUrlByKey(String keyCode){
        return prop.getProperty(keyCode);
    }
 其中MyContext是所在的类。
 
 第二种:
public static ResourceBundle config = ResourceBundle
.getBundle("config");
 
第三种:
  

第一步:

在applicationContext.xml配置:

 
1
2
3
4
5
6
7
8
9
10
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:/config/*.properties</value>
            </list>
        </property>
    </bean>
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="properties" ref="configProperties" />
    </bean>

第二步:

建立配置文件内容:

例如:userPageSize=5

第三步:

在Controller中使用注解获得配置项内容:

 
1
2
@Value("#{configProperties['userPageSize']}")
private String userPageSize;

第四步:

后面的代码就可以使用userPageSize这个私有变量了,这个字符串的值就是我们配置文件中配置的5.

第四种:

实际项目中,通常将一些可配置的定制信息放到属性文件中(如数据库连接信息,邮件发送配置信息等),便于统一配置管理。例中将需配置的属性信息放在属性文件/WEB-INF/configInfo.properties中。 

其中部分配置信息(邮件发送相关): 

#邮件发送的相关配置
email.host = smtp.163.com
email.port = xxx
email.username = xxx
email.password = xxx
email.sendFrom = xxx@163.com



在Spring容器启动时,使用内置bean对属性文件信息进行加载,在bean.xml中添加如下: 

Xml代码
<!-- spring的属性加载器,加载properties文件中的属性 -->
	<bean id="propertyConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location">
			<value>/WEB-INF/configInfo.properties</value>
		</property>
		<property name="fileEncoding" value="utf-8" />
	</bean>



属性信息加载后其中一种使用方式是在其它bean定义中直接根据属性信息的key引用value,如邮件发送器bean的配置如下: 

Xml代码
<!-- 邮件发送 -->
	<bean id="mailSender"
		class="org.springframework.mail.javamail.JavaMailSenderImpl">
		<property name="host">
			<value>${email.host}</value>
		</property>
		<property name="port">
			<value>${email.port}</value>
		</property>
		<property name="username">
			<value>${email.username}</value>
		</property>
		<property name="password">
			<value>${email.password}</value>
		</property>
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">true</prop>
				<prop key="sendFrom">${email.sendFrom}</prop>
			</props>
		</property>
	</bean>



另一种使用方式是在代码中获取配置的属性信息,可定义一个javabean:ConfigInfo.java,利用注解将代码中需要使用的属性信息注入;如属性文件中有如下信息需在代码中获取使用: 

Java代码 
#生成文件的保存路径
file.savePath = D:/test/
#生成文件的备份路径,使用后将对应文件移到该目录
file.backupPath = D:/test bak/



ConfigInfo.java 中对应代码: 

Java代码
@Component("configInfo")
public class ConfigInfo {
    @Value("${file.savePath}")
    private String fileSavePath;

    @Value("${file.backupPath}")
    private String fileBakPath;
        
    public String getFileSavePath() {
        return fileSavePath;
    }

    public String getFileBakPath() {
        return fileBakPath;
    }    
}



业务类bo中使用注解注入ConfigInfo对象: 

Java代码
@Autowired
private ConfigInfo configInfo;



需在bean.xml中添加组件扫描器,用于注解方式的自动注入: 

Xml代码
<context:component-scan base-package="com.my.model" />


(上述包model中包含了ConfigInfo类)。 

通过get方法获取对应的属性信息,优点是代码中使用方便,缺点是如果代码中需用到新的属性信息,需对ConfigInfo.java做相应的添加修改。

原文地址:https://www.cnblogs.com/yanduanduan/p/4882458.html