spring--基于配置文件的依赖注入

Spring 依赖注入

  • 经过类构造函数被注入到bean中。所以,控制流经过依赖注入(DI)已经“反转”
  • 第二种方法是经过 Setter 方法

Spring 基于构造函数的依赖注入

   <bean id="exampleBean" class="examples.ExampleBean">
      <constructor-arg type="int" value="2001"/>
      <constructor-arg type="java.lang.String" value="Zara"/>
   </bean>

 

其中constructor-arg标签有的属性值
Name

构造函数中的参数名html

Index

构造函数中的参数序号java

Value

参数值spring

Ref 引用bean
Type:bean 类类型

Spring 基于设值函数的依赖注入

   <bean id="john-classic" class="com.example.Person">
      <property name="name" value="John Doe"/>
      <property name="person" ref="jane"/>
   </bean>
   <bean name="jane" class="com.example.Person">
      <property name="name" value="John Doe"/>
   </bean>

使用 p-namespace 实现 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"
    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">

   <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

<bean id="outerBean" class="...">
 <property name="target"> 
<bean id="innerBean" class="..."/> 
</property> </bean> 

Spring 注入集合

如今若是想传递多个值,如 Java Collection 类型 List、Set、Map 和 Properties,Spring 提供了四种类型的集合的配置元素,以下所示:函数

<list>spa

它有助于连线,如注入一列值,容许重复。code

<set>xml

它有助于连线一组值,但不能重复。htm

<map>three

它能够用来注入名称-值对的集合,其中名称和值能够是任何类型。ci

<props>

它能够用来注入名称-值对的集合,其中名称和值都是字符串类型。

集合在xml文件中的注入方式

<bean id="javaCollection" class="com.tutorialspoint.JavaCollection">

      <property name="addressList">
         <list>
            <value>INDIA</value>
            <value>Pakistan</value>
            <value>USA</value>
            <value>USA</value>
         </list>
      </property>

      <property name="addressSet">
         <set>
            <value>INDIA</value>
            <value>Pakistan</value>
            <value>USA</value>
            <value>USA</value>
        </set>
      </property>

      <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>

      <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>

集合注入 Bean 引用:

<bean id="..." class="...">

      <property name="addressList">
         <list>
            <ref bean="address1"/>
            <ref bean="address2"/>
            <value>Pakistan</value>
         </list>
      </property>

      <property name="addressSet">
         <set>
            <ref bean="address1"/>
            <ref bean="address2"/>
            <value>Pakistan</value>
         </set>
      </property>

      <property name="addressMap">
         <map>
            <entry key="one" value="INDIA"/>
            <entry key ="two" value-ref="address1"/>
            <entry key ="three" value-ref="address2"/>
         </map>
      </property>

   </bean>

注入 null 和空字符串的值

若是你须要传递一个空字符串做为值,那么你能够传递它,以下所示:

<bean id="..." class="exampleBean"> <property name="email" value=""/> </bean>

前面的例子至关于 Java 代码:exampleBean.setEmail("")。

若是你须要传递一个 NULL 值,那么你能够传递它,以下所示:

<bean id="..." class="exampleBean"> <property name="email"><null/></property> </bean>

Spring Beans 自动装配

可用于指示 Spring 容器为来使用自动装配进行依赖注入。

能够使用<bean>元素的 autowire 属性为一个 bean 定义指定自动装配模式。

no

默认值,不进行自动装配,显式的bean引用来连线。

byName

属性名自动装配。 bean 的自动装配的属性设置为 byName,尝试匹配,而且将它的属性与在配置文件中被定义为相同名称的 beans 的属性进行链接。

byType

数据类型自动装配。 bean 的自动装配的属性设置为 byType。而后若是它的类型匹配配置文件中的有一个确切的 bean ,它将尝试匹配和链接属性的类型。若是存在不止一个这样的 bean,则一个致命的异常将会被抛出。

construct

相似于 byType,但该类型适用于构造函数参数类型。若是在容器中没有一个构造函数参数类型的 bean,则一个致命错误将会发生。

autodetect

Spring首先尝试经过 constructor 使用自动装配来链接,若是它不执行,Spring 尝试经过 byType 来自动装配。

Spring 自动装配 ‘byName’

根据属性名和其余的bean id进行匹配

   <bean id="textEditor" class="com.tutorialspoint.TextEditor">
       <property name="spellChecker" ref="spellChecker" />
       <property name="name" value="Generic Text Editor" />
   </bean>

   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

可省略为下图

   <bean id="textEditor" class="com.tutorialspoint.TextEditor"
      autowire="byName">
      <property name="name" value="Generic Text Editor" />
   </bean>

   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

Spring 自动装配 ‘byType’

根据属性class匹配与属性相同的bean

Spring 由构造函数自动装配

autowire 属性设置为 constructor,自动在容器中寻找id与参数名相同的bean(相似于byType)

相关文章
相关标签/搜索