举报投诉联系我们 手机版 热门标签 名动网
您的位置:名动网 > spring自动装配bean有哪些方式 Spring 自动装配 byName

spring自动装配bean有哪些方式 Spring 自动装配 byName

2023-06-21 13:20 Spring教程

spring自动装配bean有哪些方式 Spring 自动装配 byName

spring自动装配bean有哪些方式 Spring 自动装配 byName

spring自动装配bean有哪些方式

Spring 自动装配 byName

这种模式由属性名称指定自动装配。Spring 容器看作 beans,在 XML 配置文件中 beans 的 auto-wire 属性设置为 byName。然后,它尝试将它的属性与配置文件中定义为相同名称的 beans 进行匹配和连接。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常。

例如,在配置文件中,如果一个 bean 定义设置为自动装配 byName,并且它包含 spellChecker 属性(即,它有一个 setSpellChecker(...) 方法),那么 Spring 就会查找定义名为 spellChecker 的 bean,并且用它来设置这个属性。你仍然可以使用 <property> 标签连接其余的属性。下面的例子将说明这个概念。

让我们在恰当的位置使用 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>

但是,如果你要使用自动装配 “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.tutorialspoint.TextEditor" 
      autowire="byName">
      <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.
阅读全文
以上是名动网为你收集整理的spring自动装配bean有哪些方式 Spring 自动装配 byName全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
  • 技术培训麻辣烫 JSP XML

    技术培训麻辣烫 JSP XML

    2023-05-08 JSP教程

    JSP教程 -JSP XML从JSP发送XML要将XML数据作为页面发送回客户端,请将页面的内容类型设置为text/xml。%@ page contentType=text/...

  •  <c:if> 标签

    <c:if> 标签

    2023-04-25 JSP教程

    c:if 标签 c:if标签判断表达式的值,如果表达式的值为真则执行其主体内容。语法格式c:if test=boolean var=string scope=string ...

  •  <fmt:parseNumber> 标签

    <fmt:parseNumber> 标签

    2023-04-16 JSP教程

    fmt:parseNumber 标签 fmt:parseNumber标签用来解析数字,百分数,货币。语法格式fmt:parseNumbervalue=stringtype=stringpatter...

  • 服务器端响应时间 服务器端响应

    服务器端响应时间 服务器端响应

    2023-05-07 Servlet教程

    服务器 HTTP 响应正如在前面的章节中讨论的一样,当一个 Web 服务器对浏览器响应一个 HTTP 请求时,响应通常包括一个状态行、一...

  • 概述是什么意思 概述

    概述是什么意思 概述

    2023-04-08 Servlet教程

    概述什么是 Servlets?Java servlet 是运行在 Web 或应用服务器上的程序,作为在来自 Web 浏览器或其他 HTTP 客户机的请求和在 H...

© 2024 名动网 mdwl.vip 版权所有 联系我们