欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-03-21 10:27:41|栏目:JAVA代码|点击:

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> 

总结

以上就是本文关于Spring中利用配置文件和@value注入属性值代码详解的全部内容,希望对大家有所帮助。有什么问题可以随时留言,小编会及时回复大家的。

上一篇:浅谈springmvc的DispatcherServlet分析

栏    目:JAVA代码

下一篇:Spring Boot Async异步执行任务过程详解

本文标题:Spring中利用配置文件和@value注入属性值代码详解

本文地址:http://www.codeinn.net/misctech/84761.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有