spring实例化bean给所有各类型属性依赖注入值、Integer、Date、String、Map、List、Set、Url、File类型的属性注入值

应以一个实体类,有Integer、Date、String、Map<>、List<>、Set<>、Url、File类型的属性
Map<>、List<>、Set<>这些属性注意要导 java.util 的包

package test;

import java.io.File;
import java.net.URL;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Student {
	private Integer age;
	private Date birthday;
	private Integer sno;
	private String username;
	private Map<String,Integer> scores;
	private List<String> lessons;
	private URL url;
	private Set<Integer> numbers;
	private File photo;
	
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public Integer getSno() {
		return sno;
	}
	public void setSno(Integer sno) {
		this.sno = sno;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public Map<String, Integer> getScores() {
		return scores;
	}
	public void setScores(Map<String, Integer> scores) {
		this.scores = scores;
	}
	public List<String> getLessons() {
		return lessons;
	}
	public void setLessons(List<String> lessons) {
		this.lessons = lessons;
	}
	public URL getUrl() {
		return url;
	}
	public void setUrl(URL url) {
		this.url = url;
	}
	public Set<Integer> getNumbers() {
		return numbers;
	}
	public void setNumbers(Set<Integer> numbers) {
		this.numbers = numbers;
	}
	public File getPhoto() {
		return photo;
	}
	public void setPhoto(File photo) {
		this.photo = photo;
	}
	@Override
	public String toString() {
		return "Student [age=" + age + ", birthday=" + birthday + ", sno=" + sno + ", username=" + username
				+ ", scores="  + scores+", lessons=" + lessons.toString() + ", url=" + url + ", numbers=" + numbers.toString() + ", photo="
				+ photo + "]";
	}
}

配置bean.xml文件,在bean中给这些属性注入值

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd">

	<bean id="student" class="test.Student">
		<!-- 给Integer的类型属性注入值 -->
		<property name="age" value="13"></property>
		<!-- 给Date的类型属性注入值 -->
		<property name="birthday" value="2010/12/13"></property>
		<!-- 给Integer的类型属性注入值 -->
		<property name="sno" value="100"></property>
		<!-- 给String的类型属性注入值 -->
		<property name="username" value="hongtong"></property>
		 <!-- 给Map的类型属性注入值 -->
		 <property name="scores">
			<map>
				<entry key="yuwen" value="67"></entry>
				<entry key="shuxue" value="97"></entry>
			</map>
		</property> 
		<!-- 给List的类型属性注入值 -->
		<property name="lessons">
			<list>
				<value>maths</value>
				<value>chinese</value>
			</list>
		</property>
		
		<property name="url" value="http://www.baidu.com"></property>
		<!-- 给Set的类型属性注入值 -->
		<property name="numbers">
			<list>
			<value>23</value>
			<value>34</value>
			</list>
		</property>
		<property name="photo" value="d:\\bg.jpg"></property> 
	</bean>
</beans>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

code_mo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值