6.依赖注入
1.构造器注入
2.Set方式注入【重点】
- 依赖注入:set注入
- 依赖:bean对象的创建
- 注入:bean对象中的所有属性,由容器来注入
1.创建两个实体类
Address
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
'}';
}
}
Student实体类
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;
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
", books=" + Arrays.toString(books) +
", hobbys=" + hobbys +
", card=" + card +
", games=" + games +
", wife='" + wife + '\'' +
", info=" + 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;
}
}
2.beans.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="address" class="com.ws.pojo.Address">
<property name="address" value="西安"/>
</bean>
<bean id="student" class="com.ws.pojo.Student">
<property name="name" value="王多多"/>
<!-- bean注入-->
<property name="address" ref="address"/>
<!-- array-->
<property name="books" >
<array>
<value>红楼梦</value>
<value>西游记</value>
<value>水浒传</value>
<value>三国演义</value>
</array>
</property>
<!-- list-->
<property name="hobbys">
<list>
<value>敲代码</value>
<value>玩游戏</value>
<value>看电影</value>
</list>
</property>
<!-- map-->
<property name="card">
<map>
<entry key="身份证" value="12154646874846"/>
<entry key="密码" value="12154646874846"/>
</map>
<!-- set-->
</property>
<property name="games">
<set>
<value>lol</value>
<value>bob</value>
<value>coc</value>
</set>
<!-- null-->
</property>
<property name="wife">
<null></null>
</property>
<!-- properties -->
<property name="info">
<props>
<prop key="学号">19070320</prop>
<prop key="姓名">王多多</prop>
</props>
</property>
</bean>
</beans>
3.测试
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
System.out.println(student.toString());
}
}
3.拓展方式注入
1.p-namespace
- 无参构造+set注入
在beans.xml里面的beans插入依赖
xmlns:p="http://www.springframework.org/schema/p"
2.创建一个User实体类
public class User {
private String name;
private int age;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", 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;
}
}
3.直接使用p命名
<bean id="user" class="com.ws.pojo.User" p:name="王多多" p:age="10"/>
4.测试
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user");
System.out.println(user.toString());
}
}
可以成功
2.C-namespace
- 有参构造construct
1.设置约束
xmlns:c="http://www.springframework.org/schema/c"
2.需要加上有参构造
public class User {
private String name;
private int age;
public User() {
}
public User(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", 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;
}
}
3.测试
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
User user = (User) context.getBean("user2");
System.out.println(user.toString());
}
}
可以成功