×
Spring 概述Spring 体系结构Spring 环境配置Spring Hello World 实例Spring IoC 容器Spring BeanFactory 容器Spring ApplicationContextSpring Bean 定义Spring Bean 作用域Spring Bean 生命周期Spring Bean 后置处理器Spring Bean 定义继承Spring 依赖注入Spring 基于构造函数依赖注入Spring 基于设值函数依赖注入Spring 注入内部 BeansSpring 注入集合Spring Beans 自动装配Spring 自动装配 `byName`Spring 自动装配 `byType`Spring 由构造函数自动装配Spring 基于注解的配置Spring @Required 注释Spring @Autowired 注释Spring @Qualifier 注释Spring JSR-250 注释Spring 基于 Java 的配置Spring 中的事件处理Spring 中的自定义事件Spring 框架的 AOPSpring 基于 AOP 的 XML架构Spring 基于 AOP 的 @AspectJSpring JDBC 框架Spring JDBC 示例Spring 中 SQL 的存储过程Spring 事务管理Spring 编程式事务管理Spring 声明式事务管理Spring Web MVC 框架Spring MVC Hello World 例子Spring MVC 表单处理例子Spring 页面重定向例子Spring 静态页面例子Spring 异常处理例子Spring 使用 Log4J 记录日志

Spring 自动装配 `byType`


这种模式由属性类型指定自动装配。Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 byType。然后,如果它的 type 恰好与配置文件中 beans 名称中的一个相匹配,它将尝试匹配和连接它的属性。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常。

例如,在配置文件中,如果一个 bean 定义设置为自动装配 byType,并且它包含 SpellChecker 类型的 spellChecker 属性,那么 Spring 就会查找定义名为 SpellChecker 的 bean,并且用它来设置这个属性。你仍然可以使用 <property> 标签连接其余属性。下面的例子将说明这个概念,你会发现和上面的例子没有什么区别,除了 XML 配置文件已经被改变。

让我们在恰当的位置使用 Eclipse IDE,然后按照下面的步骤来创建一个 Spring 应用程序:

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

这里是 TextEditor.java 文件的内容:

package com.bootwiki;
public class TextEditor {
   private SpellChecker spellChecker;
   private String name;
   public void setSpellChecker( SpellChecker spellChecker ) {
      this.spellChecker = spellChecker;
   }
   public SpellChecker getSpellChecker() {
      return spellChecker;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      return name;
   }
   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}

下面是另一个依赖类文件 SpellChecker.java 的内容:

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

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

package com.bootwiki;
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.bootwiki.TextEditor">
      <property name="spellChecker" ref="spellChecker" />
      <property name="name" value="Generic Text Editor" />
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.bootwiki.SpellChecker">
   </bean>

</beans>

但是,如果你要使用自动装配 “byType”,那么你的 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.bootwiki.TextEditor" 
      autowire="byType">
      <property name="name" value="Generic Text Editor" />
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="SpellChecker" class="com.bootwiki.SpellChecker">
   </bean>

</beans>

一旦你完成了创建源代码和 bean 的配置文件,我们就可以运行该应用程序。如果你的应用程序一切都正常,它将打印下面的消息:

Inside SpellChecker constructor.
Inside checkSpelling.

分类导航

关注微信下载离线手册

bootwiki移动版 bootwiki
(群号:472910771)