Spring 由构造函数自动装配

转载自  Spring 由构造函数自动装配

Spring 由构造函数自动装配

这种模式与 byType 非常相似,但它应用于构造器参数。Spring 容器看作 beans,在 XML 配置文件中 beans 的 autowire 属性设置为 constructor。然后,它尝试把它的构造函数的参数与配置文件中 beans 名称中的一个进行匹配和连线。如果找到匹配项,它会注入这些 bean,否则,它会抛出异常。

例如,在配置文件中,如果一个 bean 定义设置为通过构造函数自动装配,而且它有一个带有 SpellChecker 类型的参数之一的构造函数,那么 Spring 就会查找定义名为 SpellChecker 的 bean,并用它来设置构造函数的参数。你仍然可以使用 <constructor-arg> 标签连接其余属性。下面的例子将说明这个概念。

让我们在恰当的位置使用 Eclipse IDE,然后按照下面的步骤来创建一个 Spring 应用程序:

步骤描述
1创建一个名称为 SpringExample 的项目,并且在已创建的项目的 src 文件夹中创建一个包 com.tutorialspoint
2使用 Add External JARs 选项,添加所需的 Spring 库,在 Spring Hello World Example 章节中已说明。
3在 com.tutorialspoint 包中创建 Java 类 TextEditorSpellChecker 和 MainApp
4在 src 文件夹中创建 Beans 的配置文件 Beans.xml
5最后一步是创建所有 Java 文件和 Bean 配置文件的内容,并运行该应用程序,正如下面解释的一样。

这里是 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/beanshttp://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/beanshttp://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>

一旦你完成了创建源代码和 bean 的配置文件,我们就可以运行该应用程序。如果你的应用程序一切都正常,它将打印下面的消息:

Inside SpellChecker constructor.
Inside checkSpelling.

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/326672.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ssm(Spring+Spring mvc+mybatis)Spring配置文件——applicationContext-servlet.xml

<?xml version"1.0" encoding"UTF-8"?> <beansxmlns"http://www.springframework.org/schema/beans"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xmlns:p"http://www.springframework.org/schema/p"xm…

【南京】.Net 开源基础服务线下技术交流会

南京地区的.net开发人员对基础服务这块感兴趣的&#xff0c;欢迎大家参加及会后继续交流&#xff0c;踊跃参与&#xff01;若对基础服务相关有深度技术交流的&#xff0c;后续交换联系方式&#xff0c;可一起深度合作。 .NET技术行业落地分享交流会 邀请南京地区.NET技术专家和…

mysql 语句块语法_MySQL ------ MySQL常用语句的语法 (三十四)

MySQL常用的语句语法注意&#xff1a;1、 | 符号用来指出几个选中中的一个&#xff0c;因此NULL | NOT NULL 表示给出null 或 not null2、包含在方括号中的关键字或子句是可选的(如 [like this])3、既没有列出所有的MySQL语句&#xff0c;也没有列出每一条子句和选项4、大写的表…

ssm(Spring+Spring mvc+mybatis)Spring配置文件——applicationContext.xml

<?xml version"1.0" encoding"UTF-8"?> <beansxmlns"http://www.springframework.org/schema/beans"xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance"xmlns:p"http://www.springframework.org/schema/p"xm…

图的广度优先算法+遍历

