构造方法注入

  • 具体有3种注入方式:通过构造方法的 a.参数名称注入 b.参数下标注入 c.默认参数顺序注入

参数名称注入

  • School实体类
package com.example.pojo03;

public class School {
private String name;
private String address; @Override
public String toString() {
return "School{" +
"name='" + name + '\'' +
", address='" + address + '\'' +
'}';
} public School(String name, String address) {
this.name = name;
this.address = address;
System.out.println("School有参构造方法执行,实例对象被创建....");
}
}
  • applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>

<!-- bean工厂 -->
<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.xsd"> <!-- 通过构造方法的参数名称,注册School实例对象 -->
<bean id="school" class="com.example.pojo03.School">
<constructor-arg name="name" value="nefu"/>
<constructor-arg name="address" value="哈尔滨"/>
</bean> </beans>
  • 测试
package com.example.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestConstructor { //测试:通过构造方法的参数名称注入
@Test
public void testConstructorArgs(){
//创建Spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("source03/applicationContext.xml");
//取出School对象并打印输出
System.out.printf("School实例对象: " + ac.getBean("school"));
}
}
  • 测试结果
School有参构造方法执行,实例对象被创建....
School实例对象: School{name='nefu', address='哈尔滨'} Process finished with exit code 0

参数下标注入

  • 实体类:新增Student实体类,持有School实例对象的引用
package com.example.pojo03;

public class Student {
private String name;
private int age;
private School school; @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", school=" + school +
'}';
} public Student(String name, int age, School school) {
this.name = name;
this.age = age;
this.school = school;
System.out.println("Student有参构造方法执行,实例对象被创建....");
}
}
  • applicationContext.xml:新增bean标签,注册Student实例对象
    <!-- 通过构造方法的参数下标,注册Student实例对象 -->
<bean id="student" class="com.example.pojo03.Student">
<constructor-arg index="0" value="荷包蛋"/>
<constructor-arg index="1" value="20"/>
<constructor-arg index="2" ref="school"/>
</bean>
  • 测试:新增测试方法
    //测试:通过构造方法的参数下标注入
@Test
public void testConstructorIndex(){
//创建Spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("source03/applicationContext.xml");
//取出Student对象并打印输出
System.out.printf("Student实例对象: " + ac.getBean("student"));
}
  • 测试结果
School有参构造方法执行,实例对象被创建....
Student有参构造方法执行,实例对象被创建....
Student实例对象: Student{name='荷包蛋', age=20, school=School{name='nefu', address='哈尔滨'}} Process finished with exit code 0

默认参数顺序注入

  • applicationContext.xml:新增bean标签,通过构造方法默认参数顺序注册Student实例对象,注意将之前对Student实例对象的注册先注释掉
    <!-- 通过构造方法默认参数顺序,注册Student实例对象 -->
<bean id="student02" class="com.example.pojo03.Student">
<constructor-arg value="荷包蛋"/>
<constructor-arg value="20"/>
<constructor-arg ref="school"/>
</bean>
  • 测试:新增测试方法
    //测试:通过构造方法默认参数顺序注入
@Test
public void testConstructorDefaultOrder(){
//创建Spring容器
ApplicationContext ac = new ClassPathXmlApplicationContext("source03/applicationContext.xml");
//取出Student对象并打印输出
System.out.printf("Student实例对象: " + ac.getBean("student02"));
}
  • 测试结果
School有参构造方法执行,实例对象被创建....
Student有参构造方法执行,实例对象被创建....
Student实例对象: Student{name='荷包蛋', age=20, school=School{name='nefu', address='哈尔滨'}} Process finished with exit code 0

注意

前两种注入方式,由于一种依靠参数名和待注入值绑定,一种依靠参数下标和待注入值绑定,做到了注入值与待注入目标一一对应

所以注入标签顺序随意,调换 < constructor-arg />标签的前后顺序,仍可正确注入数据

    <!-- 通过构造方法的参数下标,注册Student实例对象 -->
<bean id="student" class="com.example.pojo03.Student">
<constructor-arg index="0" value="荷包蛋"/>
<constructor-arg index="2" ref="school"/>
<constructor-arg index="1" value="20"/>
</bean>
  • 但是依靠参数默认顺序注入时,要严格参考实体类中待注入属性的顺序和类型,保证与标签中的待注入值的类型相同,不然会类型解析错误,数据注入失败
    <!-- 通过构造方法默认参数顺序,注册Student实例对象 -->
<bean id="student02" class="com.example.pojo03.Student">
<constructor-arg value="荷包蛋"/>
<constructor-arg ref="school"/>
<constructor-arg value="20"/>
</bean>
  • 容器启动时,School实例对象成功创建并注入数据,但创建Student对象时,待注入数据类型和目标属性类型不对应,类型解析错误,创建失败

