Spring基础 专题
专题目录
您的位置:java > Spring基础专题 > Spring 自动装配 `byType`
Spring 自动装配 `byType`
作者:--    发布时间:2019-11-20

这种模式由属性类型指定自动装配。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.tutorialspoint
2 使用 add external jars 选项,添加所需的 spring 库,在 spring hello world example 章节中已说明。
3 com.tutorialspoint 包中创建 java 类 texteditorspellcheckermainapp
4 src 文件夹中创建 beans 的配置文件 beans.xml
5 最后一步是创建所有 java 文件和 bean 配置文件的内容,并运行该应用程序,正如下面解释的一样。

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

package com.tutorialspoint;
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.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" />
      <property name="name" value="generic text editor" />
   </bean>

   <!-- definition for spellchecker bean -->
   <bean id="spellchecker" class="com.tutorialspoint.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.tutorialspoint.texteditor" 
      autowire="bytype">
      <property name="name" value="generic text editor" />
   </bean>

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

</beans>

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

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