一.构造方法注入
通过构造方法注入Bean的属性值或依赖的对象,它保证了Bean实例在实例化后就可以使用
构造器注入在<constructor-arg>元素里声明的属性
1.新建类User,并Generate toString()方法
public class User {
private String name;
private Integer age;
public User(String name,Integer age){
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
2.在applicationContext.xml里面编写:
<!--Bean的构造方法的属性注入-->
<bean id="user" class="com.imooc.ioc.demo4.User">
<constructor-arg name="name" value="张三" />
<constructor-arg name="age" value="23"/>
</bean>
3.新建测试类SpringDemo4
@Test
public void demo1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User)applicationContext.getBean("user");
System.out.println(user);
}
二.set方法属性注入
1.新建类User,并setter,getter方法,Generate toString()方法
public class Person {
private String name;
private Integer age;
private Cat cat;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Cat getCat() {
return cat;
}
public void setCat(Cat cat) {
this.cat = cat;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", cat=" + cat +
'}';
}
}
2.在applicationContext.xml里面编写:
<!--Bean的set方法的属性注入->
<bean id="person" class="com.imooc.ioc.demo4.Person">
<property name="name" value="李四"/>
<property name="age" value="32"/>
</bean>
3.新建测试类SpringDemo4
@Test
public void demo2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = (Person)applicationContext.getBean("person");
System.out.println(person);
}
三.p名称空间属性注入
使用p命名空间,为了简化XML文件配置,Spring从2.5开始引入一个新的p名称空间
P:<属性名>="xxx"引入常量值
p:<属性名>-ref="xxx"引用其他Bean对象
1.在applicationContext.xml里面引入p
xmlns:p="http://www.springframework.org/schema/p"
<!--Bean的p名称空间的属性注入-->
<bean p:cat-ref="cat" p:age="34" p:name="大黄" class="com.imooc.ioc.demo4.Person" id="person"/>
<bean p:name="小黄" class="com.imooc.ioc.demo4.Cat" id="cat"/>
四.SpEL属性注入
SqEL:spring expression language , spring 表达式语言 ,对依赖注入进行简化
语法:#{表达式}
<bean id="" value"#{表达式}">
如:
#{ ‘hello’ } :使用字符串
#{ beanId} :使用另一个bean
#{beanId.content.toUpperCase()} :使用指定名属性,并使用方法
#{ T(java.lang.Math).PI} :使用静态字段方法
1.新建类Category ,得到getter方法,Generate toString()方法
public class Category {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Category{" +
"name='" + name + '\'' +
'}';
}
}
2.新建类Product ,并setter,getter方法,Generate toString()方法
public class Product {
private String name;
private Double price;
private Category category;
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;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Override
public String toString() {
return "Product{" +
"name='" + name + '\'' +
", price=" + price +
", category=" + category +
'}';
}
}
3.在applicationContext.xml里面编写
<bean class="com.imooc.ioc.demo4.Category" id="category">
<property name="name" value="#{'服装'}"/>
</bean>
<bean class="com.imooc.ioc.demo4.Product" id="product">
<property name="name" value="#{'男装'}"/>
<property name="price" value="#{productInfo.calculatePrice()}"/>
<property name="category" value="#{category}"/>
</bean>
4.新建测试类SpringDemo4
@Test
public void demo3(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Product product = (Product)applicationContext.getBean("product");
System.out.println(product);
}
五.复杂类型属性注入
1.新建CollectionBean类,getter和setter方法,generate toString()方法:
public class CollectionBean {
private String[] arrs; //数组类型
private List<String> list; //List集合类型
private Set<String> set; //Set集合类型
private Map<String,Integer> map; //Map集合类型
private Properties properties; //属性类型
public String[] getArrs() {
return arrs;
}
public void setArrs(String[] arrs) {
this.arrs = arrs;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Map<String, Integer> getMap() {
return map;
}
public void setMap(Map<String, Integer> map) {
this.map = map;
}
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@Override
public String toString() {
return "CollectionBean{" +
"arrs=" + Arrays.toString(arrs) +
", list=" + list +
", set=" + set +
", map=" + map +
", properties=" + properties +
'}';
}
}
2.新建测试demo3:
@Test
public void demo3(){
//创建Spring的工厂
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂获得类
CollectionBean collectionBean =(CollectionBean) applicationContext.getBean("collectionBean");
System.out.println(collectionBean);
}
3.五种注入方式写在applicationContext.xml里面,写在<bean>
标签里面:
<bean class="com.imooc.ioc.demo1.CollectionBean" id="collectionBean"></bean>
1)数组类型属性注入
<!--数组类型-->
<property name="arrs">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
<value>ddd</value>
</list>
</property>
2)List集合类型属性注入
<!--List集合的属性注入-->
<property name="list">
<list>
<value>111</value>
<value>222</value>
<value>333</value>
</list>
</property>
3)Set集合类型属性注入
<!--Set集合的属性注入-->
<property name="set">
<list>
<value>ddd</value>
<value>eee</value>
<value>fff</value>
</list>
</property>
4)Map集合类型属性注入
<!--Map集合的属性注入-->
<property name="map">
<map>
<entry value="111" key="aaa"></entry>
<entry value="222" key="bbb"></entry>
<entry value="333" key="ccc"></entry>
</map>
</property>
5)Properties类型属性注入
<!--Properties的属性注入-->
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">1234</prop>
</props>
</property>
6)运行demo3结果:
CollectionBean{arrs=[aaa, bbb, ccc, ddd], list=[111, 222, 333], set=[ddd, eee, fff], map={aaa=111, bbb=222, ccc=333}, properties={password=1234, username=root}}