【学习笔记】依赖注入
1.构造器注入
默认是无参构造注入,在bean中,对属性是通过set注入
有参构造注入,有三种方式
都是使用constructor-arg 标签来为有参构造的参数赋值
-
直接通过属性名
<bean id="User" class="pojo.User"> <constructor-arg name="name" value="张三"/> </bean>
-
通过参数的索引
<bean id="User" class="pojo.User"> <constructor-arg index="0" value="李四"/> </bean>
-
通过参数的类型
<bean id="User" class="pojo.User"> <constructor-arg type="java.lang.String" value="王五"/> </bean>
2.Set方式注入
依赖注入的本质也就是set注入
-
依赖:bean对象的创建依赖于容器
-
注入:bean对象中的属性由容器来注入
【测试环境】
-
引用对象
package com.wang.pojo; public class Address { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
-
真实对象(引用对象、数组、list集合、map集合、set集合、properties、null)
package com.wang.pojo; import java.util.*; public class Student { private String name; private Address address; private String[] books; private List<String> hobbys; private Map<String,String> card; private Set<String> games; private Properties info; private String pet; public String getName() { return name; } public void setName(String name) { this.name = name; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String[] getBooks() { return books; } public void setBooks(String[] books) { this.books = books; } public List<String> getHobbys() { return hobbys; } public void setHobbys(List<String> hobbys) { this.hobbys = hobbys; } public Map<String, String> getCard() { return card; } public void setCard(Map<String, String> card) { this.card = card; } public Set<String> getGames() { return games; } public void setGames(Set<String> games) { this.games = games; } public Properties getInfo() { return info; } public void setInfo(Properties info) { this.info = info; } public String getPet() { return pet; } public void setPet(String pet) { this.pet = pet; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", address=" + address + ", books=" + Arrays.toString(books) + ", hobbys=" + hobbys + ", card=" + card + ", games=" + games + ", info=" + info + ", pet='" + pet + '\'' + '}'; } }
我们要做的就是将Student中的所有属性都注入值,然后通过toString打印出来
配置文件xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.wang.pojo.Student">
<!--普通属性注入值-->
<property name="name" value="张三"/>
<!--引用对象属性注入值-->
<property name="address" ref="address"/>
<!--数组属性注入值-->
<property name="books">
<array>
<value>三国演义</value>
<value>水浒传</value>
<value>西游记</value>
<value>红楼梦</value>
</array>
</property>
<!--List集合属性注入值-->
<property name="hobbys">
<list>
<value>吃饭</value>
<value>睡觉</value>
</list>
</property>
<!--Map集合属性注入值-->
<property name="card">
<map>
<entry key="身份证" value="1111110000000111000"/>
<entry key="学生证" value="2023213213"/>
</map>
</property>
<!--Set集合属性注入值-->
<property name="games">
<set>
<value>原</value>
<value>和</value>
</set>
</property>
<!--Properties属性注入值-->
<property name="info">
<props>
<prop key="性别">男</prop>
<prop key="学号">11110000</prop>
<prop key="籍贯">中国</prop>
</props>
</property>
<!--null属性注入值-->
<property name="pet">
<null/>
</property>
</bean>
<bean id="address" class="com.wang.pojo.Address">
<property name="address" value="山东"/>
</bean>
</beans>
3.p命名空间和c命名空间注入
所谓p命名空间和c命名空间就是一种扩展注入,通过引入xml依赖,可以更加方便的注入值,其本质还是set注入和构造器注入
p命名空间-----set注入
c命名空间-----构造器注入
使用之前需要导入xml依赖
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
举例:
新建一个User类,属性为name和age
package com.wang.pojo;
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = 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 "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
注意点:c命名空间必须要有有参构造,p命名空间必须要有无参构造和set方法
配置文件:
<!--p命名空间-->
<bean id="user" class="com.wang.pojo.User" p:name="张三" p:age="18"/>
<!--c命名空间-->
<bean id="user2" class="com.wang.pojo.User" c:name="李四" c:age="20"/>