Spring_3_注入基本类型的值和spring表达式

/**
 * 注入基本类型的值
 * @author Administrator
 *
 */
public class ValueBean {
	//基本类型
	private String name;
	private int age;
	//集合类型
	private List<String> city;
	private Set<String> interest;
	private Map<String, Double> score;
	private Properties db;
	public ValueBean() {
		System.out.println("ValueBean()");
	}
	//对应的set方法
	public void setName(String name) {
		this.name = name;
	}
	public void setDb(Properties db) {
		this.db = db;
	}
	public void setScore(Map<String, Double> score) {
		this.score = score;
	}
	public void setInterest(Set<String> interest) {
		this.interest = interest;
	}

	public void setCity(List<String> city) {
		this.city = city;
	}
	public void setAge(int age) {
		this.age = age;
	}
	@Override
	public String toString() {
		return "ValueBean [name=" + name + ", age=" + age + ", city=" + city + ", interest=" + interest + ", score="
				+ score + ", db=" + db + "]";
	}

	public String getName() {
		return name;
	}

	public int getAge() {
		return age;
	}

	public List<String> getCity() {
		return city;
	}

	public Set<String> getInterest() {
		return interest;
	}

	public Map<String, Double> getScore() {
		return score;
	}

	public Properties getDb() {
		return db;
	}	
}

配置文件,在bean元素内添加property元素

使用value属性来注入,spring容器会帮我们做一些类型的转换工作,比如将字符串转换成数字。

<!-- 1.注入基本类型的值 -->
	<!-- 属性name=实例变量 -->
	<bean name="valuebean1" class="value.ValueBean">
		<!-- name=属性名  value=属性值 -->
		<property name="name" value="史迪奇"/>
		<property name="age" value="12"/>
		<!-- List -->
		<property name="city">
			<list>
				<value>北京</value>
				<value>上海</value>
				<value>广州</value>
				<value>深圳</value>
				<value>上海</value>
			</list>
		</property>
		<!-- Set -->
		<property name="interest">
			<!-- Set不允许重复集,重复的元素则覆盖 -->
			<set>
				<value>打篮球</value>
				<value>看电影</value>
				<value>钓鱼</value>
				<value>听音乐</value>
				<value>看电影</value>
			</set>
		</property>
		<!-- Map -->
		<property name="score">
			<map>
				<entry key="英语" value="90" />
				<entry key="math" value="80.5"/>
			</map>
		</property>
		<!-- Properties -->
		<property name="db">
			<props>
				<prop key="username">Administrator</prop>
				<prop key="password">123456</prop>
			</props>
		</property>
	</bean>

采用引用的方式注入集合类型的值

1.将集合类型的值先配置成一个bean。

2.再将这个bean注入到对应的bean里面。

<!-- 2.采用引用的方式注入集合类型的值 -->
	<!-- 将集合类型的值配置成一个bean
			util:list 表示使用的是util命名空间下的list元素 ,
			命名空间:为了区分同名的元素面添加前缀(限定)。表示使用util这个命名空间下的list元素 -->
	<util:list id="cityBean">
		<value>南宁</value>
		<value>柳州</value>
		<value>桂林</value>
	</util:list>
	<util:set id="interestBean">
		<value>打篮球</value>
		<value>看电影</value>
		<value>钓鱼</value>
		<value>听音乐</value>
		<value>看电影</value>
	</util:set>
	<util:map id="scoreBean">
		<entry key="语文" value="90"/>
		<entry key="数学" value="95"/>
		<entry key="英语" value="80"/>
	</util:map>
	<util:properties id="dbBean">
		<prop key="username">admin</prop>
		<prop key="password">12345</prop>
	</util:properties>
	<!-- 配置成一个bean,采用引用的方式注入集合类型的值 -->		
	<bean name="valuebean2" class="value.ValueBean">
		<!-- 注入属性 -->
		<property name="city" ref="cityBean"/>
		<property name="interest" ref="interestBean"/>
		<property name="score" ref="scoreBean"/>
		<property name="db" ref="dbBean"/>
	</bean>		
		
	<!-- 3.读取.Properties文件 -->	
	<!-- 
		location属性:指定属性文件的位置。
		classpath:表示让容器依据类路径去找属性文件
		spring容器会读取指定位置的文件的内容,将这些内容存到Properties对象里面
	 -->		
	<util:properties id="config" location="classpath:config.properties"/>

spring表达式

/**
 * spring表达式
 * @author Administrator
 *
 */
public class SpringELBean {
	private String name;
	private String city;
	private double score;
	private String pageSize;
	
	public SpringELBean() {
		System.out.println("SpringELBean()");
	}
	public void setName(String name) {
		this.name = name;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public void setScore(double score) {
		this.score = score;
	}
	public void setPageSize(String pageSize) {
		this.pageSize = pageSize;
	}
	@Override
	public String toString() {
		return "SpringELBean [name=" + name + ", city=" + city + ", score=" + score + ", pageSize=" + pageSize + "]";
	}
}
<!-- 4.使用spring表达式可以读取其它的bean的属性 #{bean的id.属性名},底层调用getXXX方法 -->
	<bean id="sprEL" class="value.SpringELBean">
		<property name="name" value="#{valuebean1.name}"/>
		<property name="city" value="#{valuebean1.city[2]}"/>
		<!-- key为汉字时在中括号里使用单引号括起来,score['英语'] -->
		<property name="score" value="#{valuebean1.score.math}"></property>
		<property name="pageSize" value="#{config.pageSize}"/>
	</bean>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值