Spring 03: 基于xml的构造方法注入的更多相关文章

  1. spring3——IOC之基于XML的依赖注入(DI )

    我们知道spring容器的作用是负责对象的创建和对象间关系的维护,在上一篇博客中我们讲到spring容器会先调用对象的无参构造方法创建一个空值对象,那么接下来容器就会对对象的属性进行初始化,这个初始化 ...

  2. Spring 框架的概述以及Spring中基于XML的IOC配置

    Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...

  3. spring的基于xml的AOP配置案例和切入点表达式的一些写法

    <?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...

  4. Spring中基于xml的AOP

    1.Aop 全程是Aspect Oriented Programming 即面向切面编程,通过预编译方式和运行期动态代理实现程序功能的同一维护的一种技术.Aop是oop的延续,是软件开发中的 一个热点 ...

  5. spring的基于XML方式的属性注入

    1.掌握spring的属性注入的方法: 1.1构造方法注入普通值---------<constructor-arg>标签的使用 首先新建一个类 package spring.day1.de ...

  6. Spring框架第三篇之基于XML的DI注入

    一.注入分类 Bean实例在调用无参构造器创建空值对象后,就要对Bean对象的属性进行初始化.初始化是由容器自动完成的,称为注入.根据注入方式的不同,常用的有两类:设值注入.构造注入.实现特定接口注入 ...

  7. Spring:基于注解的依赖注入的使用

    1.什么是pojo?什么是bean? 首先,在之前几篇Spring的介绍文章当中,自己都提到了一个名词叫做POJO类,但是在回顾Spring的注解的使用的时候,去形容java当中的对象还有一个名词是叫 ...

  8. spring-第十八篇之spring AOP基于XML配置文件的管理方式

    1.在XML配置文件中配置切面.切入点.增强处理.spring-1.5之前只能使用XML Schema方式配置切面.切入点.增强处理. spring配置文件中,所有的切面.切入点.增强处理都必须定义在 ...

  9. Spring学习--基于 XML 的配置声明切面

    正常情况下 , 基于注解的生命要优先于基于 XML 的声明. 通过 AspectJ 注解 , 切面可以与 AspectJ 兼容 , 而基于 XML 的配置则是 Spring 专有的.由于 Aspect ...

随机推荐

  1. App上看到就忍不住点的小红点是如何实现的?

    你有没有发现,我们解锁手机后桌面上App右上角总能看到一个小红点,这就是推送角标.推送角标指的是移动设备上App图标右上角的红色圆圈,圆圈内的白色数字表示未读消息数量.角标是一种比较轻的提醒方式,通过 ...

  2. PyTorch的Variable已经不需要用了!!!

    转载自:https://blog.csdn.net/rambo_csdn_123/article/details/119056123 Pytorch的torch.autograd.Variable今天 ...

  3. Java集合框架(四)-HashMap

    1.HashMap特点 存放的元素都是键值对(key-value),key是唯一的,value是可以重复的 存放的元素也不保证添加的顺序,即是无序的 存放的元素的键可以为null,但是只能有一个key ...

  4. 理论+案例,带你掌握Angular依赖注入模式的应用

    摘要:介绍了Angular中依赖注入是如何查找依赖,如何配置提供商,如何用限定和过滤作用的装饰器拿到想要的实例,进一步通过N个案例分析如何结合依赖注入的知识点来解决开发编程中会遇到的问题. 本文分享自 ...

  5. react的setState到底是同步还是异步

    在介绍这个问题之前,我们先来看一下一个例子: state = {number:1};componentDidMount(){this.setState({number:3})console.log(t ...

  6. SAP FPM 相关包 APB_FPM_CORE

    related interface: APB_FPM_COREAPB_FPM_CORE_4_EXT_SCAPB_FPM_CORE_INTERNALAPB_FPM_CORE_RESTRICTED

  7. SAP 时区转换

    DATA:l_tzone TYPE tzonref-tzone.   "TIME ZONE    DATA:l_timesp TYPE tzonref-tstamps."TIME  ...

  8. django生成迁移文件和执行迁移的命令

    生成迁移文件: python manage.py makemigrations    #创建数据库迁移文件 执行迁移: python manage.py migrate    # 根据数据库迁移文件生 ...

  9. 如何获取GC(垃圾回收器)的STW(暂停)时间?

    前言 在现代的容器化和微服务应用中,因为分布式的环境和错综复杂的调用关系,APM(Application Performance Monitoring 应用性能监控)显得尤为重要,它通过采集应用程序各 ...

  10. 阿里云有奖体验:用PolarDB-X搭建一个高可用系统

    体验简介 场景将提供一台配置了CentOS 8.5操作系统和安装部署PolarDB-X集群的ECS实例(云服务器).通过本教程的操作,带您体验如何使用PolarDB-X搭建一个高可用系统,通过直接ki ...