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. sql注入的一丢丢

  2. DHorse的链路追踪

    目前,DHorse的链路追踪功能是通过SkyWalking来实现.实现原理是DHorse在部署应用时,通过指定SkyWalking的Agent来收集服务的调用链路信息.下面就来具体看一下DHorse如 ...

  3. 【每日一题】【第n个 n-->0】19./NC53 【删除】链表的倒数第 N 个结点-211123/220127

    给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点. 答案: import java.util.*; /* * public class ListNode { * int val; * ...

  4. 【每日一题】【双端降序队列Deque】2021年12月28日-239. 滑动窗口最大值

    给你一个整数数组 nums,有一个大小为 k 的滑动窗口从数组的最左侧移动到数组的最右侧.你只可以看到在滑动窗口内的 k 个数字.滑动窗口每次只向右移动一位. 返回滑动窗口中的最大值. 来源:力扣(L ...

  5. 【Java】【数据库】索引为何使查询变得更快?--B+树

    排序数据的二分查找 二分查找的时间复杂度是\(O(log_2n)\),明显快于暴力搜索. 索引 建立索引的数据,就是通过事先排好顺序,在查找时可以应用二分查找来提高查询效率. 所以索引应该尽可能建立在 ...

  6. [数据结构][洛谷]P3375模板题 KMP

    主要还是KMP算法,上学期没学,只是考前抱了抱佛脚,也没怎么弄明白. 先放代码: //KMP #include <bits/stdc++.h>//万能头 using namespace s ...

  7. vuex的使用详解

    一.下载vuex 在store文件夹下的index.js中    官方文档:https://vuex.vuejs.org/zh/ 需要使用的页面 sotre中 mutations的调用方法 store ...

  8. cmd命令行ssh连接Linux服务器

    打开cmd工具 使用命令ssh连接服务器 ssh 用户名@ip地址 (不需要指定端口号,默认端口就是22) 输入密码即可

  9. Introduction & Directory

    一个日常划水的高中生而已啦,我不会承认这个博客的CSS是copy的 一个貌似并不准确的图-- <算法竞赛进阶指南>学习: 动态规划: DP经典例题--LIS&LCS 洛谷题解: 搜 ...

  10. js任务队列EventLoop

    JS 执行机制 在我们学js 的时候都知道js 是单线程的如果是多线程的话会引发一个问题在同一时间同时操作DOM 一个增加一个删除JS就不知道到底要干嘛了,所以这个语言是单线程的但是随着HTML5到来 ...