IoC Di

Di 指的是bean之间的依赖注入,设置对象之间的级联关系

Classes:

package com.southwind.entity;

import lombok.Data;

@Data
public class Classes {
private Integer id;
private String name; }

Student:

package com.southwind.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor; @Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private Integer id;
private String name;
private Integer age;
private Classes classes;
}

spring-di.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">
<!--Classes-->
<bean id ="classes" class="com.southwind.entity.Classes">
<property name="id" value="1"></property>
<property name="name" value="1班"></property>
</bean>
<!-- Student-->
<bean id ="student" class="com.southwind.entity.Student">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
<property name="age" value="18"></property>
<property name="classes" ref="classes"></property>
</bean>
</beans>

Test2:

package com.southwind.test;

import com.southwind.entity.Classes;
import com.southwind.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test2 {
public static void main(String[] args) {
//IoC容器自动创建对线,开发者只需要取出对象即可
ApplicationContext applicationContext =new ClassPathXmlApplicationContext("spring-di.xml");
// String[] names = applicationContext.getBeanDefinitionNames();
// for (String name:names){
// System.out.println(name);
// }
Student student = (Student)applicationContext.getBean("student");
Classes classes =(Classes)applicationContext.getBean("classes");
System.out.println(student);
}
}

Bean对象的级联学要使用ref属性来完成映射,而不能直接使用value否值会抛出类型转化异常:

Classes:

package com.southwind.entity;

import lombok.Data;

import javax.swing.*;
import java.util.List; @Data
public class Classes {
private Integer id;
private String name;
private List<Student> studentList; }

spring-di.xml:

<!--Classes-->
<bean id ="classes" class="com.southwind.entity.Classes">
<property name="id" value="1"></property>
<property name="name">
<value><![CDATA[<一班>]]></value>
</property>
<property name="studentList">
<list>
<ref bean="student" ></ref>
<ref bean="student1" ></ref>
</list>
</property>
</bean>

但是要注意不能闭环

spring中的Bean

bean 是根据scope 来生成的,表示bean的作用域,scope有4种“

  1. singleton ,单例,表示通过Spring容器获取对象的唯一性 默认

    (无论取不取,只要加载IoC容器,就创建对象)
  2. prototype, 原型,表示通过Spring容器获取的对象是不同的

    (如果不取Bean就不创建对象,取几个就创建几个对象)
  3. request ,请求,表示在一次HTTP请求内有效
  4. session ,会话,表示在一个用户内有效

    requestion、session仅在web中有效

Spring的继承

Spring的继承不同于Java ,区别:java中的继承是真对类的,SPring中的继承是针对对象的(Bean)

Spring的继承中,子Bean可以以继承父Bean中的值

通过parent设置继承关系,同时值bean的值来继承和覆盖

<bean id="user1" class="com.southwind.entity.User" scope="prototype" >
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="user" class="com.southwind.entity.User" scope="prototype" parent="user1">
<property name="name" value="李四"></property>
</bean>

即使是两个不同的类,只是赋值。(属性名一样必须,不一样抛异常)(只要成员变量一样就行)

Spring的依赖:

用来设置两个Bean的顺序

IoC容器默认情况是通过spring.xml中bean的配置顺序来决定创建的顺序

在不修改spring.xml来depends-on=""修改

<bean id="user" class="com.southwind.entity.User" depends-on="student">

</bean>
<bean id="student" class="com.southwind.entity.Student"></bean>

