Spring使用SpEL表达式读取properties配置文件的两种方式

本文介绍了两种在Spring中使用SpEL表达式读取properties配置文件的方法。第一种直接在配置文件中定义属性,然后通过SpEL注入;第二种是创建一个Java Bean来读取配置,并通过注解扫描加载。另外,还提到了一种非SpEL的加载方式,需要注意配置文件格式的调整以避免解析错误。
摘要由CSDN通过智能技术生成

第一种:

config.properties:

index.version=v1

spring配置文件,加载config.properties:

	<!-- 获取properties中的值 -->
	<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="locations">
			<list>
				<value>classpath:config.properties</value>
			</list>
		</property>
	</bean>

	<!-- Spring的动态变量,能在bean中直接调用 -->
	<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="properties" ref="configProperties" />
	</bean>

注入使用:

	@Value("#{configProperties['index.version']}")
	public String ve
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值