详解Java的Spring框架下bean的自动装载方式

作者:goldensun 时间:2022-10-23 08:13:59 

Spring容器可以自动装配相互协作bean之间的关系,这有助于减少对XML配置,而无需编写一个大的基于Spring应用程序的较多的<constructor-arg>和<property>元素。

自动装配模式:
有下列自动装配模式,可用于指示Spring容器使用自动装配依赖注入。使用<bean/>元素的autowire属性为一个bean定义中指定自动装配模式。

byName模式
这种模式规定由自动装配属性名称。Spring容器在外观上自动线属性设置为byName的XML配置文件中的bean。然后,它尝试匹配和接线其属性与配置文件中相同的名称定义的Bean。如果找到匹配项,它会注入这些bean,否则,它会抛出异常。

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

来创建一个Spring应用程序:
这里是TextEditor.java文件的内容:


package com.yiibai;

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.yiibai;

public class SpellChecker {
 public SpellChecker() {
  System.out.println("Inside SpellChecker constructor." );
 }

public void checkSpelling() {
  System.out.println("Inside checkSpelling." );
 }

}

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


package com.yiibai;

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

<!-- Definition for spellChecker bean -->
 <bean id="spellChecker" class="com.yiibai.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.yiibai.TextEditor"
  autowire="byName">
  <property name="name" value="Generic Text Editor" />
 </bean>

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

</beans>

创建源代码和bean配置文件完成后,让我们运行应用程序。如果一切顺利,这将打印以下信息:


Inside SpellChecker constructor.
Inside checkSpelling.

byType模式
式规定由自动装配属性类型。Spring容器在外观上autowire属性设置为byType的XML配置文件中的bean。然后,它尝试匹配和连接一个属性,如果它的类型有完全相同的豆子名称的一个匹配的配置文件。如果找到匹配项,它会注入这些bean,否则,它会抛出异常。

例如,如果一个bean定义设置为自动装配byType的配置文件,它包含拼写检查类型的aspellChecker属性,春季查找名为拼写检查一个bean定义,并用它来设置该属性。仍然可以使用<property>标签接线其余属性。下面的例子将说明这个概念,会发现和上面的例子没有什么区别,除了XML配置文件已被更改。

同样,来创建一个Spring应用程序说明:
这里是TextEditor.java文件的内容:


package com.yiibai;

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.yiibai;

public class SpellChecker {
 public SpellChecker(){
  System.out.println("Inside SpellChecker constructor." );
 }

public void checkSpelling() {
  System.out.println("Inside checkSpelling." );
 }

}

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


package com.yiibai;

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

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

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

</beans>

当创建源代码和bean配置文件完成后,让我们运行应用程序。如果一切顺利,这将打印以下信息:


Inside SpellChecker constructor.
Inside checkSpelling.

由构造函数自动装配
这种模式是非常相似byType,但它应用于构造器参数。 Spring容器在外观上autowire属性被设置XML配置文件中bean的。然后,它尝试匹配和连线它的构造函数的参数与bean名称的配置文件只有一个。如果找到匹配项,它会注入这些bean,否则,它会抛出异常。

例如,如果一个bean定义设置为通过构造配置文件自动装配,它具有与拼写检查类型的参数之一的构造函数,春天寻找一个bean定义namedSpellChecker,并用它来设置构造函数的参数。仍然可以使用<constructor-arg>标签连线剩余的参数。下面的例子将说明这个概念。
这里是TextEditor.java文件的内容:


package com.yiibai;

public class TextEditor {
 private SpellChecker spellChecker;
 private String name;

public TextEditor( SpellChecker spellChecker, String name ) {
  this.spellChecker = spellChecker;
  this.name = name;
 }
 public SpellChecker getSpellChecker() {
  return spellChecker;
 }
 public String getName() {
  return name;
 }

public void spellCheck() {
  spellChecker.checkSpelling();
 }
}

下面是另外一个相关的类文件SpellChecker.java内容:


package com.yiibai;

public class SpellChecker {
 public SpellChecker(){
  System.out.println("Inside SpellChecker constructor." );
 }

public void checkSpelling()
 {
  System.out.println("Inside checkSpelling." );
 }

}

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


package com.yiibai;

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.yiibai.TextEditor">
  <constructor-arg ref="spellChecker" />
  <constructor-arg value="Generic Text Editor"/>
 </bean>

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

</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.yiibai.TextEditor"
  autowire="constructor">
  <constructor-arg value="Generic Text Editor"/>
 </bean>

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

</beans>

创建源代码和bean配置文件完成后,让我们运行应用程序。如果一切顺利,这将打印以下信息:


Inside SpellChecker constructor.
Inside checkSpelling.

除此之外,还有autodetect和默认方式,这里就不再细讲。
自动装配的局限性:
自动装配最好效果是它始终在一个项目中使用。如果自动装配不一般的使用,它可能会被混淆为开发人员可以使用它来连接只有一个或两个bean定义。不过,自动装配可以显著减少需要指定属性或构造器参数,但你应该使用它们之 * 虑自动装配的局限性和缺点。

详解Java的Spring框架下bean的自动装载方式

标签:Java,Spring
0
投稿

猜你喜欢

  • 客户端实现蓝牙接收(C#)知识总结

    2021-11-21 22:17:11
  • Android实现底部导航栏功能(选项卡)

    2022-01-08 21:34:14
  • C#实现String类型和json之间的相互转换功能示例

    2023-06-18 07:35:32
  • C#队列的简单使用

    2022-01-21 03:05:42
  • winform 中显示异步下载的图片

    2022-09-03 19:04:07
  • SpringBoot整合Mybatis实现CRUD

    2022-01-10 09:26:45
  • Android中RecyclerView 滑动时图片加载的优化

    2021-07-29 09:58:12
  • Spring Boot 2结合Spring security + JWT实现微信小程序登录

    2022-07-14 08:25:54
  • Java画笔的简单实用方法

    2021-11-01 05:38:10
  • 你是不是这样写异常处理代码的呢?

    2022-08-08 02:10:10
  • JVM完全解读之GC日志记录分析

    2022-09-22 12:43:08
  • Knife4j 3.0.3 整合SpringBoot 2.6.4的详细过程

    2023-11-25 03:54:34
  • Java关于含有继承类的成员初始化过程讲解

    2022-09-24 23:25:45
  • Android消息机制Handler的工作过程详解

    2023-07-31 13:49:03
  • Logger.error打印错误异常的详细堆栈信息

    2022-01-06 23:03:04
  • Android点击WebView实现图片缩放及滑动浏览效果

    2021-12-12 19:32:48
  • Android GPS定位测试(附效果图和示例)

    2023-05-11 17:38:10
  • Springboot Thymeleaf模板文件调用Java类静态方法

    2023-11-25 05:34:47
  • DUCC配置平台实现一个动态化线程池示例代码

    2023-11-28 12:07:39
  • 重新认识Java的System.in

    2023-08-24 01:55:18
  • asp之家 软件编程 m.aspxhome.com