市面上做网站多少钱,网页设计logo图片,龙口市规划建设局网站,东莞南城网站建设转载自 Spring 由构造函数自动装配
Spring 由构造函数自动装配
这种模式与 byType 非常相似#xff0c;但它应用于构造器参数。Spring 容器看作 beans#xff0c;在 XML 配置文件中 beans 的 autowire 属性设置为 constructor。然后#xff0c;它尝试把它的构造函数的参数…转载自 Spring 由构造函数自动装配
Spring 由构造函数自动装配
这种模式与 byType 非常相似但它应用于构造器参数。Spring 容器看作 beans在 XML 配置文件中 beans 的 autowire 属性设置为 constructor。然后它尝试把它的构造函数的参数与配置文件中 beans 名称中的一个进行匹配和连线。如果找到匹配项它会注入这些 bean否则它会抛出异常。
例如在配置文件中如果一个 bean 定义设置为通过构造函数自动装配而且它有一个带有 SpellChecker 类型的参数之一的构造函数那么 Spring 就会查找定义名为 SpellChecker 的 bean并用它来设置构造函数的参数。你仍然可以使用 constructor-arg 标签连接其余属性。下面的例子将说明这个概念。
让我们在恰当的位置使用 Eclipse IDE然后按照下面的步骤来创建一个 Spring 应用程序
步骤描述1创建一个名称为 SpringExample 的项目并且在已创建的项目的 src 文件夹中创建一个包 com.tutorialspoint。2使用 Add External JARs 选项添加所需的 Spring 库在 Spring Hello World Example 章节中已说明。3在 com.tutorialspoint 包中创建 Java 类 TextEditorSpellChecker 和 MainApp。4在 src 文件夹中创建 Beans 的配置文件 Beans.xml。5最后一步是创建所有 Java 文件和 Bean 配置文件的内容并运行该应用程序正如下面解释的一样。
这里是 TextEditor.java 文件的内容
package com.tutorialspoint;
public class TextEditor {private SpellChecker spellChecker;private String name;public TextEditor( SpellChecker spellChecker, String name ) {this.spellChecker spellChecker;this.name name;}public SpellChecker getSpellChecker() {return spellChecker;}public String getName() {return name;}public void spellCheck() {spellChecker.checkSpelling();}
}
下面是另一个依赖类文件 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 version1.0 encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd!-- Definition for textEditor bean --bean idtextEditor classcom.tutorialspoint.TextEditorconstructor-arg refspellChecker /constructor-arg valueGeneric Text Editor//bean!-- Definition for spellChecker bean --bean idspellChecker classcom.tutorialspoint.SpellChecker/bean/beans
但是如果你要使用自动装配 “by constructor”那么你的 XML 配置文件将成为如下
?xml version1.0 encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd!-- Definition for textEditor bean --bean idtextEditor classcom.tutorialspoint.TextEditor autowireconstructorconstructor-arg valueGeneric Text Editor//bean!-- Definition for spellChecker bean --bean idSpellChecker classcom.tutorialspoint.SpellChecker/bean/beans
一旦你完成了创建源代码和 bean 的配置文件我们就可以运行该应用程序。如果你的应用程序一切都正常它将打印下面的消息
Inside SpellChecker constructor.
Inside checkSpelling.