Spring的ioc控制反转

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实例

@Test
	public 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:参数位置,第一个参数位置为0
		value:参数值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表达式注入。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值