关键词:
文章目录
Spring Beans 自动装配
- 可以使用·
bean
元素来声明 bean 和通过使用 XML 配置文件中的<constructor-arg>和<property>元素来注入 。
- Spring容器可以在不使用和 元素的情况下自动装配相互协作的 bean 之间的关系,这会减少XML配置的数量
- 自动装配模式 Spring 容器为来使用自动装配进行依赖注入
- 可以使用bean元素的autowire属性为一个bean定义指定自动装配模式
- bean的装配模式有以下几种
模式 | 描述 |
---|---|
no | 默认的设置,没有自动装配 |
byName | 由属性名自动装配,Spring容器在看到XML配置文件中bean的自动装配的属性设置为byName,然后尝试匹配,并且将它的属性与在配置文件中被定义为相同名称的 beans 的属性进行连接。 |
byType | 由属性数据类型自动装配。Spring 容器看到在 XML 配置文件中 bean 的自动装配的属性设置为 byType。然后如果它的类型匹配配置文件中的一个确切的 bean 名称,它将尝试匹配和连接属性的类型。如果存在不止一个这样的 bean,则一个致命的异常将会被抛出。 |
constructor | 类似于 byType,但该类型适用于构造函数参数类型。如果在容器中没有一个构造函数参数类型的 bean,则一个致命错误将会发生。 |
autodetect | Spring首先尝试通过 constructor 使用自动装配来连接,如果它不执行,Spring 尝试通过 byType 来自动装配。 |
Spring自动装配的局限性
- 当自动装配始终在同一个项目中使用时,效果是最好的
- 如果不是使用自动装配,可能会使我们使用它来连接只有一个或者两个bean定义
- 自动装配可以显著减少需要指定的属性或者构造器参数
限制 | 描述 |
---|---|
重写的可能性 | 可以使用总是重写自动装配的 和 设置来指定依赖关系。 |
原始数据类型 | 不能自动装配所谓的简单类型包括基本类型,字符串和类。 |
混乱的本质 | 自动装配不如显式装配精确,所以如果可能的话尽可能使用显式装配。 |
Spring 自动装配byName
- byName模式由属性名称指定自动装配。Spring容器看作beans,在 XML 配置文件中 beans 的 auto-wire 属性设置为 byName,然后,它尝试将它发属性和配置文件定义为相同命名称的beans进行匹配和连接。如果找到匹配项,它将注入这些 beans,否则,它将抛出异常。
- 在配置文件中,如果一个Bean定义设置为自动装配byName,并且它包含 spellChecker 属性(即,它有一个 setSpellChecker(…) 方法),那么 Spring 就会查找定义名为 spellChecker 的 bean,并且用它来设置这个属性。而且可以使用·
<property>
标签连接其余的属性
Spring 自动装配byName样例
- 创建一个名称为 SpringExample 的项目,并且在已创建的项目的 src 文件夹中创建一个包 com.tutorialspoint。
- 使用 Add External JARs 选项,添加所需的 Spring 库,
- 在 com.tutorialspoint 包中创建 Java 类 TextEditor,SpellChecker 和 MainApp
- 在 src 文件夹中创建 Beans 的配置文件 Beans.xml。
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();
不适用自动装配byName的配置文件 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>
运行该应用程序,输出结果为
Inside SpellChecker constructor.
Inside checkSpelling.
Spring自动装配byType
- byType模式由属性类型指定自动装配,Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 byType,如果它的 type 恰好与配置文件中 beans 名称中的一个相匹配,它将尝试匹配和连接它的属性,如果找到匹配项,它将注入这些 beans,否则,它将抛出异常
- 在配置文件中,如果一个 bean 定义设置为自动装配 byType,并且它包含 SpellChecker 类型的 spellChecker 属性,那么 Spring 就会查找类型为 SpellChecker 的 bean并且用它来设置这个属性,仍然可以使用 标签连接其余属性。
Spring自动装配byType样例
- 创建一个名称为 SpringExample 的项目,并且在已创建的项目的 src 文件夹中创建一个包 com.tutorialspoint。
- 使用 Add External JARs 选项,添加所需的 Spring 库,
- 在 com.tutorialspoint 包中创建 Java 类 TextEditor,SpellChecker 和 MainApp。
- 在 src 文件夹中创建 Beans 的配置文件 Beans.xml
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 的内容:**
```c
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>
使用自动装配 “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.tutorialspoint.TextEditor"
autowire="byType">
<property name="name" value="Generic Text Editor" />
</bean>
<!-- Definition for spellChecker bean -->
<bean id="SpellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
Spring 由构造函数自动装配
- 由构造函数自动装配模式与 byType 非常相似,但它应用于构造器参数。Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 constructor。然后,它尝试把它的构造函数的参数与配置文件中 beans 名称中的一个进行匹配和连线。如果找到匹配项,它会注入这些 bean,否则,它会抛出异常
- 在配置文件中,如果一个 bean 定义设置为通过构造函数自动装配,而且它有一个带有 SpellChecker 类型的参数之一的构造函数,那么 Spring 就会查找定义名为 SpellChecker 的 bean,并用它来设置构造函数的参数。但是仍然可以使用
<constructor-arg>
标签连接其余属性。
Spring 由构造函数自动装配
- 创建一个名称为 SpringExample 的项目,并且在已创建的项目的 src 文件夹中创建一个包 com.tutorialspoint。
- 使用 Add External JARs 选项,添加所需的 Spring 库,
- 在 com.tutorialspoint 包中创建 Java 类 TextEditor,SpellChecker 和 MainApp。
- 在 src 文件夹中创建 Beans 的配置文件 Beans.xml
TextEditor.java 文件的内容:
package com.tutorialspoint;
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.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">
<constructor-arg ref="spellChecker" />
<constructor-arg value="Generic Text Editor"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
使用自动装配 “by constructor”,的 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="constructor">
<constructor-arg value="Generic Text Editor"/>
</bean>
<!-- Definition for spellChecker bean -->
<bean id="SpellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
高性能云服务器
精品线路独享带宽,毫秒延迟,年中盛惠 1 折起
spring入门(代码片段)
文章目录SpringBeans自动装配Spring自动装配的局限性Spring自动装配byNameSpring自动装配byName样例Spring自动装配byTypeSpring自动装配byType样例Spring由构造函数自动装配Spring由构造函数自动装配SpringBeans自动装配可以使用·bean元素来声明bean... 查看详情
spring入门案例(简单)(代码片段)
Spring的入门案例(简单)该案例主要用来概述使用SpringIOC方式创建对象并调用方法,希望对大家有所帮助Spring的概述什么是Spring:Spring是分层的JavaSE/EE应用full-stack轻量级开源框架Spring的两大核心:IOC(InverseOfCon... 查看详情
springboot入门到精通-spring的注解编程(代码片段)
SpringBoot入门到精通系列SpringBoot入门到精通-Spring的注解编程(一)SpringBoot入门到精通-SpringBoot入门(二)SpringBoot入门到精通-Spring的基本使用(三)SpringBoot入门到精通-SpringBoot集成SSM(四)前言SpringBoot并不是一个新技术了,但是我发现... 查看详情
springboot入门到精通-spring的基本使用(代码片段)
SpringBoot入门到精通系列SpringBoot入门到精通-Spring的注解编程(一)SpringBoot入门到精通-SpringBoot入门(二)SpringBoot入门到精通-Spring的基本使用(三)SpringBoot入门到精通-SpringBoot集成SSM(四)前言上一篇文章我们讲的是SpringBoot的入门程序,... 查看详情
spring入门:使用maven管理spring项目(代码片段)
让我们先回顾下本系列的前3篇博客:Spring入门(一):创建Spring项目Spring入门(二):自动化装配beanSpring入门(三):通过JavaConfig装配bean1.为什么要使用Maven?之前我们新建的项目,项目所要依赖的第三方jar包都在项目的类路径下(通常... 查看详情
spring学习-ioc-入门(代码片段)
在讲spring的IOC之前,我们先看一个示例三层视图的示例packagebean1;//service层publicclassCallServiceprivatestaticCallDaocallDao=newCallDao();publicvoidcall()System.out.println("这是CallService");callDao.save( 查看详情
spring事务快速入门详解(代码片段)
文章目录Spring事务简介什么是事务?事务特征(ACID)Spring事务的作用Spring事务核心接口Spring事务角色Spring事务实战分析需求和分析代码实现测试环境准备使用Spring事务管理修改案例在业务层接口上添加Spring事务管理... 查看详情
spring入门spring-test模块(代码片段)
自动化转配bean的测试案例分析packagesoundsystem;importstaticorg.junit.Assert.*;importorg.junit.Rule;importorg.junit.Test;importorg.junit.contrib.java.lang.system.StandardOutputStreamLog;importorg.junit.runner.RunW 查看详情
java--spring入门(代码片段)
一、Spring简介(一)简介Spring是于2003年兴起的一个轻量级的Java开发框架,它是为了解决企业应用开发的复杂性而创建的Spring的核心是控制反转(IoC)和面向切面编程(AOP)。Spring是可以在JavaSE/EE中使... 查看详情
spring5框架概述入门案例(代码片段)
1、Spring5框架概述1、Spring是==轻量级==的开源的JavaEE框架2、Spring可以解决企业应用开发的复杂性3、Spring有两个核心部分:==IOC==和==Aop==(1)IOC:控制反转,把创建对象过程交给Spring进行管理(2)Aop:面向切面,不修改源代码进... 查看详情
spring-boot入门(代码片段)
依赖我们使用IDE初始化spring-boot的时候,会发现这两个依赖<artifactId>spring-boot-starter-web</artifactId>//spring-boot针对web的依赖<artifactId>spring-boot-starter-test</artifactId>//用来做单元测试还有sprin 查看详情
spring——入门(代码片段)
Spring是什么?Struts是Web框架Hibernate是ORM框架,处于持久层Spring是容器框架,用于配置Bean,并维护Bean之间关系的一种框架。Spring中有一个非常重要的概念:Bean,Bean是Java中的任何一种对象,它可以是JavaB... 查看详情
玩转spring——spring入门基础(代码片段)
什么是SpringSpring是一个开源的的轻量级的应用开发框架,其目的是用来简化企业级应用程序开发,减少代码之间的侵入性。Spring提供的IOC和AOP应用,可以将组件的耦合度将至最低,即我们常说的解耦,这样能便于日后系统的升级... 查看详情
spring快速入门——从理论到第一个程序详细总结(代码片段)
目录一、spring简介1、什么是spring2、简单理解:3、spring的优势4、spring的体系结构①CoreContainer(核心容器)②DataAccess/Integration(数据访问/集成)③web④其他模块二、Spring的下载安装1、下载2、spring目录结构3、配置(可跳... 查看详情
springmvc入门系列-springmvc简介与入门(代码片段)
SpringMVC简介与入门什么是SpringMvc?SpringMVC:是基于spring的一个框架,实际上就是spring的一个模块。专门是做web开发的。理解是servlet的一个升级web开发底层是servlet,框架是在servlet基础上面加入一些功能,让你做veb开发方便。springMV... 查看详情
的c友,都能快速入门spring(代码片段)
Spring快速入门1.spring1.1.优点1.2.缺点1.3.Spring框架的组成结构图1.3.1.核心容器1.3.2.AOP和设备支持1.3.3.数据访问与集成1.3.4.Web1.3.5.消息(Messaging)1.3.6.Test2.Spring核心ioc2.1.ioc容器2.1.BeanFactory容器2.1.2\\.ApplicationContext容器2.1.2.1.Cl 查看详情
springboot入门到精通-springboot入门(代码片段)
SpringBoot入门到精通系列SpringBoot入门到精通-Spring的注解编程(一)SpringBoot入门到精通-SpringBoot入门(二)SpringBoot入门到精通-Spring的基本使用(三)SpringBoot入门到精通-SpringBoot集成SSM(四)前言经过上面的学习,我们已经掌握的Spring的注... 查看详情
spring入门(代码片段)
...系作者获得授权,非商业转载请注明出处。前言随着Spring的崛起以及其功能的完善,现在可能绝大部分项目的开发都是使用Spring(全家桶)来进行开发, 查看详情