Spring中利用配置文件和@value注入属性值

欢迎大家关注公众号「JAVA前线」查看更多精彩分享文章,主要包括源码分析、实际应用、架构思维、职场分享、产品思考等等,同时欢迎大家加我微信「java_front」一起交流学习

1 简单属性值注入

package com.xy.test1;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service // 需要被注入属性值的类需要被Spring管理
public class PropertiesService1 {

	// 利用@Value注解,即使没有该属性或者属性文件也不会报错
	
	// @Value输入属性值name,默认值xydefault
	@Value("${name:xydefault}")
	private String name;

	// @Value输入属性值num,默认值-1
	@Value("${num:-1}")
	private Integer num;

	// @Value输入属性值type,默认值-2
	@Value("${type:-2}")
	private Integer type;

	public void getInfo() {
		System.out.println("name:" + name + ",num:" + num + ",type:" + type);
	}
}
#src/main/resource新建文件info.properties
name=xy1
num=101
type=1
<!-- applicationContext.xml文件 -->
<!-- 扫描测试属性包中的类,要注入属性类需要被Spring管理 -->
<context:component-scan base-package="com.xy.test1" />

<!--方法1-->
<!-- <context:property-placeholder location="classpath*:info/info.properties" /> -->

<!--方法2-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="order" value="1" />
	<property name="locations">
		<list>
			<value>classpath:info/info.properties</value>
		</list>
	</property>
</bean>

 

 

2 利用util标签注入复杂属性值

package com.xy.test2;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 该类必须被Spring容器管理属性才可以被注入。利用@Value注解,即使没有该属性或者属性文件也不会报错
 */
@Service
public class PropertiesService2 {

	@Value("#{testPro}")
	private Properties pros;

	@Value("#{testList}")
	private List<String> myList;

	@Value("#{testMap}")
	private Map<Integer, String> myMap;


	public Properties getPros() {
		return pros;
	}

	public void setPros(Properties pros) {
		this.pros = pros;
	}

	public List<String> getMyList() {
		return myList;
	}

	public void setMyList(List<String> myList) {
		this.myList = myList;
	}

	public Map<Integer, String> getMyMap() {
		return myMap;
	}

	public void setMyMap(Map<Integer, String> myMap) {
		this.myMap = myMap;
	}
}
#src/main/resource新建文件info2.properties
name=xy2
num=102
type=2
<!-- applicationContext.xml -->	
<!-- 扫描测试属性包中的类,要注入属性类需要被Spring管理 -->
<context:component-scan base-package="com.xy.test2" />

<!-- properties -->
<util:properties id="testPro" location="classpath:info/info2.properties" />

<!-- list -->
<util:list id="testList" list-class="java.util.ArrayList">
	<value>first</value>
	<value>second</value>
	<value>third</value>
</util:list>

<!-- map -->
<util:map id="testMap" map-class="java.util.HashMap">
	<entry key="1" value="first" />
	<entry key="2" value="second" />
	<entry key="3" value="third" />
</util:map>

欢迎大家关注公众号「JAVA前线」查看更多精彩分享文章,主要包括源码分析、实际应用、架构思维、职场分享、产品思考等等,同时欢迎大家加我微信「java_front」一起交流学习

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值