Spring-注入properties属性文件

Spring-注入properties属性文件

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));
        }
    }
}

bootstrap.yml文件相当于项目启动时的引导文件,内容相对固定。application.yml文件是微服务的一些常规配置参数,变化比较频繁。bootstrap.yml文件也是Spring Boot的默认配置文件,而且其加载的时间相比于application.yml更早

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值