图解 代码实现 package com.atguigu.graph;import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList;/*** 创建人 wdl* 创建时间 2021/4/2* 描述*/ public class Graph {private ArrayList<String> vertexList;//存储顶点集合private int[][…

Spring @Required 注释

转载自 Spring Required 注释 Spring Required 注释 Required 注释应用于 bean 属性的 setter 方法&#xff0c;它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中&#xff0c;否则容器就会抛出一个 BeanInitializationException 异常。下面显示的是一个使用 Requir…

vue父子组件生命周期顺序_vue父子组件生命周期执行顺序

Parent-- Child1-- Child2装载parent beforeCreateparent createdparent beforeMountchild1 beforeCreatechild1 createdchidl1 beforeMountchild2 brforeCreatechild2 createdchild2 beforeMountchild1 mountedchild2 mountedparent mounted更新parent beforeUpdatechild1 bef…

.Net异步编程知多少

1. 引言 最近在学习Abp框架&#xff0c;发现Abp框架的很多Api都提供了同步异步两种写法。异步编程说起来&#xff0c;大家可能都会说异步编程性能好。但好在哪里&#xff0c;引入了什么问题&#xff0c;以及如何使用&#xff0c;想必也未必能答的上来。 自己对异步编程也不是很…

指纹识别开发1.0

在不久之前&#xff0c;用java和C#分别开发了个人脸识别&#xff0c;感觉挺不错的&#xff0c;于是脑袋一发热&#xff0c;想了想能不能搞个指纹识别&#xff0c;答案当然是能&#xff0c;那么问题来了&#xff0c;在人脸识别的时候可以借助自带摄像头提取你的face&#xff0c;…

Spring @Autowired 注释

转载自 Spring Autowired 注释 Spring Autowired 注释 Autowired 注释对在哪里和如何完成自动连接提供了更多的细微的控制。 Autowired 注释可以在 setter 方法中被用于自动连接 bean&#xff0c;就像 Autowired 注释&#xff0c;容器&#xff0c;一个属性或者任意命名的可…

DFS VS BFS

实际案例 代码实现 package com.atguigu.graph;import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList;/*** 创建人 wdl* 创建时间 2021/4/2* 描述*/ public class Graph {private ArrayList<String> vertexList;//存储顶点集合private in…

CLR运行时细节 - Method Descriptor

方法描述符:MethodDesc 运行时用来描述类型的托管方法,它保存在方法描述桶(MethodDescChunk)内;方法描述符保存了方法在运行时的一些重要信息:是否JIT编译;是否有方法表槽(决定了方法入口是跟在方法描述符(MethodDesc)后还是在方法表(MethodTable)后面);距离MethodDescChunk的索…

beanutil 批量copy_BeanUtils.copyProperties 需要getset方法支持

今天在调用这个方法时&#xff0c;发现属性没有映射上&#xff0c;结果一看是model类没有加上getset方法 PropertyDescriptor[] targetPds getPropertyDescriptors(actualEditable); List ignoreList (ignoreProperties ! null ? Arrays.asList(ignoreProperties) : null);f…

ssm(Spring+Spring mvc+mybatis)mybatis配置文件——mybatis-config.xml

<?xml version"1.0" encoding"UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration><settings><!-- 打印查…

2015蓝桥杯省赛---java---B---3(三羊献瑞)

题目 三羊献瑞 思路分析 由于是填空题&#xff0c;没有时间和内存的要求&#xff0c;所以看到这个题&#xff0c;第一想法就是暴力破解&#xff0c;当然了&#xff0c;怎么快就怎么做。 由于 "三"是数字的首位&#xff0c;低位的数字进位后必然为1&#xff0c;所…

Spring @Qualifier 注释

转载自 Spring Qualifier 注释 Spring Qualifier 注释 可能会有这样一种情况&#xff0c;当你创建多个具有相同类型的 bean 时&#xff0c;并且想要用一个属性只为它们其中的一个进行装配&#xff0c;在这种情况下&#xff0c;你可以使用 Qualifier 注释和 Autowired 注释通…

mysql common是什么_MySQL common_schema简介

common_schema为MySQL提供了查询脚本&#xff0c;分析并且信息化的视图和一个函数库&#xff0c;以便更容易的管理和诊断。它引入的一些基于SQL的工具简common_schema的简介&#xff1a;Shlomi Noach 的common_schema项目()是一套针对服务器脚本化和管理的强大的代码和视图。co…

ssm(Spring+Spring mvc+mybatis)——web.xml

<?xml version"1.0" encoding"UTF-8"?> <web-app version"3.0" xmlns"http://java.sun.com/xml/ns/javaee" xmlns:xsi"http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation"http://java.sun.co…

数据库权限分配探讨

上周&#xff0c; 有位朋友给我提出了这样的需求&#xff1a;区分用户访问数据库的权限。顺便总结了下有如下要求&#xff1a; 某个用户查询所有数据库的权限 某个用户只有备份数据库的权限 给一个用户只能查看指定数据库的权限 给一个用户只有某个表的权限 要进行以上任务&…

2015蓝桥杯省赛---java---B---6(加法变乘法)

题目 加法变乘法 思路分析 两个算式进行相减操作 代码实现 package com.atguigu.lanqiao;public class Main { // 简单枚举public static void main(String[] args) {for (int i 1; i < 46; i) {for (int j i 2; j < 48; j) {if (i * (i 1) - (i i 1) j * (…