课程表

Spring 入门

Spring IoC 容器

Spring 依赖注入

Spring Beans 自动装配

Spring 基于注解的配置

Spring 框架

Spring 事务管理

Spring Web MVC 框架

工具箱
速查手册

Spring 基于设值函数的注入

当前位置:免费教程 » Java相关 » Spring

当容器调用一个无参的构造函数或一个无参的静态 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 文件的内容:

  1. package com.tutorialspoint;
  2. public class TextEditor {
  3. private SpellChecker spellChecker;
  4. // a setter method to inject the dependency.
  5. public void setSpellChecker(SpellChecker spellChecker) {
  6. System.out.println("Inside setSpellChecker." );
  7. this.spellChecker = spellChecker;
  8. }
  9. // a getter method to return spellChecker
  10. public SpellChecker getSpellChecker() {
  11. return spellChecker;
  12. }
  13. public void spellCheck() {
  14. spellChecker.checkSpelling();
  15. }
  16. }

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

  1. package com.tutorialspoint;
  2. public class SpellChecker {
  3. public SpellChecker(){
  4. System.out.println("Inside SpellChecker constructor." );
  5. }
  6. public void checkSpelling() {
  7. System.out.println("Inside checkSpelling." );
  8. }
  9. }

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

  1. package com.tutorialspoint;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainApp {
  5. public static void main(String[] args) {
  6. ApplicationContext context =
  7. new ClassPathXmlApplicationContext("Beans.xml");
  8. TextEditor te = (TextEditor) context.getBean("textEditor");
  9. te.spellCheck();
  10. }
  11. }

下面是配置文件 Beans.xml 的内容,该文件有基于设值函数注入的配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  7.  
  8. <!-- Definition for textEditor bean -->
  9. <bean id="textEditor" class="com.tutorialspoint.TextEditor">
  10. <property name="spellChecker" ref="spellChecker"/>
  11. </bean>
  12.  
  13. <!-- Definition for spellChecker bean -->
  14. <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
  15. </bean>
  16.  
  17. </beans>

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

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

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

  1. Inside SpellChecker constructor.
  2. Inside setSpellChecker.
  3. Inside checkSpelling.

使用 p-namespace 实现 XML 配置:

如果你有许多的设值函数方法,那么在 XML 配置文件中使用 p-namespace 是非常方便的。让我们查看一下区别:

以带有 标签的标准 XML 配置文件为例:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  7.  
  8. <bean id="john-classic" class="com.example.Person">
  9. <property name="name" value="John Doe"/>
  10. <property name="spouse" ref="jane"/>
  11. </bean>
  12.  
  13. <bean name="jane" class="com.example.Person">
  14. <property name="name" value="John Doe"/>
  15. </bean>
  16.  
  17. </beans>

上述 XML 配置文件可以使用 p-namespace 以一种更简洁的方式重写,如下所示:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:p="http://www.springframework.org/schema/p"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  8.  
  9. <bean id="john-classic" class="com.example.Person"
  10. p:name="John Doe"
  11. p:spouse-ref="jane"/>
  12. </bean>
  13.  
  14. <bean name="jane" class="com.example.Person"
  15. p:name="John Doe"/>
  16. </bean>
  17.  
  18. </beans>

在这里,你不应该区别指定原始值和带有 p-namespace 的对象引用。-ref 部分表明这不是一个直接的值,而是对另一个 bean 的引用。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号