04 Spring的依赖注入
依赖注入:Dependency Injection。它是 spring 框架核心 ioc 的具体实现。
我们的程序在编写时,通过控制反转,把对象的创建交给了 spring,但是代码中不可能出现没有依赖的情况。
ioc 解耦只是降低他们的依赖关系,但不会消除。例如:我们的业务层仍会调用持久层的方法。
那这种业务层和持久层的依赖关系,在使用 spring 之后,就让 spring 来维护了。
简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。
构造函数注入
顾名思义,就是使用类中的构造函数,给成员变量赋值。注意,赋值的操作不是我们自己做的,而是通过配置的方式,让 spring 框架来为我们注入。具体代码如下:
Car.java
- package com.itzn.model;
- import java.util.Date;
- public class Car {
- private String name;
- private long price;
- private Date buyDate;
- public Car(String _name, long _price, Date _buyDate) {
- name = _name;
- price = _price;
- buyDate = _buyDate;
- }
- public void MyStr() {
- System.out.println("name=" + name + ",price=" + price + ",buyDate=" + buyDate);
- }
- }
bean.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.xsd">
- <!--构造函数注入-->
- <bean id="car" class="com.itzn.model.Car">
- <constructor-arg name="_name" value="逍客"></constructor-arg>
- <constructor-arg name="_price" value=""></constructor-arg>
- <constructor-arg name="_buyDate" ref="now"></constructor-arg>
- </bean>
- <bean id="now" class="java.util.Date"></bean>
- </beans>
SpringTest.java
- public class SpringTest {
- public static void main(String[] args) {
- ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
- Car car=(Car) ac.getBean("car");
- car.MyStr();
- }
- }
使用构造函数的方式,要求:
类中需要提供一个对应参数列表的构造函数。
涉及的标签:
constructor-arg
属性:
index:指定参数在构造函数参数列表的索引位置
type:指定参数在构造函数中的数据类型
name:指定参数在构造函数中的名称 用这个找给谁赋值
=======上面三个都是找给谁赋值,下面两个指的是赋什么值的==============
value:它能赋的值是基本数据类型和 String 类型
ref:它能赋的值是其他 bean 类型,也就是说,必须得是在配置文件中配置过的 bean
优势:在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。
弊端:改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供。
set 方法注入
顾名思义,就是在类中提供需要注入成员的 set 方法。具体代码如下:
Car2.java
- package com.itzn.model;
- import java.util.Date;
- public class Car2 {
- private String name;
- private long price;
- private Date buyDate;
- public void setName(String name) {
- this.name = name;
- }
- public void setPrice(long price) {
- this.price = price;
- }
- public void setBuyDate(Date buyDate) {
- this.buyDate = buyDate;
- }
- public void MyStr() {
- System.out.println("name=" + name + ",price=" + price + ",buyDate=" + buyDate);
- }
- }
bean.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.xsd">
- <!--构造函数注入-->
- <bean id="car" class="com.itzn.model.Car">
- <constructor-arg name="_name" value="逍客"></constructor-arg>
- <constructor-arg name="_price" value=""></constructor-arg>
- <constructor-arg name="_buyDate" ref="now"></constructor-arg>
- </bean>
- <bean id="now" class="java.util.Date"></bean>
- <!--set注入-->
- <bean id="dasauto" class="com.itzn.model.Car2">
- <property name="name" value="朗逸"></property>
- <property name="price" value=""></property>
- <property name="buyDate" ref="now"></property>
- </bean>
- </beans>
SpringTest.java
- package com.itzn.test;
- import com.itzn.model.Car;
- import com.itzn.model.Car2;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class SpringTest {
- public static void main(String[] args) {
- ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
- Car car=(Car) ac.getBean("car");
- Car2 car2=(Car2) ac.getBean("dasauto");
- car.MyStr();
- car2.MyStr();
- }
- }
使用 set 方法的方式
涉及的标签:
property
属性:
name:找的是类中 set 方法后面的部分
ref:给属性赋值是其他 bean 类型的
value:给属性赋值是基本数据类型和 string 类型的
实际开发中,此种方式用的较多。
优势:创建对象时没有明确的限制,可以直接使用默认构造函数
弊端:如果有某个成员必须有值,则获取对象是有可能set方法没有执行。
集合属性注入
顾名思义,就是给类中的集合成员传值,它用的也是set方法注入的方式,只不过变量的数据类型都是集合。
我们这里介绍注入数组,List,Set,Map,Properties。具体代码如下:
Car3.java
- package com.itzn.model;
- import java.util.*;
- public class Car3 {
- private String[] myStrs;
- private List<String> myList;
- private Set<String> mySet;
- private Map<String,String> myMap;
- private Properties myProps;
- public void setMyStrs(String[] myStrs) {
- this.myStrs = myStrs; }
- public void setMyList(List<String> myList) {
- this.myList = myList; }
- public void setMySet(Set<String> mySet) {
- this.mySet = mySet; }
- public void setMyMap(Map<String, String> myMap) {
- this.myMap = myMap; }
- public void setMyProps(Properties myProps) {
- this.myProps = myProps; }
- public void MyStr() {
- System.out.println(Arrays.toString(myStrs));
- System.out.println(myList);
- System.out.println(mySet);
- System.out.println(myMap);
- System.out.println(myProps);
- }
- }
bean.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.xsd">
- <!--构造函数注入-->
- <bean id="car" class="com.itzn.model.Car">
- <constructor-arg name="_name" value="逍客"></constructor-arg>
- <constructor-arg name="_price" value=""></constructor-arg>
- <constructor-arg name="_buyDate" ref="now"></constructor-arg>
- </bean>
- <bean id="now" class="java.util.Date"></bean>
- <!--set注入-->
- <bean id="dasauto" class="com.itzn.model.Car2">
- <property name="name" value="朗逸"></property>
- <property name="price" value=""></property>
- <property name="buyDate" ref="now"></property>
- </bean>
- <!--集合属性注入-->
- <bean id="autos" class="com.itzn.model.Car3">
- <!-- 在注入集合数据时,只要结构相同,标签可以互换 -->
- <!-- 给数组注入数据 -->
- <property name="myStrs">
- <set>
- <value>一汽大众</value>
- <value>上海大众</value>
- <value>一汽奥迪</value>
- </set>
- </property>
- <!-- 注入 list 集合数据 -->
- <property name="myList">
- <array>
- <value>奔驰</value>
- <value>宝马</value>
- <value>奥迪</value>
- </array>
- </property>
- <!-- 注入 set 集合数据 -->
- <property name="mySet">
- <list>
- <value>丰田</value>
- <value>本田</value>
- <value>日产</value>
- </list>
- </property>
- <!-- 注入 Map 数据 -->
- <property name="myMap">
- <props>
- <prop key="dasauto">大众迈腾</prop>
- <prop key="aodi">A6L</prop>
- </props>
- </property>
- <!-- 注入 properties 数据 -->
- <property name="myProps">
- <map>
- <entry key="keyA" value="aaa">
- </entry>
- <entry key="keyB">
- <value>bbb</value>
- </entry>
- </map>
- </property>
- </bean>
- </beans>
SpringTest.java
- package com.itzn.test;
- import com.itzn.model.Car;
- import com.itzn.model.Car2;
- import com.itzn.model.Car3;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class SpringTest {
- public static void main(String[] args) {
- ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
- Car car=(Car) ac.getBean("car");
- Car2 car2=(Car2) ac.getBean("dasauto");
- Car3 car3=(Car3) ac.getBean("autos");
- car.MyStr();
- car2.MyStr();
- car3.MyStr();
- }
- }
复杂类型的注入/集合类型的注入
用于给List结构集合注入标签:
list array set
用于Map结构集合注入的标签:
map props
结构相同,标签可以互换
你喜欢的代码:点击下载
04 Spring的依赖注入的更多相关文章
- 04 Spring框架 依赖注入(一)
整理了一下之前学习spring框架时候的一点笔记.如有错误欢迎指正,不喜勿喷. 上一节我们讲了几个bean的一些属性,用来限制我们实例创建过后的状态. 但是细心的我们会发现其实上面demo创建的实例并 ...
- (spring-第3回【IoC基础篇】)spring的依赖注入-属性、构造函数、工厂方法等的注入(基于XML)
Spring要把xml配置中bean的属性实例化为具体的bean,"依赖注入"是关卡.所谓的"依赖注入",就是把应用程序对bean的属性依赖都注入到spring ...
- Spring的依赖注入(DI)三种方式
Spring依赖注入(DI)的三种方式,分别为: 1. 接口注入 2. Setter方法注入 3. 构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的. 首先我们需要以下几个 ...
- spring的依赖注入的最常见的两种方法
package com.lsz.spring.action; public class User { /** * set注入 */ private String username; public vo ...
- 一步一步深入spring(3)--spring的依赖注入方式
对于spring配置一个bean时,如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象的过程,sprin ...
- spring的依赖注入是什么意思
最近学习spring框架,对依赖注入有些模糊,遂上网翻阅资料,做了下列总结,原博客为CSDN 南夏的 spring的依赖注入是什么意思,侵删! Spring 能有效地组织J2EE应用各层的对象.不管是 ...
- SpringBoot系列: 理解 Spring 的依赖注入(一)
==============================Spring 的依赖注入==============================对于 Spring 程序, Spring 框架为我们提供 ...
- Spring.NET依赖注入框架学习--实例化容器常用方法
Spring.NET依赖注入框架学习---实例化容器常用方法 本篇学习实例化Spring.NET容器的俩种方式 1.通过XmlObjectFactory创建一个Spring.NET容器 IResour ...
- Spring.NET依赖注入框架学习--简单对象注入
Spring.NET依赖注入框架学习--简单对象注入 在前面的俩篇中讲解了依赖注入的概念以及Spring.NET框架的核心模块介绍,今天就要看看怎么来使用Spring.NET实现一个简单的对象注入 常 ...
随机推荐
- springboot2 配置 https
package cn.xiaojf.aibus.configure; import org.apache.catalina.Context; import org.apache.catalina.co ...
- ansible的基础使用(一)
ansible基础使用(一) ansible的主要功能 A:为什么是ansible B:ansible的安装 C:ansible的相关文件 D:ansible的基本使用 ansible的简单操作 A: ...
- 在ensp中RSTP基础设置
为什么我们要有rstp? rstp就是stp的加强版 实验模拟内容 搭建拓扑 相关参数(实验的时候看看自己的mac地址可能与我的并不同) 我们开始配置RSTP基本功能,由于交换机默认开启MSTP,所有 ...
- 小程序云函数,解决接口https问题
本实例只是简单记录http请求 1,云函数如下 // 云函数入口函数 exports.main = async (event, context) => { let req = await got ...
- Docker部署ELK 7.0.1集群之Elasticsearch安装介绍
elk介绍这里不再赘述,本系列教程多以实战干货为主,关于elk工作原理介绍,详情查看官方文档. 一.环境规划 主机名 IP 角色 节点名 centos01 10.10.0.10 es node-10 ...
- c++基础(四)—— 泛型算法
1.find(first, last, value) 头文件:algorithm 参数:前两个参数是“表示元素范围的迭代器”,第三个是一个值 说明:find 将范围中进行寻找.搜索失败:如果范围中无匹 ...
- 深度探索MySQL主从复制原理
深度探索MySQL主从复制原理 一 .概要 MySQL Replication (MySQL 主从复制) 是什么? 为什么要主从复制以及它的实现原理是什么? 1.1 MySQL 主从复制概念 MySQ ...
- 【LEETCODE】52、数组分类,简单级别,题目:717,661,746,628,643,849
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
- [洛谷P4052][JSOI2007]文本生成器
题目大意:有$n$个字符串$s_i$,问有多少个长度为$m$的字符串至少包含$n$个字符串中的一个,字符集 A-Z .$s_i,m\leqslant100,n\leqslant60$ 题解:$AC$自 ...
- 有助于改善性能的Java代码技巧
前言 程序的性能受到代码质量的直接影响.这次主要介绍一些代码编写的小技巧和惯例.虽然看起来有些是微不足道的编程技巧,却可能为系统性能带来成倍的提升,因此还是值得关注的. 慎用异常 在Java开发中,经 ...