Spring 引入properties配置文件的方式

29 篇文章 0 订阅
12 篇文章 0 订阅

spring注入常量的方式,可以直接在java代码中使用

*.properties配置文件是在开发中常用的配置文件方式,一些需要经常变动的参数会以键值对的方式放在其中,然后可以在*.xml文件和java代码中直接引用,避免出现硬编码导致违反开闭原则。而咋Spring中直接引用properties配置文件有以下几种方式,在此记录,权作日记。


方法一:采用配置文件<util:**>标签方式来配置
可以对set、map、list、properties文件等类型的数据进行配置,以下以properties文件为例说明使用方法步骤:
1、applicationContext.xml中添加
<beans 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.1.xsd">  
</beans>


2、使用标签引入properties文件路径

<util:properties id="configEnv" location="config/env.properties" />

3、在代码中注入

@Value("#configEnv['db_key']")
private String db_key;


Tips:
1、一定要使用spring2.0以上版本方才支持此功能
2、db_key 可以是Boolean、int、Double等其他基本类型
方法二:使用PropertyPlaceholderConfigurer方式完成配置文件载入
<bean id="appProperty"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:app.properties</value>
</array>
</property>
</bean>


然后使用@Value("${db_key}")直接注入使用
Tips:
1、此种方式还可以用标签<context:property-placeholder>来代替 使用方式:
<context:property-placeholder
            ignore-resource-not-found="true"
            location="classpath*:/application.properties,
                      classpath*:/db.properties" />


location 可以一次引入多个文件
2、变量的前缀就此丢失了,因此需要在变量的key中加入前缀,以保证不会重名,一般格式:db.conn.maxConns = 30
3、此种方式注入通用性更好,可以在Spring配置文件中直接使用。如:
<bean id="dataSource" class="">
<property id="username" value="${db.oracle.username}"></property>
<property id="password" value="${db.oracle.password}"></property>
</bean>


4、还可以不通过spring配置文件而直接在程序中加载properties文件的方法,如:
context = new ClassPathXmlApplicationContext("applicationContext.xml");


PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource("classpath*:/config/util.properties"));
cfg.setBeanFactory(context);



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于spring配置文件加载properties文件,可以使用以下几种方式: 1. 使用PropertyPlaceholderConfigurer属性占位符 可以在配置文件中使用${}占位符来引用属性值,同时需要在配置文件引入对应的*.properties文件。在Spring容器启动时,会通过PropertyPlaceholderConfigurer将properties配置文件中的键值对装载到Spring的环境变量中,供${}占位符使用。 例如: ``` <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:/config/app.properties</value> </list> </property> </bean> <bean id="user" class="com.example.User"> <property name="name" value="${user.name}"/> <property name="age" value="${user.age}"/> </bean> ``` 在上面的例子中,将classpath:/config/app.properties中的键值对装载到Spring的环境变量中,供${}占位符使用。 2. 使用util命名空间的PropertiesFactoryBean 可以在Spring配置文件中使用util命名空间的PropertiesFactoryBean来装载properties文件中的属性,并且使用${}占位符引用这些属性值。 例如: ``` <util:properties id="appConfig" location="classpath:/config/app.properties"/> <bean id="user" class="com.example.User"> <property name="name" value="${user.name}" /> <property name="age" value="${user.age}" /> </bean> ``` 在上面的例子中,通过util:properties装载classpath:/config/app.properties中的属性。在User bean中使用${}占位符引用属性值。 希望这些方法能够帮到您!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值