什么是Bean属性的注入?就是对 一个对象的属性赋值`
构造方法注入
@Data
@AllArgsConstructor
@NoArgsConstructor
//必须有参无参构造方法,set get方法 toString方法
public class Car {
private Integer id;
private String name;
private Double price;
}
applicationContext.xml配置文件
<bean id="car" class="pojo.Car">
<property name="id" value="100"/>
<property name="name" value="山地车"/>
<property name="price" value="1000"/>
</bean>
测试
@Test
public void carTest() {
//获取配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//传统写法
Car car = new Car(1008,"单车",2d);
System.out.println(car);
//构造器属性注入
Car car2 = applicationContext.getBean("car", Car.class);
System.out.println(car2);
}
set方法注入
@Data
@AllArgsConstructor
@NoArgsConstructor
//必须有参无参构造方法,set get方法 toString方法
public class Car {
private Integer id;
private String name;
private Double price;
}
再创建一个Person对象
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private Integer id;
private String name;
private Car car;
}
applicationContext.xml配置文件
<bean id="car" class="pojo.Car">
<property name="id" value="100"/>
<property name="name" value="山地车"/>
<property name="price" value="1000"/>
</bean>
<bean id="person" class="pojo.Person">
<property name="id" value="100"/>
<property name="name" value="张三"/>
<!--这里是引的car的bean对象-->
<property name="car" ref="car"/>
</bean>
测试
@Test
public void login() {
//获取配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//set属性注入
Person person = applicationContext.getBean("person", Person.class);
System.out.println(person);
}
P命名空间注入
p名称空间出现的目地为了简化以上两种注入方式
引入p名称空间约束头
<beans xmlns="http://www.springframework.org/schema/beans"
<!--添加p约束头-->
xmlns:p="http://www.springframework.org/schema/p"
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">
applicationContext.xml配置文件
<!--p名称空间属性注入-->
<bean id="person2" class="pojo.Person" p:id="99" p:name="rose" p:car-ref="car" />
测试
@Test
public void login() {
//获取配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//P命名空间注入
Person person = applicationContext.getBean("person2", Person.class);
System.out.println(person);
}
集合类型属性注入
@Data
public class ConllectionBean {
private List<String> list;
private Set<Integer> set;
private Map<Integer,String> map;
private Properties properties;
}
applicationContext.xml配置文件
<bean id="conllectionBean" class="pojo.ConllectionBean">
<!--list-->
<property name="list">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
<!--set-->
<property name="set">
<set>
<value>11</value>
<value>22</value>
<value>33</value>
</set>
</property>
<!--map-->
<property name="map">
<map>
<entry key="11" value="aa"/>
<entry key="22" value="bb"/>
<entry key="33" value="cc"/>
</map>
</property>
<!--properties-->
<property name="properties">
<props>
<prop key="a">aa</prop>
<prop key="b">bb</prop>
<prop key="c">cc</prop>
</props>
</property>
</bean>
测试
@Test
public void login() {
//获取配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//集合类型属性注入
ConllectionBean conllectionBean = applicationContext.getBean("conllectionBean", ConllectionBean.class);
System.out.println(conllectionBean);
}