详解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定义。不过,自动装配可以显著减少需要指定属性或构造器参数,但你应该使用它们之 * 虑自动装配的局限性和缺点。
猜你喜欢
- 想在Linux进行JAVA开发吗?环境如何搭建,第一个HelloWorld如何实现,下面马上奉献:1、环境搭建1.1 Java JDK 的安
- 1、引入依赖<dependency> <groupId>org.apache.pdfbox</gr
- 封面图下个季度的目标是把前端监控相关的内容梳理出来,梳理出来之后可能会在公司内部做个分享~Flutter应用程序既括代码也包括一些其他的资产
- java 单例的五种实现方式及其性能分析序言在23种设计模式中,单例是最简单的设计模式,但是也是很常用的设计模式。从单例的五种实现方式中我们
- 若代理类在程序运行前就已经存在,那么这种代理方式被成为 静态代理 ,这种情况下的代理类通常都是我们在Java代码中定义的。 通常情况下, 静
- 方式一public class Test{ public static void main(String[] args) throws Ex
- 读取文件流时,经常会遇到乱码的现象,造成乱码的原因当然不可能是一个,这里主要介绍因为文件编码格式而导致的乱码的问题。首先,明确一点,文本文件
- 1、SpringMVC验证@Validated的使用第一步:编写国际化消息资源文件编写国际化消息资源ValidatedMessage.pro
- 面试题1:你了解线程池么?简单介绍一下。java提供的一个java.util.concurrent.Executor接口的实现用于创建线程池
- Java调用Oracle存储过程详解步骤:1、编写Oracle存储过程2、编写数据库获取连接工具类3、编写简单应用调用存储过程实现:1、Or
- java 8引入了lambda表达式,lambda表达式实际上表示的就是一个匿名的function。在java 8之前,如果需要使用到匿名f
- 前言通过深入分析Spring源码,我们知道Spring框架包括大致六大模块, 如Web模块,数据库访问技术模块,面向切面模块,基础设施模块,
- SpringBootWeb开发回顾一下:springboot帮助我们配置了什么,能不能进行修改,能修改哪些,能否扩展?xxxAutoConf
- 一、引言Good Good Study,Day Day Up,童鞋点个关注,不迷路,么么哒~~~MP自带的条件构造器虽然很强大,有时候也避免
- JDK12的五大重要新特性Java12在March 19, 2019发布了。在2017年发布Java 9之后,Java平台发布节奏已从每3年
- 1.导入jar包: <!--jmsTemplate--> <dependency> <
- 一、导入和导出导入:通过解析excel表格中的数据,然后将数据放到一个集合中,接着通过对持久层操作,将数据插入到数据库中,再加载一下页面,从
- 前文本章是关于Java流程控制语句的最全汇总,本篇为汇总中篇。流程是人们生活中不可或缺的一部分,它表示人们每天都在按照一定的流程做事。比如出
- idea spring Initializr创建项目勾选项目所需要的依赖pom.xml文件会加载勾选的依赖,也可以不勾选后面通过自己常用的p
- 所谓文件的断点续传,就是一个线程传输文件,另一个线程控制传输标识,以达到暂停文件效果、恢复文件上传的效果。本demo使用最基本的线程之间的通