目录

byType

此模式按属性类型指定自动装配。 Spring容器在XML配置文件中查看autowire属性设置为byType autowire 。 然后,如果属性的type与配置文件中的一个bean名称匹配,则会尝试匹配并连接属性。 如果找到匹配,它将注入这些bean。 否则,bean将不会被连线。

例如,如果bean配置在配置文件中设置为autowire byType ,并且它包含SpellChecker类型的spellChecker属性,则Spring会查找名为SpellChecker的bean定义,并使用它来设置属性。 您仍然可以使用“property”标签连接其余属性。 以下示例将说明除了XML配置文件已更改之外,您将发现与上述示例没有区别的概念。

让我们有一个可用的Eclipse IDE,并按照以下步骤创建一个Spring应用程序 -

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

这是TextEditor.java文件的内容 -

package com.iowiki;
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.iowiki;
public class SpellChecker {
   public SpellChecker(){
      System.out.println("Inside SpellChecker constructor." );
   }
   public void checkSpelling() {
      System.out.println("Inside checkSpelling." );
   }
}

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

package com.iowiki;
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.iowiki.TextEditor">
      <property name = "spellChecker" ref = "spellChecker" />
      <property name = "name" value = "Generic Text Editor" />
   </bean>
   <!-- Definition for spellChecker bean -->
   <bean id = "spellChecker" class = "com.iowiki.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.iowiki.TextEditor" autowire = "byType">
      <property name = "name" value = "Generic Text Editor" />
   </bean>
   <!-- Definition for spellChecker bean -->
   <bean id = "SpellChecker" class = "com.iowiki.SpellChecker"></bean>
</beans>

完成源和bean配置文件的创建后,让我们运行应用程序。 如果您的应用程序一切正常,它将打印以下消息 -

Inside SpellChecker constructor.
Inside checkSpelling.
↑回到顶部↑
WIKI教程 @2018