文章目录
6.1 概念
-
依赖注入(Dependency Injection,DI)
-
依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 .
-
注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配
6.2 构造器注入
实体类User
package com.jl.pojo;
public class User {
private String name;
public User() {
}
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("name=" + name);
}
}
构造器注入
<bean id="user" class="com.jl.pojo.User">
<constructor-arg name="name" value="哈哈哈哈哈哈"/>
</bean>
6.2 Set 注入 (重点)
实体类Address
package com.jl.pojo;
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
实体类Student
package com.jl.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 String wife;
private Properties info;
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 String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address=" + address +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + info +
'}';
}
}
6.2.1 常量注入
beans.xml
<bean id="student" class="com.jl.pojo.Student">
<!-- 普通值注入 value -->
<property name="name" value="张三"/>
</bean>
Test测试
public class Test1 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.getName());
}
6.2.2 Bean注入
注意点:这里的值是一个引用,用ref
<bean id="address" class="com.jl.pojo.Address">
<property name="address" value="天津"/>
</bean>
<bean id="student" class="com.jl.pojo.Student">
<property name="address" ref="address"/>
</bean>
6.2.3 数组注入
<bean id="student" class="com.jl.pojo.Student">
<property name="books">
<array>
<value>西游记</value>
<value>红楼梦</value>
<value>水浒传</value>
</array>
</property>
</bean>
6.2.4 List注入
<!-- list注入 -->
<property name="hobbys">
<list>
<value>听歌</value>
<value>玩</value>
<value>吃</value>
<value>乐</value>
</list>
</property>
6.2.5 Map注入
<!-- Map注入 -->
<property name="card">
<map>
<entry key="身份证" value="123456"/>
<entry key="银行卡" value="1234567890"/>
</map>
</property>
6.2.6 set注入
<!-- Set注入 -->
<property name="games">
<set>
<value>LOL</value>
<value>BOB</value>
</set>
</property>
6.2.7 Null注入
<!-- null注入 -->
<property name="wife">
<null/>
</property>
6.2.8 Properties注入
<!-- Properties -->
<property name="info">
<props>
<prop key="学号">1950410914</prop>
<prop key="性别">男</prop>
</props>
</property>
测试
public class Test1 {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
结果:
Student{
name='张三',
address=Address{address='天津'},
books=[红楼梦, 西游记, 水浒传, 三国演义],
hobbys=[听歌, 玩, 吃, 乐],
card={身份证=123456, 银行卡=1234567890},
games=[LOL, BOB],
wife='null',
info={学号=1950410914, 性别=男}
}
6.3 p命名和c命名注入
- p命名注入:对应Set注入
- c命名注入:对应构造器注入
创建一个实体类User 【注意:这里没有有参构造器!】
package com.jl.pojo;
public class User {
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 "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
P命名注入:需要在头文件中加入约束文件
导入约束 : xmlns:p="http://www.springframework.org/schema/p"
<!--P(属性: properties)命名空间 , 属性依然要设置set方法-->
<bean id="user" class="com.jl.pojo.User" p:name="李四" p:age="18"/>
C命名注入 (实体类需要有有参构造方法)
导入约束 : xmlns:c="http://www.springframework.org/schema/c"
<!-- C命名注入,通过构造器注入, construct-args -->
<bean id="user2" class="com.jl.pojo.User" c:age="18" c:name="张三" />
注意:
p命名和c命名空间不能直接使用,需要 导入xml约束!
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"