Spring IOC

一、什么是控制反转

  所谓的IOC(Inversion of Control),控制反转,就是指将对象的创建,对象的存储(map),对象的管理(依赖查找,依赖注入)交给了spring容器。
  在此之前,当需要对象时,通常是利用new关键字创建一个对象:

//1.创建一个Hello对象
Hello hello = new Hello();
//2.调用hello对象的方法
hello.sayHi();

但由于new对象,会提高代码之间耦合性.

  所谓的耦合指的是在软件开发中,在层与层之间产生了某种紧密的关系。这种关系可能会导致,在我们修改或者是替换某一层时会影响其他的层,像这种情况我们就称之为层与层之间产生了耦合。
  由于耦合(层与层之间的紧密关系)可能会导致我们在修改某一层时影响到其他的层,而这严重违反了我们对软件进行分层的最初设想 – 软件各层之间应该相互独立、互不干扰,在修改或者是替换某一层时,应该做到不影响其他层。

而使用spring框架,对象的创建可以交给spring来做:

//1.从spring容器中获取bean对象(而不是自己new)
Hello hello = (Hello) ac.getBean("hello");
//2.调用hello对象的方法
hello.sayHi();

  只需要将类提前配置在spring配置文件中,就可以将对象的创建交给spring容器,当需要对象时,不需要自己创建,而是直接通过spring获取即可,省去了new对象,可以降低代码之间的耦合性。

二、IOC案例

  1. 创建Maven工程,引入spring相关依赖包
    1)创建Maven—Java工程
    在这里插入图片描述
    2)引入spring的jar包:在maven工程的pom.xml文件中添加如下配置
<!-- 集中定义依赖版本号 -->
<properties>
	<junit.version>4.10</junit.version>
	<spring.version>4.1.3.RELEASE</spring.version>
</properties>

<dependencies>
	<!-- 单元测试 -->
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>${junit.version}</version>
		<scope>test</scope>
	</dependency>

	<!-- Spring -->
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-webmvc</artifactId>
		<version>${spring.version}</version>
	</dependency>
</dependencies>
  1. 创建spring核心配置文件—applicationContext.xml
    1)在工程的java/resources目录下,创建applicationContext.xml文件:
    在这里插入图片描述
    2)在applicationContext.xml中添加如下内容(下面的配置不需要掌握,只是一个文件格式):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
</beans>
  1. 创建实体类—Hello,并将Hello对象的创建交给Spring管理
    1)创建Hello实体类
    在这里插入图片描述
    2)在Hello类中添加方法如下:
package com.company.spring;

public class Hello {
	public void sayHi(){
		System.out.println("Hello.sayHi()...");
	}
}

3)将Hello对象的创建交给Spring容器来管理,在核心配置文件中添加如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	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-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- 声明bean对象,id是唯一标志,class:bean对象的全路径 -->
	<bean id="hello" class="com.company.spring.Hello"></bean>
</beans>
  1. 创建测试类—TestIOC,创建Hello对象,并调用其中的方法
    1)创建TestIOC测试类
    在这里插入图片描述
    2)测试结果:
    在这里插入图片描述

三、IOC小结

  这就是spring框架的IOC——控制反转。之前我们自己new对象,例如:

User u = new User();

  而现在,变成由一个初始化的xml配置文件来创建,也就是由spring容器来创建。

	User u = (User) ac.getBean("user ");

  当程序运行,spring开始工作后,会加载整个xml核心配置文件,读取到,获取到class属性中类的全路径,利用反射创建该类的对象。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值