目录

基于Setter的依赖注入(Setter-based dependency injection)

基于setter的DI是在调用无参数构造函数或无参数静态工厂方法来实例化bean之后,通过容器调用bean上的setter方法来完成的。

例子 (Example)

以下示例显示了一个TextEditor类,它只能使用基于setter的纯注入进行依赖注入。

让我们有一个可用的Eclipse IDE,并按照以下步骤创建一个Spring应用程序 -

脚步 描述
1 创建一个名为SpringExample的项目,并在创建的项目中的src文件夹下创建一个包com.iowiki
2 使用Add External JARs选项添加所需的Spring库,如Spring Hello World Example章节中所述。
3com.iowiki包下创建Java类TextEditorMainAppMainApp
4src文件夹下创建Beans配置文件Beans.xml
5 最后一步是创建所有Java文件和Bean配置文件的内容并运行应用程序,如下所述。

这是TextEditor.java文件的内容 -

package com.iowiki;
public class TextEditor {
   private SpellChecker spellChecker;
   // a setter method to inject the dependency.
   public void setSpellChecker(SpellChecker spellChecker) {
      System.out.println("Inside setSpellChecker." );
      this.spellChecker = spellChecker;
   }
   // a getter method to return spellChecker
   public SpellChecker getSpellChecker() {
      return spellChecker;
   }
   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}

在这里,您需要检查setter方法的命名约定。 要设置变量setSpellChecker()我们使用的setSpellChecker()方法与Java POJO类非常相似。 让我们创建另一个依赖类文件SpellChecker.java -

package com.iowiki;
public class SpellChecker {
   public SpellChecker(){
      System.out.println("Inside SpellChecker constructor." );
   }
   public void checkSpelling() {
      System.out.println("Inside checkSpelling." );
   }
}

以下是MainApp.java文件的内容 -

package com.iowiki;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
      TextEditor te = (TextEditor) context.getBean("textEditor");
      te.spellCheck();
   }
}

以下是配置文件Beans.xml ,它具有基于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"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
   <!-- Definition for textEditor bean -->
   <bean id = "textEditor" class = "com.iowiki.TextEditor">
      <property name = "spellChecker" ref = "spellChecker"/>
   </bean>
   <!-- Definition for spellChecker bean -->
   <bean id = "spellChecker" class = "com.iowiki.SpellChecker"></bean>
</beans>

您应该注意基于构造函数的注入和基于setter的注入中定义的Beans.xml文件的区别。 唯一的区别是在元素中,我们使用标签进行基于构造函数的注入,使用标签进行基于setter的注入。

要注意的第二个要点是,如果要传递对象的引用,则需要使用“property”标记的ref属性,如果直接传递value ,则应使用value属性。

完成源和bean配置文件的创建后,让我们运行应用程序。 如果您的应用程序一切正常,这将打印以下消息 -

Inside SpellChecker constructor.
Inside setSpellChecker.
Inside checkSpelling.

使用p-namespace进行XML配置

如果您有许多setter方法,那么在XML配置文件中使用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"
   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配置,如下所示 -

<?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的引用。

↑回到顶部↑
WIKI教程 @2018