Dependency Injection,依赖注入
简单来说什么是依赖注入,就是给属性赋值(包括基本数据类型和引用数据类型)
这里我们定义一个类,里面包含了我们常见的数据类型,顺便定了getter和setter
里面包含常量String ,int double这种
以及数组和集合
另外就是其他的类的实例,这里给其他类的实例赋值
public class Student {
private String name;
// 类
private Address address;
//字符串数组
private String[] books;
private List<String> hobbies;
private Set<String> games;
private Map<String, Long> cards;
private String wife;
private Properties info;
public void show() {
System.out.println("Student's name = " + name + ",address = "
+ address.getAddress());
//数组
System.out.print("books = ");
for (String string : books) {
System.out.print(string + " ");
}
System.out.println();
//集合
System.out.println("hobbies = " + hobbies);
System.out.println("games = " + games);
System.out.println("bank cards = " + cards);
System.out.println("wife = " + wife);
System.out.println("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> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public Set<String> getGames() {
return games;
}
public void setGames(Set<String> games) {
this.games = games;
}
public Map<String, Long> getCards() {
return cards;
}
public void setCards(Map<String, Long> cards) {
this.cards = cards;
}
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;
}
}
类注入
当然首先要有这个类才行
上面的Address类
public class Address {
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
首先要在xml文件中定义这个类
这个是这个Address类的bean,通过后面的class看
①
<!-- 类注入 -->
<bean id="address" class="com.dn.di.Address">
<property name="address" value="上海" />
</bean>
这个是我们定义要注入的类的bean,通过后面的class看
②
<bean id="student" class="com.dn.di.Student">
</bean>
把①放到②里面
<bean id="student" class="com.dn.di.Student">
<property name="address" ref="address" />
</bean>
这里面property是对象名
这个类注入就算有那么一点点复杂的注入了,但是两行代码还是搞定了,所以其他的也同理,注意一下格式就好了,所以整体的配置文件就是这个样子的
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 创建对象的第一种方式:利用无参构造器 id:唯一标识符 class:类的全类名 -->
<bean id="helloIoc" class="com.dn.ioc.HelloIocContructor"></bean>
<!-- 别名属性 name:和bean的 id 属性对应 -->
<alias name="helloIoc" alias="helloIoc2" />
<!-- 创建对象的第二种方式:利用静态工厂方法 id的名字随便取 -->
<bean id="helloStaticFactory" factory-method="getInstances"
class="com.dn.ioc.HelloIocStaticFactory"></bean>
<!-- 创建对象的第三种方法:利用实例工厂方法 -->
<bean id="instanceFactory" class="com.dn.ioc.HelloInstanceFactory"></bean>
<bean id="instance" factory-bean="instanceFactory" factory-method="getInstance"></bean>
<!-- DI依赖注入 -->
<!-- bean中的id不能重复 ,所以把同一个类的注入都写到一个bean里面 -->
<bean id="student" class="com.dn.di.Student">
<!-- property是对象名 -->
<!-- 常量注入 -->
<property name="name" value="名字" />
<!-- 类注入 -->
<property name="address" ref="address" />
<!-- 数组注入 -->
<property name="books">
<array>
<value>傲慢与偏见</value>
<value>基督山伯爵</value>
<value>钢铁是怎样炼成的</value>
</array>
</property>
<!-- List注入 -->
<property name="hobbies">
<list>
<value>看书</value>
<value>听歌</value>
<value>协作</value>
</list>
</property>
<!-- Map注入 -->
<property name="cards">
<map>
<!-- 第一种写法 -->
<entry key="招商银行" value="6214586873865814" />
<!-- 第二种写法 -->
<entry>
<key>
<value>工商银行</value>
</key>
<value>132164612231684684</value>
</entry>
</map>
</property>
<!-- Null注入 -->
<property name="wife">
<null/>
</property>
<!-- Properties注入 -->
<property name="info">
<props>
<prop key="学号">20190828001</prop>
<prop key="sex">男</prop>
<prop key="name">名字</prop>
</props>
</property>
</bean>
<!-- 类注入 -->
<bean id="address" class="com.dn.di.Address">
<property name="address" value="上海" />
</bean>
</beans>
最后自己再定义一个测试类
public class DITest {
public static void testSet(){
//1.启动spring容器
//2.从spring容器中取出数据
//3.通过对象调用方法
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Student Student = (Student) context.getBean("student");
Student.show();;
}
public static void main(String[] args) {
DITest.testSet();
}
}
结果
实例工厂方法构造函数
Student's name = 名字,address = 上海
books = 傲慢与偏见 基督山伯爵 钢铁是怎样炼成的
hobbies = [看书, 听歌, 协作]
games = null
bank cards = {招商银行=6214586873865814, 工商银行=132164612231684684}
wife = null
info = {学号=20190828001, name=名字, sex=男}