Spring(Ioc DI、Spring的继承-依赖)
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种“
- singleton ,单例,表示通过Spring容器获取对象的唯一性 默认
(无论取不取,只要加载IoC容器,就创建对象) - prototype, 原型,表示通过Spring容器获取的对象是不同的
(如果不取Bean就不创建对象,取几个就创建几个对象) - request ,请求,表示在一次HTTP请求内有效
- 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的继承-依赖)的更多相关文章
- Spring IOC&DI 控制反转和依赖注入
控制反转(Inversion of Control,缩写为IOC),它是把你设计好的对象交给spring控制,而不再需要你去手动 new Object(); 网上对于IOC的解释很多,对程序员而言,大 ...
- Spring+IOC(DI)+AOP概念及优缺点
Spring pring是一个轻量级的DI和AOP容器框架. 说它轻量级有一大部分原因是相对与EJB的(虽然本人从没有接触过EJB的应用),重要的是,Spring是非侵入式的,基于spring开发的应 ...
- IOC/DI控制反转与依赖注入
IOC/DI控制反转与依赖注入 IOC和DI表现的效果的是一样的只不过对于作用的对象不同,有了不一样的名字. 先用一个现实的例子来说明IOC/DI表现出来的效果.
- 黑马-Spring(IOC&DI) AOP
IOC(控制翻转) 概念 把对象的创建.初始化.销毁等工作交给spring容器来做 案例 环境 步骤 1. 写一个HelloWorld类 2. 写一个配置文件 把hello类放到spring容 ...
- spring IOC DI AOP MVC 事务, mybatis 源码解读
demo https://gitee.com/easybao/aop.git spring DI运行时序 AbstractApplicationContext类的 refresh()方法 1: pre ...
- 零基础学习java------37---------mybatis的高级映射(单表查询,多表(一对一,一对多)),逆向工程,Spring(IOC,DI,创建对象,AOP)
一. mybatis的高级映射 1 单表,字段不一致 resultType输出映射: 要求查询的字段名(数据库中表格的字段)和对应的java类型的属性名一致,数据可以完成封装映射 如果字段和jav ...
- spring ioc DI 理解
下面是我从网上找来的一些大牛对spring ioc和DI的理解,希望也能让你对Spring ioc和DI的设计思想有更进一步的认识. 一.分享Iteye的开涛对Ioc的精彩讲解 Ioc—Inversi ...
- Spring Ioc DI 原理
IOC(DI):其实这个Spring架构核心的概念没有这么复杂,更不像有些书上描述的那样晦涩.Java程序员都知道:java程序中的每个业务逻辑至少需要两个或以上的对象来协作完成,通常,每个对象在使用 ...
- Spring系列(二):Spring IoC/DI的理解
这几天重新学习了一下Spring,在网上找了相关的ppt来看,当看到Spring IoC这一章节的时候,先大致浏览了一下内容,有将近50页的内容,内心窃喜~QAQ~,看完这些内容能够对IoC有更深层次 ...
- spring Ioc/DI的理解
学习spring的童鞋都知道,spring中有两个非常重要的点,Ioc(控制反转)与DI(依赖注入),对于初级玩家来说,这两个概念可能有点模棱两可的感觉,今天就谈下自己的一点理解,不足请多多指教!!! ...
随机推荐
- (C++) 类与 static_cast 与 dynamic_cast
static_cast static_cast相当于C语言里面的强制转换,适用于: 用于类层次结构中基类(父类)和派生类(子类)之间指针或引用的转换.进行上行转换(把派生类的指针或引用转换成基类表示) ...
- 基于echarts的带流动光效的折线图-lowline-for-echarts使用记录
起源 在技术群看到有人问这个react插件,带着好奇心看了一下. 标题:基于echarts的带流动光效的折线图 - 掘金 网址:https://juejin.cn/post/7090566240233 ...
- 【知识体系】Kafka文档汇总、组成及架构,配置,常见名词解释,命令行及api操作,官方文档内容,各部分深入,zookeeper和security,监控和运维
〇.相关资料 1.快速搭建文档: 2.详细讲义 3.在线官方文档:http://kafka.apache.org/documentation/ 4.Kafka知识个人总结 5.KafkaPPT汇报 链 ...
- 干电池升压5V,功耗比较低
干电池升压5V,功耗10uA PW5100干电池升压5V芯片 输出电容: 所以为了减小输出的纹波,需要比较大的输出电容值.但是输出电容过大,就会使得系统的 反应时间过慢,成本也会增加.所以建议使用一个 ...
- .NET性能优化-ArrayPool同时复用数组和对象
前两天在微信后台收到了读者的私信,问了一个这样的问题,由于私信回复有字数和篇幅限制,我在这里统一回复一下.读者的问题是这样的: 大佬您好,之前读了您的文章受益匪浅,我们有一个项目经常占用 7-8GB ...
- .NET周报【12月第1期 2022-12-08】
国内文章 CAP 7.0 版本发布通告 - 支持延迟消息,性能炸了? https://www.cnblogs.com/savorboard/p/cap-7-0.html) 今天,我们很高兴宣布 CAP ...
- from表单前后端数据编码格式-Ajax发送json格式数据-Ajax发送文件-Django自带序列化组件-Ajax结合sweetalert
目录 一:前后端传输数据的编码格式(contentType) 1.研究post请求数据的编码格式 2.可以朝后端发送post请求的方式 3.前后端传输数据的编码格式 4.研究form表单 5.当for ...
- Linux下“减速”查看日志的方法
Linux下"减速"查看日志的方法 需求场景 今天查看日志,有个需求,需要按照指定"速率"输出日志信息到终端屏幕上,方便查看. 这个需求日常应该也经常会碰到,比 ...
- Ng-Matero v15 正式发布
前言 Angular 按照既定的发版计划在 11 月中旬发布了 v15 版本.推迟了一个月(几乎每个版本都是这个节奏),ng-matero 也终于更新到了 v15.其实 ng-matero 本身的更新 ...
- [python] tensorflow中的argmax()函数argmax()函数
首先 import tensorflow as tf tf.argmax(tenso,n)函数会返回tensor中参数指定的维度中的最大值的索引或者向量.当tensor为矩阵返回向量,tensor为向 ...