Spring基于Setter函数的依赖注入(DI)

以下内容引用自http://wiki.jikexueyuan.com/project/spring/dependency-injection/spring-setter-based-dependency-injection.html

当容器调用一个无参的构造函数或一个无参的静态factory方法来初始化你的bean后,通过容器在你的bean上调用Setter函数,基于Setter函数的DI就完成了。

例子:

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.jsoft.testspring</groupId>
  <artifactId>testbeansetter</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>testbeansetter</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    
    <!-- Spring Core -->
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
     
    <!-- Spring Context -->
    <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.4.RELEASE</version>
    </dependency>
    
  </dependencies>
</project>

SpellChecker.java:

package com.jsoft.testspring.testbeansetter;

public class SpellChecker {
    public SpellChecker(){
        System.out.println("SpellChecker无参数构造函数初始化");
    }
    
    public void checkSpelling(){
        System.out.println("SpellChecker检查方法");
    }
}

TextEditor.java:

package com.jsoft.testspring.testbeansetter;

public class TextEditor {
    private SpellChecker spellChecker;
    
    public void setSpellChecker(SpellChecker spellChecker){
        System.out.println("TextEditor通过setter初始化");
        this.spellChecker = spellChecker;
    }
    
    public void spellCheck() {
        this.spellChecker.checkSpelling();
    }
    
}

beans.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.xsd">
                        
    <bean id="spellChecker" class="com.jsoft.testspring.testbeansetter.SpellChecker"></bean>
    
    <bean id="textEditor" class="com.jsoft.testspring.testbeansetter.TextEditor">
        <property name="SpellChecker" ref="spellChecker"></property>
    </bean>
   
</beans>

唯一的区别就是在基于构造函数注入中,我们使用的是<constructor-arg>标签,而在基于Setter函数的注入中,我们使用的是<property>标签。

第二个你需要注意的点是,如果你要把一个引用传递给一个对象,那么你需要使用ref属性,而如果你要直接传递一个值,那么你应该使用value属性。

App.java:

package com.jsoft.testspring.testbeansetter;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
        TextEditor textEditor = (TextEditor)applicationContext.getBean("textEditor");
        textEditor.spellCheck();
    }
}

运行结果:

下面将介绍使用p-namespace实现XML配置:

如果你有许多的Setter函数方法,那么在XML配置文件中使用p-namespace是非常方便的。区别如下:

以下为使用<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-3.0.xsd">

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

</beans>

改用p-namespace之后:

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

看起来非常的整洁,在这里,您应该注意到使用p-namespace指定原始值和对象引用的区别。-ref部分表示这不是一个直接的值,而是引用另一个bean。

 

测试工程:https://github.com/easonjim/5_java_example/tree/master/springtest/test8/testbeansetter

posted @ 2017-05-20 23:30  EasonJim  阅读(378)  评论(0编辑  收藏  举报