Spring(Ioc DI、Spring的继承-依赖)的更多相关文章

  1. Spring IOC&DI 控制反转和依赖注入

    控制反转(Inversion of Control,缩写为IOC),它是把你设计好的对象交给spring控制,而不再需要你去手动 new Object(); 网上对于IOC的解释很多,对程序员而言,大 ...

  2. Spring+IOC(DI)+AOP概念及优缺点

    Spring pring是一个轻量级的DI和AOP容器框架. 说它轻量级有一大部分原因是相对与EJB的(虽然本人从没有接触过EJB的应用),重要的是,Spring是非侵入式的,基于spring开发的应 ...

  3. IOC/DI控制反转与依赖注入

    IOC/DI控制反转与依赖注入 IOC和DI表现的效果的是一样的只不过对于作用的对象不同,有了不一样的名字. 先用一个现实的例子来说明IOC/DI表现出来的效果.

  4. 黑马-Spring(IOC&DI) AOP

    IOC(控制翻转) 概念 把对象的创建.初始化.销毁等工作交给spring容器来做 案例 环境 步骤 1.  写一个HelloWorld类 2.  写一个配置文件   把hello类放到spring容 ...

  5. spring IOC DI AOP MVC 事务, mybatis 源码解读

    demo https://gitee.com/easybao/aop.git spring DI运行时序 AbstractApplicationContext类的 refresh()方法 1: pre ...

  6. 零基础学习java------37---------mybatis的高级映射(单表查询,多表(一对一,一对多)),逆向工程,Spring(IOC,DI,创建对象,AOP)

    一.  mybatis的高级映射 1  单表,字段不一致 resultType输出映射: 要求查询的字段名(数据库中表格的字段)和对应的java类型的属性名一致,数据可以完成封装映射 如果字段和jav ...

  7. spring ioc DI 理解

    下面是我从网上找来的一些大牛对spring ioc和DI的理解,希望也能让你对Spring ioc和DI的设计思想有更进一步的认识. 一.分享Iteye的开涛对Ioc的精彩讲解 Ioc—Inversi ...

  8. Spring Ioc DI 原理

    IOC(DI):其实这个Spring架构核心的概念没有这么复杂,更不像有些书上描述的那样晦涩.Java程序员都知道:java程序中的每个业务逻辑至少需要两个或以上的对象来协作完成,通常,每个对象在使用 ...

  9. Spring系列(二):Spring IoC/DI的理解

    这几天重新学习了一下Spring,在网上找了相关的ppt来看,当看到Spring IoC这一章节的时候,先大致浏览了一下内容,有将近50页的内容,内心窃喜~QAQ~,看完这些内容能够对IoC有更深层次 ...

  10. spring Ioc/DI的理解

    学习spring的童鞋都知道,spring中有两个非常重要的点,Ioc(控制反转)与DI(依赖注入),对于初级玩家来说,这两个概念可能有点模棱两可的感觉,今天就谈下自己的一点理解,不足请多多指教!!! ...

随机推荐

  1. 第2-4-5章 规则引擎Drools高级语法-业务规则管理系统-组件化-中台

    目录 6. Drools高级语法 6.1 global全局变量 6.2 query查询 6.3 function函数 6.4 LHS加强 6.4.1 复合值限制in/not in 6.4.2 条件元素 ...

  2. GitHub上的一个笔记相关小项目

    就是一个笔记屑小项目, C++编写,有想一起开发的私信 AlgorithWeaver/V-note (github.com) 项目名V-note QVQ

  3. 【JVM调优】Day02:CMS的三色标记算法、分区的G1回收器、短时停顿的ZGC回收器

    一.CMS及其三色标记算法 1.核心 标记整个图谱的过程分为多步 多个线程相互工作,才能标记完 标记的算法,JVM虚拟机.go语言使用的都是三色标记算法 2.含义 从那个地方开始,用三种颜色替代 一开 ...

  4. Java中将 int[] 数组 转换为 List(ArrayList)

    前言 说起数组转换成 ArrayList,很多同学第一反应就是遍历数组,将元素逐个添加到 ArrayList 中,但是这个看着就lower,一般不会这么答. 所以马上就会想到Arrays工具类的 as ...

  5. jQuery使用 前端框架Bootstrap

    目录 jQuery查找标签 1.基本选择器 2.组合选择器 3.后代选择器 4.属性选择器 5.基本筛选器 7.筛选器方法 链式操作的本质 操作标签 1.class操作 2.位置操作 3.文本操作 4 ...

  6. 【深入浅出 Yarn 架构与实现】4-2 RM 管理 Application Master

    上一篇文章对 ResourceManager 整体架构和功能进行了讲述.本篇将对 RM 中管理 Application Master 的部分进行深入的讲解. 下面将会介绍 RM 与 AM 整体通信执行 ...

  7. [R语言] R语言PCA分析教程 Principal Component Methods in R

    R语言PCA分析教程 Principal Component Methods in R(代码下载) 主成分分析Principal Component Methods(PCA)允许我们总结和可视化包含由 ...

  8. Hive详解(02) - Hive 3.1.2安装

    Hive详解(02) - Hive 3.1.2安装 安装准备 Hive下载地址 Hive官网地址:http://hive.apache.org/ 官方文档查看地址:https://cwiki.apac ...

  9. 使用小黄鸟(HttpCanary)+模拟器(VMOS Pro)对手机APP进行抓包

    最近接触app开发,苦于app端不能像网页端可以F12看请求信息,对于后端来说当接口出现异常却不能拿到请求参数是很苦恼的, 因为之前了解过逍遥模拟器,先使用了模拟器对appj进行抓包,但发现这一款ap ...

  10. 通过Docker启动Solace,并在Spring Boot通过JMS整合Solace

    1 简介 Solace是一个强大的实时性的事件驱动消息队列.本文将介绍如何在Spring中使用,虽然代码使用的是Spring Boot,但并没有使用相关starter,跟Spring的整合一样,可通用 ...