在spring中bean的属性注入有两种
1. 构造器注入
创建实体类,提供get,set方法及满参构造
package com.itcast.di;
public class Car {
private String name;
private Double price;
public Car(String name, Double price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
在applicationContext.xml中添加属性
<bean id="car" class="com.itcast.di.Car">
<!--<constructor-arg>是指可以通过它的构造方法给属性赋值,index="位置" type="类型" value="值"-->
<constructor-arg index="0" type="java.lang.String" value="宝马"></constructor-arg>
<constructor-arg index="1" type="java.lang.Double" value="1000000"></constructor-arg>
</bean>
测试:
package com.itcast.di;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class CarTest {
@Test
public void test1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) applicationContext.getBean("car");
System.out.println(car.getName()+":"+car.getPrice());
}
}
输出:
总结:构造方法注入在实际开发过程中应用的并不是非常多
2. Setter方法注入
创建实体类,提供get,set方法及无参构造
添加无参构造
public Car(){
super();
}
在applicationContext.xml中添加属性
<!--使用setter方法注入-->
<bean id="car1" class="com.itcast.di.Car">
<property name="name" value="奔驰"></property>
<property name="price" value="2000000"></property>
</bean>
测试:
@Test
public void Test2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) applicationContext.getBean("car1");
System.out.println(car.getName()+":"+car.getPrice());
}
输出:
扩展:属性中的ref属性
我们可以使用ref属性引用其它的bean对象,完成bean之间的注入
编写Person类,引入Car属性,提供get,set方法
package com.itcast.di;
public class Person {
private String personName;
private Car car;
public String getPersonName() {
return personName;
}
public void setPersonName(String person) {
this.personName = person;
}
public Car getCar() {
return car;
}
public void setCar(Car car) {
this.car = car;
}
}
在applicationContext.xml中添加属性
在中添加ref=“car1”,car1为测试setter注入方式bean的id
<bean id="person" class="com.itcast.di.Person" >
<property name="person" value="张三"></property>
<property name="car" ref="car1"></property>
</bean>
测试:
@Test
public void test3(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person) applicationContext.getBean("person");
System.out.println(person.getPersonName()+":"+person.getCar().getName()+":"+person.getCar().getPrice());
}
打印:
集合属性的注入
在spring中对于集合属性,可以使用专门的标签来完成注入例如:list set map properties等集合元素来完成集合属性注入.
创建集合实体类,提供getter,setter方法
package com.itcast.di;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CollectionDemo {
private List list;
private Set set;
private Map map;
private Properties properties;
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
}
在applicationContext.xml中添加属性
<!--集合属性注入-->
<bean id="CollectionDemo" class="com.itcast.di.CollectionDemo">
<property name="list">
<list>
<value>张三</value>
<value>100</value>
<ref bean="car1"></ref>
</list>
</property>
<property name="set">
<set>
<value>10</value>
<value>李四</value>
<value>20</value>
</set>
</property>
<property name="map">
<map>
<entry key="username" value="王五"></entry>
<entry key-ref="person" value-ref="car1"></entry>
</map>
</property>
<property name="properties">
<props>
<prop key="company">baidu</prop>
<prop key="price">100000</prop>
</props>
</property>
</bean>
测试:
package com.itcast.di;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public class CarTest {
@Test
public void test4(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionDemo collectionDemo = (CollectionDemo)applicationContext.getBean("CollectionDemo");
List list = collectionDemo.getList();
System.out.println(list);
}
@Test
public void test5(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionDemo collectionDemo = (CollectionDemo)applicationContext.getBean("CollectionDemo");
Set set = collectionDemo.getSet();
System.out.println(set);
}
@Test
public void test6(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionDemo collectionDemo = (CollectionDemo)applicationContext.getBean("CollectionDemo");
Map map = collectionDemo.getMap();
System.out.println(map);
}
@Test
public void test7(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CollectionDemo collectionDemo = (CollectionDemo)applicationContext.getBean("CollectionDemo");
Properties properties = collectionDemo.getProperties();
System.out.println(properties);
}
}