Spring基础 专题
专题目录
您的位置:java > Spring基础专题 > Spring 基于设值函数的依赖注入
Spring 基于设值函数的依赖注入
作者:--    发布时间:2019-11-20

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

示例:

下述例子显示了一个类 texteditor,它只能使用纯粹的基于设值函数的注入来实现依赖注入。

让我们用 eclipse ide 适当地工作,并按照以下步骤创建一个 spring 应用程序。

步骤 描述
1 创建一个名为 springexample 的项目,并在创建的项目中的 src 文件夹下创建包 com.tutorialspoint
2 使用 add external jars 选项添加必需的 spring 库,解释见 spring hello world example chapter.
3 com.tutorialspoint 包下创建 java类 texteditorspellcheckermainapp
4 src 文件夹下创建 beans 的配置文件 beans.xml
5 最后一步是创建所有 java 文件和 bean 配置文件的内容并按照如下所示的方法运行应用程序。

下面是 texteditor.java 文件的内容:

package com.tutorialspoint;
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();
   }
}

在这里,你需要检查设值函数方法的名称转换。要设置一个变量 spellchecker,我们使用 setspellchecker() 方法,该方法与 java pojo 类非常相似。让我们创建另一个依赖类文件 spellchecker.java 的内容:

package com.tutorialspoint;
public class spellchecker {
   public spellchecker(){
      system.out.println("inside spellchecker constructor." );
   }
   public void checkspelling() {
      system.out.println("inside checkspelling." );
   }  
}

以下是 mainapp.java 文件的内容:

package com.tutorialspoint;
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 的内容,该文件有基于设值函数注入的配置:

<?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.tutorialspoint.texteditor">
      <property name="spellchecker" ref="spellchecker"/>
   </bean>

   <!-- definition for spellchecker bean -->
   <bean id="spellchecker" class="com.tutorialspoint.spellchecker">
   </bean>

</beans>

你应该注意定义在基于构造函数注入和基于设值函数注入中的 beans.xml 文件的区别。唯一的区别就是在基于构造函数注入中,我们使用的是〈bean〉标签中的〈constructor-arg〉元素,而在基于设值函数的注入中,我们使用的是〈bean〉标签中的〈property〉元素。

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

当你完成了创建源和 bean 配置文件后,让我们开始运行应用程序。如果你的应用程序运行顺利的话,那么将会输出下述所示消息:

inside spellchecker constructor.
inside setspellchecker.
inside checkspelling.

使用 p-namespace 实现 xml 配置:

如果你有许多的设值函数方法,那么在 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>

上述 xml 配置文件可以使用 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 的引用。


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册