依赖注入(DI)的三种方式
个人对依赖注入的理解是:为bean所代表的对象的属性赋值 。对象的属性有多种类型,主要包括基本数据类型、String、引用类型、集合类型、属性类型、空字符串以及空值null。在通过配置文件进行依赖注入时,主要有以下三种配置方式:
1. 有参构造器注入
这种方式的前提是,在bean所对应的类中显式定义有参构造函数,涉及到的属性才能够使用<constructor-arg/>标签进行赋值。具体参照链接Spring IOC(控制反转)_鲸鱼-D的博客-CSDN博客中IOC创建对象方式中的方式2。
2.Set方式注入(重点)
前提: 类中有无参构造函数(默认or显式定义)以及setter方法。
- Student类:类中的成员属性包括各种数据类型的变量
public class Student {private String name;private Address address;private String[] books;private List<String> hobbies;private Map<String, String> card;private Set<String> games;private String wife1;private String wife2;private Properties info;public String getName() {return name;}public void setName(String name) {this.name = name;}public Address getAddress() {return address;}public void setAddress(Address address) {this.address = address;}public String[] getBooks() {return books;}public void setBooks(String[] books) {this.books = books;}public List<String> getHobbies() {return hobbies;}public void setHobbies(List<String> hobbies) {this.hobbies = hobbies;}public Map<String, String> getCard() {return card;}public void setCard(Map<String, String> card) {this.card = card;}public Set<String> getGames() {return games;}public void setGames(Set<String> games) {this.games = games;}public String getWife1() {return wife1;}public void setWife1(String wife1) {this.wife1 = wife1;}public String getWife2() {return wife2;}public void setWife2(String wife2) {this.wife2 = wife2;}public Properties getInfo() {return info;}public void setInfo(Properties info) {this.info = info;}@Overridepublic String toString() {return "Student{" +"name='" + name + '\'' +", address=" + address.toString() +", books=" + Arrays.toString(books) +", hobbies=" + hobbies +", card=" + card +", games=" + games +", wife1='" + wife1 + '\'' +", wife2='" + wife2 + '\'' +", info=" + info +'}';} }
- 配置文件
<?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.xsd"><bean id="address" class="com.jing.pojo.Address"><property name="address" value="Beijing"/></bean><bean id="student" class="com.jing.pojo.Student"><!--基本数据类型herString注入--><property name="name" value="YuJing"/><!--引用类型注入--><property name="address" ref="address"/><!--数组类型注入--><property name="books"><array value-type="java.lang.String"><value>红楼梦</value><value>西游记</value><value>水浒传</value><value>三国演义</value></array></property><!--单列集合List注入--><property name="hobbies"><list value-type="java.lang.String"><value>看综艺</value><value>敲代码</value><value>听歌</value></list></property><!--双列结合Map注入--><property name="card"><map key-type="java.lang.String" value-type="java.lang.String"><entry key="YuJing" value="123456"/><entry key="JingYu" value="654321"/></map></property><!--单列结合Set--><property name="games"><set value-type="java.lang.String"><value>LOL</value><value>COC</value><value>BOB</value></set></property><!--空值null--><property name="wife1"><null/></property><!--空字符串注入--><property name="wife2" value=""/><!--Properties注入--><property name="info"><props><prop key="driver">com.mysql.jdbc.driver</prop><prop key="url">http://mysql</prop><prop key="username">root</prop><prop key="password">root</prop></props></property></bean></beans>
3. 扩展方式注入
为了简化上述两种配置方式,Spring为我们提供了与上述两种方式分别对应的扩展方式:
- c命名空间:简化方式1,除了方式1中的前提以外,还需要在配置文件中加入约束xmlns:c="http://www.springframework.org/schema/c"
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:c="http://www.springframework.org/schema/c"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--C命名空间注入,可以通过构造器注入:construct-args--><bean id="student1" class="com.jing.pojo.Student" c:name="郭靖" c:wife="黄蓉"></bean></beans>
- p命名空间:简化方式2,除了方式2中的前提以外,还需要再配置文件中加入约束xmlns:p="http://www.springframework.org/schema/p"
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--P命名空间注入,可以通过构造器注入:property--><bean id="student2" class="com.jing.pojo.Student" p:name="郭靖" p:wife="黄蓉"></bean></beans>
依赖注入(DI)的三种方式相关推荐
- 框架源码系列九:依赖注入DI、三种Bean配置方式的注册和实例化过程
一.依赖注入DI 学习目标 1)搞清楚构造参数依赖注入的过程及类 2)搞清楚注解方式的属性依赖注入在哪里完成的. 学习思路 1)思考我们手写时是如何做的 2)读 spring 源码对比看它的实现 3) ...
- Spring 使用@Import注入Bean的三种方式
一.准备工作 1.导入spring-context依赖 <dependency><groupId>org.springframework</groupId>< ...
- C#依赖注入常用的几种方式
1.构造器注入 通过构造器进行依赖注入 public class MyClass { private IMyInterFace _myinterface; public MyClass (IMyInt ...
- Spring: Spring依赖注入有哪几种方式?
1.属性注入 通过属性注入,比如用@Autowired.@Resource这些注解 @Autowire private ExampleService exampleServiceImpl; 对象的外部 ...
- spring依赖注入的三种方式以及优缺点
spring依赖注入的三种方式以及优缺点 一.依赖注入的三种方式 1.通过构造器注入.(spring4.3之后,推荐使用) 2.通过setter注入.(spring4.3之前,推荐使用) 3通过fil ...
- 控制反转(Ioc)和依赖注入(DI)
控制反转IOC, 全称 "Inversion of Control".依赖注入DI, 全称 "Dependency Injection". 面向的问题:软件开发 ...
- Spring_01 spring容器、控制反转(IOC)、依赖注入(DI)
目录 1 什么是spring框架 2 spring框架的特点 3 spring容器 3.1 什么是spring容器 3.2 spring容器创建对象的编程步骤 3.4 spring容器创建对象的方式 ...
- IOC控制反转的三种方式
IOC控制反转三种方式 (1).在介绍之前,来了解一下注入的概念:IoC(控制反转)即依赖注入,就是指程序在运行过程中,如果需要另外一个对象协助完成时,无需在代码中创建被调用者,而是依赖外部的注入获取 ...
- Spring MVC 实战:三种方式获取登录用户信息
前言 Web 项目中,维持用户登录状态的常用方式有三种,分别是 Cookie.Session.Token,不管哪种方案,都需要获取到用户信息供业务层使用. 由于获取用户信息与具体业务无关,因此在普通的 ...
- Spring学习3 - Bean的属性、DI依赖注入三种方式
文章目录 1. 属性设置 - XML配置文件.以及与其等价的注解形式 2. 依赖注入DI的方式 2.1 属性自动注入 - 不推荐 代码准备阶段 代码测试 2.2 子节点构造器注入 - 不推荐 2.3 ...
最新文章
- openstack之horizon源码分析之二
- 方法描述Mybatis使用之NEIS项目
- java swing点击按钮后输出结果展示_使用Eclipse编写第一个Java程序HelloWorld
- Median String
- 第八章 springboot + mybatis + 多数据源
- java常用的框架介绍
- 更新 hadoop eclipse 插件
- Firemonkey下使用StyleBook的一些经验
- Linux下的hostid
- EDI REMADV报文详解
- Matlab数学建模学习报告(一)
- Linux------进程概念、进程控制
- Source Files 与 Resource Files 的区别
- 一款简洁的导航网源码
- 从别人那copy点学习资料
- nginx、php-fpm以及mysql运行在各个用户下的配置
- Java集合(一)Java集合及其关系
- Python学习教程(Python学习路线):Day14A-网络编程入门
- 【前端修炼场】 — 这些标签你学会了么?快速拿下 “hr”
- 【科普】微波雷达感应与人体红外感应的区别
热门文章
- oracle添加外键约束
- VHDL语言的概述(诞生与发展)
- HDU2059龟兔赛跑
- AG1280 CPLD Q48Q32 with PLL
- C++ 生产者消费者模式
- 【甘道夫】Hive 0.13.1 on Hadoop2.2.0 + Oracle10g部署详细解释
- 周立功CAN上位机软件使用教程
- Out of Memory ——pycharm内存不足造成软件卡爆
- linux限制目录访问权限,linux用户的访问权限限制的方法都有哪些
- PySOT测试过程中OTB100.josn找不到OTB2015数据集的Jogging-1、Jogging-2、Skating2-1、Skating2-2、Human4-2