spring介绍

Spring的出现是为了取代EJB(Enterprise JavaBean)的臃肿、低效、脱离现实的缺点。

Spring致力于J2EE应用的各层(表现层、业务层、持久层)的解决方案,Spring是企业应用开发的“一站式”选择。

定义:
Spring是分层的JavaSE/EE应用一站式的轻量级开源框架(官网: http://spring.io/ ),以Ioc(Inverse of control)控制反转和Aop(Aspect Oriented Programming)面向切面编程为核心。
轻量级:针对EJB来说,使用方便。
一站式:spring针对各各层(表现层、业务层、持久层)提出解决方案。
表现层:springmvc(spring自己的mvc框架),提供和其它web框架整合方案。
业务层:spring基于aop(面向切面编程)思想进行事务控制。
持久层:spring自己提供JdbcTemplate,提供和其它持久层框架整合的方案。

spring核心 :Ioc(控制反转)和aop(面向切面编程)。

重点是:IOC,spring要管理各各层的bean。

什么是IOC

不使用ioc,代码中创建一个对象直接操作接口实现类,并没有面向接口开发。

面向接口开发:调用接口的方法,只面向接口而不面向接口实现类,因为一个接口可能有多个实现类。

没有面向接口开发的问题:调用接口的类和接口实现类之间存在直接耦合。

解决:
将调用接口的类和接口实现类要解耦合。

可以通过将创建接口实现类对象的工作交给工厂来作。

对象只与工厂耦合,对象之间没有耦合。
什么是IOC:
IoC (Inverse of Control)即控制反转。是指将原来程序中自己创建实现类对象的控制权反转到IOC容器中。只需要通过IOC获了对象的实例,将IOC当成一个黑盒子、工厂。
IOC入门
spring提供ioc容器,对 bean进行实例化。使用bean时候从容器中取。
IOC控制反转,将对象的创建权反转到了spring容器中。
加入spring的jar
下载spring开发包,导入jar包到工程
官网:http://spring.io/

下载地址:http://repo.springsource.org/libs-release-local/org/springframework/spring

下载版本: Spring4.2.4
加入IOC基础的jar包:

导入IOC核心容器jar包
•spring-beans-4.2.4.RELEASE.jar
•spring-context-4.2.4.RELEASE.jar
•spring-core-4.2.4.RELEASE.jar
•spring-expression-4.2.4.RELEASE.jar

spring使用JCL日志体系(commons-logging-1.2.jar)
commons-logging:相当 于原来的slf4j,只有日志接口
还需要加入日志实现:log4j

配置applicationContext.xml
spring的ioc容器的配置文件:applicationContext.xml(默认名称)

配置schema约束:
http://www.springframework.org/schema/beans/spring-beans.xsd
当然也可以在线安装,我这里使用的是本地安装
本地安装下载:https://download.csdn.net/download/a604435713/10758980

配置bean
1、编写好接口及接口实现类
2、需要在spring的容器的配置文件中配置spring要管理的bean。

获取bean实例

@Testpublic void test1() {// 创建spring容器的实例ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");// 通过容器实例对象获取bean实例// 通过 bean的名称来获取CustomerDao customerDao = (CustomerDao) applicationContext.getBean("customerDao");System.out.println(customerDao);CustomerService customerService = (CustomerService) applicationContext.getBean("customerService");System.out.println(customerService);}

spring对bean进行实例化方法
spring要对applicationContext.xml中配置的bean进行实例化。

包括:通过无参构造器实例化、通过有参构造器实例化、通过静态工厂方法和自动拆装箱

<!-- 测试spring对bean的实例化方法 --><!-- 默认通过无参构造器 --><bean id="customer1" class="cn.itcast.crm.domain.CstCustomer"></bean><!-- 通过有参构造器构造器:public CstCustomer(Long custId,String custName)--><bean id="customer2" class="cn.itcast.crm.domain.CstCustomer"><!-- index:参数位置,第一个参数位置为0value:参数值type:参数类型--><constructor-arg index="0" value="101" type="java.lang.Long"/><constructor-arg index="1" value="牛牛" type="java.lang.String"/></bean><!-- 了解,通过静态工厂方法获取bean的实例
class:配置工厂类的路径
factory-method:调用工厂方法,获取对象-->
<bean id="customer3" class="cn.itcast.crm.domain.CustomerFactory" factory-method="getCustomer"></bean>


ApplicationContext
ApplicationContext理解为spring容器的上下文,通过上下文操作容器中bean。

ClassPathXmlApplicationContext:加载classpath下的配置文件创建一个容器实例
FileSystemXmlApplicationContext:加载文件系统中任意目录 下的配置文件,创建一个容器实例

//加载多个配置文件
//      ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"bean1.xml","bean2.xml"});//通过统配符号加载
//      ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:bean*.xml");ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"bean1.xml","classpath:bean*.xml"});

DI

DI的概念
控制反转,哪些对象被反转,获得依赖对象的过程被反转了。

action:调用service,action依赖service,在action中所依赖的service创建被反转到spring容器。
service:依赖dao,依赖的dao创建被反转到spring容器

依赖注入(Dependency Injection)”。所谓依赖注入,就是由IOC容器在运行期间,动态地将对象的依赖关系注入到对象的属性中。

service:依赖dao,如何实现依赖注入?
1、spring要管理service(前提)
2、spring要管理dao(前提)
总结前提:依赖方(service)、被依赖方(dao)都需要被spring管理
3、根据依赖关系,service依赖dao,将dao实例注入至service的属性中。
底层原理:spring根据配置文件中配置依赖关系,首先获取被依赖的对象dao实例,调用service对象中set方法将dao实例设置(注入)到service属性。

DI测试

让spring将service依赖的dao注入。

1、在spring的容器中配置dao和service

2、配置依赖关系,service依赖dao

小结Ioc和Di的区别
ioc:控制反转,将对象的创建权反转到ioc容器。

DI:依赖注入,将对象所依赖的对象注入到对象的属性中。
就是IOC的具体 实现方法。

1、IOC就是一个容器
2、IOC容器中包括spring管理的所有bean。
3、IOC容器负责对bean进行实例化
4、IOC容器对bean进行实例化时候,检查有哪些依赖的属性,将依赖的属性注入到实例化的bean的属性中。

要实现依赖注入,需要spring管理依赖方和被依赖方(spring要对依赖方和被依赖方实例化)。

依赖注入方法

通过有参构造器注入属性值

通过 set方法注入(常用!!!)

测试set方法注入支持属性类型:

<!-- 测试set方法依赖注入的属性类型 --><bean id="queryVo" class="cn.itcast.crm.pojo.QueryVo"><!-- 基本类型 --><property name="page" value="1"></property><property name="username" value="大牛"></property><!-- pojo属性 --><property name="customer" ref="customer1"></property><!-- list List<String> --><property name="listString"><list><value>小牛</value><value>小牛</value></list></property><!-- list List<CstCustomer> --><property name="customerList"><list><ref bean="customer3"/><ref bean="customer3"/></list></property><!-- map --><property name="map"><map><entry key="101" value="小牛"></entry><entry key="102" value="大牛"></entry></map></property><!-- properties --><property name="properties"><props><prop key="101" >小牛</prop><prop key="102" >大牛</prop></props></property></bean>

p命名空间和spEL表达式注入方式

总结:

spring:是一个轻量级的框架,提供一站式解决方案。
spring的核心模块:IOC和AOP。
IOC:控制反转
DI:依赖注入,IOC实现方法。
spring要管理bean,需要对bean进行实例化,根据bean所依赖的对象,将依赖对象实例化自动注入到bean的属性。

先实例化再注入。
实例化方法:三种,构造器(有参和无参)、静态工厂方法。
注入方法:有参构造器注入属性值,set方法注入(常用)、p命名空间注入、spEL表达式注入。

Spring的ioc控制反转相关推荐

  1. 详解spring的IOC控制反转和DI依赖注入

    转载 详解spring的IOC控制反转和DI依赖注入 2018-06-05 15:45:34 jiuqijack 阅读数 2945 文章标签: spring IOC控制反转 DI依赖注入 更多 分类专 ...

  2. Spring原理-IOC控制反转

    spring相关文章 Spring原理-IOC控制反转 Spring框架七大核心模块 Spring Beans原理–bean生命周期 一.Spring概述 1. 定义 Spring是一个轻量级Java ...

  3. Spring之IOC~控制反转

    1.Spring概述 1.1.什么是Spring Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One- ...

  4. Spring-初识Spring框架-IOC控制反转(DI依赖注入)

    ---恢复内容开始--- IOC :控制反转 (DI:依赖注入) 使用ioc模式开发 实体类必须有无参构造方法 1.搭建Spring环境 下载jar http://maven.springframew ...

  5. 浅谈spring之IoC控制反转

    以下学习资料来源于b站动力节点 spring: 出现是在2002左右,解决企业开发的难度.减轻对项目模块之间的管理,类和类之间的管理, 帮助开发人员创建对象,管理对象之间的关系.spring核心技术 ...

  6. 三大框架之spring框架+IoC控制反转、DI依赖注入

    三大框架:业务层框架Spring+IoC+DI 往期文章:jsp与cookie.重定向与RESTFul架构支持 下一章节: 持久层框架MyBatis 初识Spring框架 MyBatis 入门http ...

  7. Spring的IOC(控制反转)与DI(依赖注入)

    Spring控制反转与依赖注入 文章目录 Spring控制反转与依赖注入 1. 控制反转(IOC) 2. 依赖注入(DI) 3. 总结 1. 控制反转(IOC) 控制反转(Inversion of C ...

  8. (八)Spring之IOC控制反转、DI依赖注入介绍和使用(详解)

    文章目录 前言 Spring Spring IOC 简介 Bean IOC 概述 IOC 本质理解 Spring IOC 应用 IOC xml装配 IOC 依赖注入 IOC Bean的作用域 IoC ...

  9. spring:IOC控制反转中“dI“

    声明student对象 注入:就是赋值的意思 di:给属性赋值 1.set注入 设置注入:spring调用类的set方法 可以再set方法中完成属性赋值 1)简单类型的set注入 先来说说基本的格式 ...

最新文章

  1. 深入理解Java中为什么内部类可以访问外部类的成员
  2. APP测试和传统软件测试有什么区别
  3. SQL Server基础知识概念要点详细讲解
  4. git stash pop冲突_这有一份 git 日常使用清单,你需要吗?
  5. OA办公系统的发展离不开企业大胆尝试使用
  6. Linux 5.4 LVM RAW 设备 配置的深入研究
  7. mplayer-php,mplayer+smplayer 前后端播放器安装
  8. Digital Text Animations for Mac - 未来感活力全屏标题动画fcpx插件
  9. 【glibc源码分析】--strcpy.c 字符串复制
  10. windows中端口号被占用的解决方法
  11. Module build failed: ReferenceError: Unknown plugin module-resolver specified
  12. collections 使用教程
  13. php与mysqli,如何通过PHP与MySQLi保持联系
  14. 数据分析结果解读_物流数据分析的行业特点有哪些?
  15. Hyper-V网络虚拟化--VM之间拷贝速度慢
  16. 51单片机蓝牙模块的使用方法
  17. 通过maven命令获取参数值
  18. 腾讯战华为:一场「渠道」之争背后,游戏行业变天了
  19. (原创)二十一天定律
  20. Tomato绕激活工具,支持两网信号/修复通知/游戏机

热门文章

  1. 【图像增强】基于matlab Frangi滤波器血管图像增强【含Matlab源码 2108期】
  2. pull request 时遇到 conflicted 的解决方法
  3. 1.3 基于协同过滤的电影推荐案例
  4. R语言案例分析:多元数据的基本统计分析
  5. 计算机看游戏显卡,如何看电脑显卡性能
  6. 【技巧分享】股票代码规则
  7. 2020阿里招聘岗位要求
  8. 文本识别综述 <软件学报_王建新等、中国图象图形学报_刘崇宇等>
  9. WebRTC语音对讲无声音
  10. 趣谈win10常用快捷键