目录

byName

此模式通过属性名称指定自动装配。 Spring容器在XML配置文件中byName auto-wire属性设置为byName的bean。 然后,它尝试匹配并将其属性与配置文件中由相同名称定义的bean相连。 如果找到匹配,它将注入这些bean。 否则,bean将不会被连线。

例如,如果在配置文件byName bean定义设置为autowire byName ,并且它包含byName属性(即,它具有setSpellChecker (...)方法),则Spring会查找名为setSpellChecker的bean定义,并使用它设置属性。 您仍然可以使用“property”标签连接其余属性。 以下示例将说明该概念。

让我们有一个可用的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>

但是如果您要使用'byName'自动装配,那么您的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 = "byName">
      <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