Spring (7): di dependency injection

Who knows if the water is hot or not 2022-09-23 10:18:27 阅读数:64

springdidependencyinjection

一、什么是DI依赖注入

依赖关系注入 (DI) 是一个过程,通过该过程,对象仅通过构造函数参数、Parameters to a factory method or properties that are set on an object instance after it is constructed or returned from a factory method define its dependencies(即,使

with other objects they use).然后,容器在创建 Bean 时注入这些依赖项.这个过程基本上是Beaninverse function of itself(因此得名“控制反转”),Controlled by using the direct construction of the class or the Service Locator pattern

The instantiation or location of its dependencies.

使用 DI 原则,代码更清晰,When an object is provided with its dependencies,解耦更有效.该对象不查找其依赖项,也不知道依赖项的位置或类.

二、依赖注入的方式

依赖注入有三种方式:Constructor-based dependency injection、利用setDependency injection for methods、Additional dependency injection.

1.Constructor-based dependency injection

这个在Spring(五)It has already been said in the study,Here is only a brief description of the way of constructor injection.

(1)通过indexindex for injection,索引从0开始.

(2)Inject by passing in the parameter name,最方便.

(3)Inject by the type of the parameter passed,Does not apply to situations of the same type.

(4)通过bean进行注入.

2.利用setDependency injection for methods

通过setMethod dependency injection is the core injection method.

This previous study has also been used,However, the previous injections are some simple basic types andString类型,Here we go to some complex types such as List、MapAnd so on to learn the injection method.

(1)Student和Addressis the class we are going to use

Address

package com.jms.pojo;
public class Address {
private int id;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Address{" +
"id=" + id +
", address='" + address + '\'' +
'}';
}
}

Student

package com.jms.pojo;
import java.util.*;
public class Student {
private String name;
private Address address;
private String[] lesson;
private List<String> hobbys;
private Map<String, String> card;
private Set<String> games;
private String graduate;
private Properties info;
public String[] getLesson() {
return lesson;
}
public void setLesson(String[] lesson) {
this.lesson = lesson;
}
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 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 getGraduate() {
return graduate;
}
public void setGraduate(String graduate) {
this.graduate = graduate;
}
public Properties getInfo() {
return info;
}
public void setInfo(Properties info) {
this.info = info;
}
@Override
public String toString() {
return "Student{" + "\n" +
"name='" + name + '\'' + "\n" +
"address=" + address.toString() + "\n" +
"lesson=" + Arrays.toString(lesson) + "\n" +
"hobbys=" + hobbys + "\n" +
"card=" + card + "\n" +
"games=" + games + "\n" +
"graduate='" + graduate + '\'' + "\n" +
"info=" + info + "\n" +
'}';
}
}

(2)beans.xml

<bean id="student" class="com.jms.pojo.Student">
<!--普通类型-->
<property name="name" value="jms"/>
<!--Bean-->
<property name="address" ref="address"/>
<!--数组-->
<property name="lesson">
<array>
<value>C</value>
<value>Java</value>
<value>Python</value>
</array>
</property>
<!--List集合-->
<property name="hobbys">
<list>
<value>编程</value>
<value>写作</value>
<value>听音乐</value>
</list>
</property>
<!--Map-->
<property name="card">
<map>
<entry key="学生卡" value="101010101"/>
<entry key="饭卡" value="20202020202"/>
<entry key="水卡" value="30303030303"/>
</map>
</property>
<!--Set-->
<property name="games">
<set>
<value>game1</value>
<value>game2</value>
<value>game3</value>
</set>
</property>
<!--null-->
<property name="graduate">
<null/>
</property>
<!--Properties-->
<property name="info">
<props>
<prop key="学号">100001</prop>
<prop key="姓名">ah van der sar</prop>
<prop key="班级">计算机3班</prop>
</props>
</property>
</bean>
<bean id="address" class="com.jms.pojo.Address">
<property name="id" value="1"/>
<property name="address" value="world"/>
</bean>

(3)测试

 @Test
public void studentTest() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
Student student = applicationContext.getBean("student", Student.class);
System.out.println(student);
}

测试结果:

3.Additional dependency injection

 c命名空间注入和p命名空间注入,This is the official injection method of expansion,pNamespace injection correspondsset注入,cNamespace injection corresponds to parameterized constructor injection.

Constraints need to be added when using both injections,The following are the official usage examples:

p-namespace:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="john-classic" class="com.example.Person">
<property name="name" value="John Doe"/>
<property name="spouse" ref="jane"/>
</bean>
<bean name="john-modern"
class="com.example.Person"
p:name="John Doe"
p:spouse-ref="jane"/>
<bean name="jane" class="com.example.Person">
<property name="name" value="Jane Doe"/>
</bean>
</beans>

c-namespace:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beanTwo" class="x.y.ThingTwo"/>
<bean id="beanThree" class="x.y.ThingThree"/>
<!-- traditional declaration with optional argument names -->
<bean id="beanOne" class="x.y.ThingOne">
<constructor-arg name="thingTwo" ref="beanTwo"/>
<constructor-arg name="thingThree" ref="beanThree"/>
<constructor-arg name="email" value="[email protected]"/>
</bean>
<!-- c-namespace declaration with argument names -->
<bean id="beanOne" class="x.y.ThingOne" c:thingTwo-ref="beanTwo"
c:thingThree-ref="beanThree" c:email="[email protected]"/>
</beans>

 

 

(本文仅作个人学习记录用,如有纰漏敬请指正)

copyright:author[Who knows if the water is hot or not],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/266/202209231010124992.html