Spring学习四:依赖注入DI(Dependency Injection)

xiaoxiao2021-02-27  481

学习目标

构造函数注入setter设值注入注入内部Bean注入集合

先看看依赖注入的好处

每个基于应用程序的 java 都有几个对象,这些对象一起工作来呈现出终端用户所看到的工作的应用程序。当编写一个复杂的 Java 应用程序时,应用程序类应该尽可能独立于其他 Java 类来增加这些类重用的可能性,并且在做单元测试时,测试独立于其他类的独立性。依赖注入有助于把这些类粘合在一起,同时保持他们独立。

依赖注入的好处是Spring管理应用程序中需要使用的Java对象,把Java对象和具体的业务尽可能保持独立,提高类可用性。

构造函数注入

在基于xml的配置元数据中,配置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 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 需要注入的Bean --> <bean id="foo" class="x.y.Foo"> <!-- 构造函数参数1,类型一个引用,是x.y.Bar类型 --> <constructor-arg ref="bar"/> <!-- 构造函数参数2 --> <constructor-arg ref="baz"/> <!-- 构造函数参数3,下标是从0开始,"Zara是一个值,而非引用,用了value标签" --> <constructor-arg index="2" value="Zara"/> <!-- 构造函数参数4,类型为int类型,同样value表示值 --> <constructor-arg type="int" value="2001"/> </bean> <bean id="bar" class="x.y.Bar"/> <bean id="baz" class="x.y.Baz"/> </beans>

练习地址

setter设值注入

和构造函数注入类似,也是在xml配置元数据中进行设置。 setter设值注入需要注入的JavaBean的setter方法存在且正确。

<?xml version="1.0" encoding="UTF-8"?> <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 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <!-- 方法一:用property标签,name表示JavaBean的属性,value表示要给该属性赋的值,ref表示给该属性指定的引用 --> <bean id="john-classic" class="com.example.Person"> <property name="name" value="John Doe"/> <property name="spouse" ref="jane"/> </bean> <bean name="jane" class="com.example.Person"> <property name="name" value="John Doe"/> </bean> <!-- 方法二:在<bean>的标签内,不用再用新的<property>标签了 --> <bean id="john-classic" class="com.example.Person" p:name="John Doe" p:spouse-ref="jane"/> </bean> <bean name="jane" class="com.example.Person" p:name="John Doe"/> </bean> </beans>

注入内部Beans

Java 内部类是在其他类的范围内被定义的,同理,inner beans 是在其他 bean 的范围内定义的 bean。因此在 或 元素内 元素被称为内部bean。

在xml配置元数据中,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 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="outerBean" class="..."> <property name="target"> <bean id="innerBean" class="..."/> </property> </bean> </beans>

鉴于目前初学Spring,不知道Inner Beans存在的意义何在,这块也就不做代码练习了。极客学院注入内部Beans

注入集合

Spring注入集合的意思是,假如某个属性是Collection类型 List、Set、Map 和 Properties时,此时用xml配置文件初始化List时,需要给List赋多个值时,该如何去做。

元素描述<list>它有助于连线,如注入一列值,允许重复。<set>它有助于连线一组值,但不能重复。<map>它可以用来注入名称-值对的集合,其中名称和值可以是任何类型。<props>它可以用来注入名称-值对的集合,其中名称和值都是字符串类型。

JavaBean对象

public class JavaCollection { List addressList; Set addressSet; Map addressMap; Properties addressProp; //TODO setter and getter }

用value直接赋值的方式注入

<?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-3.0.xsd"> <!-- Definition for javaCollection --> <bean id="javaCollection" class="com.tutorialspoint.JavaCollection"> <!-- results in a setAddressList(java.util.List) call --> <property name="addressList"> <list> <value>INDIA</value> <value>Pakistan</value> <value>USA</value> <value>USA</value> </list> </property> <!-- results in a setAddressSet(java.util.Set) call --> <property name="addressSet"> <set> <value>INDIA</value> <value>Pakistan</value> <value>USA</value> <value>USA</value> </set> </property> <!-- results in a setAddressMap(java.util.Map) call --> <property name="addressMap"> <map> <entry key="1" value="INDIA"/> <entry key="2" value="Pakistan"/> <entry key="3" value="USA"/> <entry key="4" value="USA"/> </map> </property> <!-- results in a setAddressProp(java.util.Properties) call --> <property name="addressProp"> <props> <prop key="one">INDIA</prop> <prop key="two">Pakistan</prop> <prop key="three">USA</prop> <prop key="four">USA</prop> </props> </property> </bean> </beans>

给属性传递引用的方式

<?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-3.0.xsd"> <!-- 同时有引用和值的注入 --> <bean id="..." class="..."> <!-- 引用注入 java.util.List --> <property name="addressList"> <list> <!-- address1是个List类型 --> <ref bean="address1"/> <ref bean="address2"/> <value>Pakistan</value> </list> </property> <!-- Passing bean reference for java.util.Set --> <property name="addressSet"> <set> <ref bean="address1"/> <ref bean="address2"/> <value>Pakistan</value> </set> </property> <!-- Passing bean reference for java.util.Map --> <property name="addressMap"> <map> <entry key="one" value="INDIA"/> <entry key ="two" value-ref="address1"/> <entry key ="three" value-ref="address2"/> </map> </property> </bean> </beans>

注入 null 和空字符串的值

<!-- 注入空串 --> <bean id="..." class="exampleBean"> <property name="email" value=""/> </bean> <!-- 注入null --> <bean id="..." class="exampleBean"> <property name="email"><null/></property> </bean>

总结

需要掌握以下4点: 1. 构造函数注入,方法是<bean>标签中用<constructor> 2. setter设值注入,方法是<bean>标签中用<property> 3. 注入内部Bean,方法是<bean>标签中用<bean> 4. 注入集合,对Collection子类进行初始化化,方法是<bean>标签中用<list> or <set> or <map>,然后选择用value或者ref.

转载请注明原文地址: https://www.6miu.com/read-696.html

最新回复(0)