spring为对象(bean)属性赋值(注入)

实体类

public class People{
	private int id;
	private String name;
	private Set<String> sets;	//	集合
	public People(){
		//类创建时默认走类的无参构造方法,仅用于测试,无实际意义
		System.out.println("默认构造方法");
	}
	public int getId(){
		return id;
	}
	public void setId(int id){
		System.out.println(“set方法”);
		this.id= id;
	}
	public String getName(){
		return name;
	}
	public void setName(String name){
		this.name = name;
	}
	public Set<String> getSets(){
		return sets;
	}
	public void setSets(Set<String> sets){
		this.sets= sets;
	}
}

通过构造方法进行赋值

  • 在<bean></bean>标签中添加<constructor-arg></constructor-arg>标签
	例如:
	public People(int id, String name){
		this.id  = id;
		this.name = name;
		System.out.println("有参构造方法1");
	}
	public People(String name, int id ){
		this.id  = id;
		this.name = name;
		System.out.println("有参构造方法2");
	}
	注意:当有多个有参构造方法符合情况时,spring框架找从上到下排序的最后一个进行赋值;即2在1后(如上),执行第2个;若1在2后执行第1个;
	
	
	配置文件中:
	<bean id="peo" class="People">
		<constructor-arg index="0" value="1"></constructor-arg>
		<constructor-arg index="1" value="张三"></constructor-arg>
		
	或	<constructor-arg index="id" value="1"></constructor-arg>
		<constructor-arg index="name" value="张三"></constructor-arg>
		
		<!--这里type中,int和Integer不能互转,属于不同类型-->
	或	<constructor-arg type="int" value="1"></constructor-arg>
		<constructor-arg type="java.lang.String" value="张三"></constructor-arg>
	</bean>
	
	
	关于<constructor-arg>的参数解释:
		index:形参的索引值,从0开始;
		name:形参的名称,根据名称找到第几个参数;
		type:形参类型(区分开关键字和封装类,int和Integer);
		ref:参数是引用另外一个bean;
		value:参数是基本数据类型或字符串等;
	注意:(index,name,type用来控制用哪个构造方法,当有多个构造方法都匹配时,用顺序排在最后的那一个构造方法,index,name,type可以出现一个,也可以出现多个)

设置注入(通过set方法)

  • 在<bean></bean>标签中添加<property></property>标签,走实体类的set方法。
<bean id="peo" class="People">
	<!--如果属性是普通数据类型或字符串-->
	<property name="id" value="123"></property>
	
或	<property name="name">  
		 <value>张三</value>   
	</property> 
</bean>

属性如果是set时:

<property name="sets">
	<set> 
	  	<value>1</value>
	  	<value>2</value> 
	</set>
 </property>

属性如果是map时:

<property name=“”> 
	<map>
	 	<entry  key=“” value=“”> </entry> 
	 </map>
 </property>

属性如果是数组时:

<property>
	<array>
		<value></value>
	</array>
</property>

属性是property(属性)类型时:

Property类型为一个demo.properties文件,其中内容为a=b,b=c…等键值对
<property name="prop">
	<props>
		<prop key="a">b</prop>
	</props>
</property>

属性如果是List<?>类型时:

<property name="">
	<list>
		<value></value>
	</list>
</property>

若list只有一个值:
<property name="" value="一个值"></property>

此外还有很多类型,Alt+/即可查看

设置参数进行自动注入的方法

bean的scope属性

  • 作用:控制对象有效范围(单例-有且只有一个对象,多例等);
  • 为单例时:
<bean id="teacher"  scope=”singleton”>
Teacher1 = getBean(“teacher”);
Teacher2 = getBean(“teacher”);
System.out.println(teacher1==teacher2)时为true;
说明teacher只实例化一次
  • <bean/>标签对应的对象默认是单例的,无论获取多少次,都是一个对象;

  • scope取值:

(1) singleton默认值,单例;
(2) prototype多例,每次获取重新实例化;
(3) request每次请求重新实例化;
(4) session每个会话对象内,对象是单例的;
(5) application在application对象内是单例的;

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值