Bean XML configuration (3) - configuration dependency injection


Spring series of tutorials


This article describes how to configure the dependencies between Bean use the xml.

xml configuration file in the definition of the bean can be configured in dependency of the bean, configuration generally used two kinds:

  • Constructor injection
  • Setter Method Injection

Constructor injection

The XML configuration, Spring Bean instance first create a container depends, then passed to the constructor for the class.

Example:

public class App {
    
    private Service mainService;
    private Service[] services;
  
    // 注入Service实例
    public App(Service main){
        this.mainService = main;
    }
 
    // 注入Service实例数组
    public App(Service[] services){
        this.services = services;
    }
}

In bean.xml, define App class dependencies:


<!-- App Bean定义 -->
<bean id="app" class="App">
 
 <!-- 构造函数注入单个依赖项实例 -->
 <constructor-arg ref="logger"/>
 
 <!-- 构造函数注入依赖项实例数组 -->
 <!-- <constructor-arg>
  <list>
    <ref bean="database"/>
    <ref bean="mail"/>
    <ref bean="logger"/>
  </list>
 </constructor-arg> --> 
</bean>

<!-- Service bean (依赖项)定义 -->
<bean id="database" class="Database"/>
<bean id="logger" class="Logger"/>
<bean id="mail" class="Mail"/>

Database, Logger, MailAre following Chengzi Ji class Service.

<constructor-arg>Constructor for injection manner Bean, refattributes specified to be implanted Bean (reference), the attribute value is dependent on the bean's ID.

XML bean definition of only one <constructor-arg>, so in the above example injection or a single service instance, the service instance or an array of injection.

Make sure the XML file for all dependencies configuration bean, or the Spring container can not inject these dependencies.

Setter Method Injection

The XML configuration, Setter class method calls the container Spring dependency injection.

Example:

public class App {

    // ...
    
    public Service getMainService() {
        return mainService;
    }
    
    // 通过setter方法注入服务实例
    public void setMainService(Service mainService) {
        this.mainService = mainService;
    }
    
    public Service[] getServices() {
        return services;
    }
    
    // 通过setter方法注入服务实例数组
    public void setServices(Service[] services) {
        this.services = services;
    }
}

In bean.xml, the dependency class definition. Spring container according to <property>the corresponding configuration setter method, call the class, set properties, achieve dependency injection.

<bean id="app" class="App">
 
 <!-- Setter 方法配置 -->
 <property name="mainService" ref="logger"/>
 <property name="services">
  <list>
   <ref bean="database"/>
   <ref bean="mail"/>
   <ref bean="logger"/>
  </list>
 </property>
  
</bean>

Spring container how to know which setter method is called the Spring container according to name calling setter methods:? Name corresponds to "set" property name after the keyword, name="mainService"corresponds to setMainService.

Injection configuration values

We describe the use of front <constructor-arg>and <property>Bean instance injection-dependent, they also can be used to inject values.

Example:

<bean id="app" class="App">
 
 <!-- 构造函数注入值 -->
 <constructor-arg type="int" value="12345"/>
 <constructor-arg type="java.lang.String" value="myApp"/>
 
 <!-- 也可通过构造函数的参数序号注入值 -->
 <!-- <constructor-arg index="0" value="12345"/> -->
 <!-- <constructor-arg index="1" value="myApp"/> -->

 <!-- Setter方法注入值 -->
 <!-- <property name="id" value="1234"/> -->
 <!-- <property name="name" value="myApp"/> -->
  
</bean>

XML in valuevalue is assigned to the class attribute of the property, if the reference to a bean, then use the refproperty.

If the need to pass a null or empty string value may be set as follows:

<bean id="app" class="App">
  <property name="name" value=""/>
</bean>
<bean id="app" class="App">
  <property name="name"><null/></property>
</bean>

Guess you like

Origin www.cnblogs.com/haibianren/p/11646797.html