Spring的bean的属性注入

bean的属性注入3中方式。
1.0 接口注入。
2.0 构造函数注入。
3.0 setter方法的注入。

spring支持后面的2种注入
示范如下,设计一个Person类型,和一个Student类型。

Person类,采用setter方法注入属性。


public class Person {
	
	private String name;
	private int age;
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + "]";
	}
		
}

Student类, 采用构造函数注入属性值。


public class Student {
	
	private Person person;
	
	private String id;
	
	public Student(Person person,String id)
	{
		this.person=person;
		this.id=id;
		
		System.out.println("-construct-------"+id+"student被实列化"+person);
	}

	
	public void setPerson(Person person) {
		this.person = person;
	}


	public void setId(String id) {
		this.id = id;
	}
	
	public String getId() {
		
		return id;
	}
}

xml的配置如下

<bean id="person" class="Person">
<property name="name" value="dflx"></property>
<property name="age" value="999"></property>
</bean>

<bean id="student" class="Student">
<constructor-arg index="0" ref="person"></constructor-arg>
<constructor-arg index="1" type="java.lang.String" value="123456"></constructor-arg>
</bean>

在main类中,进行相关的测试

	ApplicationContext app=new ClassPathXmlApplicationContext("helloMessage.xml");
		Student student=(Student) app.getBean("student");
		System.out.println("////////////////");
		System.out.println(student.getId());

###结果如下所示

-construct-------123456student被实列化Person [name=dflx, age=999]
////////////////
123456

spring集合类型的属性注入
list和集合的注入 设置一个测试类,来展示注入。

ublic class TestDem {
	
	
	private ArrayList<String> list;
	
	private String[] str;
	
	public void setList(ArrayList<String> list) {
		this.list = list;
	}
	
	public void setStr(String[] str) {
		this.str = str;
	}
	
	
	@Override
	public String toString() {
		return "TestDem [list=" + list + ", str=" + Arrays.toString(str) + "]";
	}

	@Test
	public void test()
	{
		ApplicationContext app=new ClassPathXmlApplicationContext("helloMessage.xml");
		TestDem test=(TestDem) app.getBean("test");
		
		System.out.println(test);
		
	}
	

}

相关的xml配置如下

<bean id="test" class="TestDem" >
<property name="list">
<list>
<value>alice</value>
<value>jack</value>
</list>
</property>

<property name="str">
<list>
<value>11</value>
<value>22</value>
<value>33</value>
</list>
</property>
</bean>

###最后的结果如下

TestDem [list=[alice, jack], str=[11, 22, 33]]

Set的注入

xml的相关配置如下

<bean id="test" class="TestDem" >
<property name="set">
<set>
<value>111</value>
<value>222</value>
</set>
</property>

</bean>

###结果如下

TestDem [set=[111, 222]]

集合属性map的注入

<bean id="test" class="TestDem" >
<property name="map">
<map>
<entry key="1" value="alice"></entry>
<entry key="2" value="marry"></entry>
</map>
</property>
</bean>

结果如下

TestDem [map={1=alice, 2=marry}]

Properties的属性注入

其xml的配置如下

<bean id="test" class="TestDem" >
<property name="proper">

<props>
<prop key="东风">冷雪</prop>
<prop key="风">雨</prop>
</props>

</property>
</bean>
</beans>

###结果如下

TestDem [proper={东风=冷雪, 风=雨}]

##和小伙伴,建了一个公众号,在摸索中,欢迎关注。搜索公众号:东风冷雪,英文:satan_master ,现在探索中。无所不有,包括生活,学习,娱乐。 三大板块。

《Spring的bean的属性注入》

点赞