1. 概述
Spring IOC(Inversion of Control,控制反转)是一种设计思想,通过依赖注入(Dependency Injection,DI)实现。它的核心思想是将对象的创建和依赖关系的管理交给Spring容器,从而降低代码之间的耦合度,提高代码的可维护性和可测试性。
2. 代码示例分析
2.1 传统方式
在没有使用Spring之前,对象的创建和依赖关系的管理是由程序自身控制的。例如:
public class UserServiceImpl implements UserService {private UserDao userDao = new UserDaoOracleImpl();public void getUser() {userDao.getUser();}
}
这种方式的缺点是代码耦合度高,修改和扩展困难。
2.2 使用Set方法注入
通过Set方法实现依赖注入,将对象的创建和依赖关系的管理交给Spring容器:
public class UserServiceImpl implements UserService {private UserDao userDao;public void setUserDao(UserDao userDao) {this.userDao = userDao;}public void getUser() {userDao.getUser();}
}
这种方式使得程序从主动创建对象转变为被动接受对象,降低了耦合度。
2.3 使用构造函数注入
通过构造函数注入,进一步简化代码:
public class UserServiceImpl implements UserService {private UserDao userDao;public UserServiceImpl(UserDao userDao) {this.userDao = userDao;}public void getUser() {userDao.getUser();}
}
这种方式更加简洁,但需要在Spring配置文件中进行相应的配置。
3. Spring配置文件
3.1.Spring IoC容器的工作原理:Spring IoC容器的主要作用是创建、管理和装配Bean。
3.1.1 读取配置文件
Spring IoC容器首先读取配置文件(如applicationContext.xml
),文件中定义了Bean的创建和依赖关系。
3.1.2 根据元数据创建和组装Bean
根据配置文件中的元数据,Spring IoC容器创建相应的Bean,并注入所需的依赖。
3.1.3 从容器中获取Bean
最后,应用程序可以从Spring IoC容器中获取已经创建和配置好的Bean,Spring通过读取配置文件(如applicationContext.xml
或beans.xml
)来创建和管理Bean。
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="mysqlImpl" class="com.kuang.dao.UserDaoMysqlImpl"/><bean id="oracleImpl" class="com.kuang.dao.UserDaoOracleImpl"/><bean id="userServiceImpl" class="com.kuang.service.UserServiceImpl"><!-- ref:引用Spring容器中创建好的对象 --><!-- value:具体的值,基本数据类型 --><property name="userDao" ref="mysqlImpl"/></bean>
</beans>
4. 控制反转(IoC)的本质
控制反转是一种通过描述(XML或注解)并通过第三方去生产或获取特定对象的方式。在Spring中,IoC容器负责对象的创建、管理和装配。
4.1 传统方式与IoC方式的对比
传统方式:程序主动创建对象,控制权在程序猿手上。
IoC方式:程序被动接收对象,控制权在Spring容器手中。
4.2 依赖注入(DI)
依赖注入是实现IoC的一种方法,通过构造函数、Set方法或注解等方式将依赖注入到对象中。
4.3 demo例子
pojo
public class Hello {private String str;public String getStr() {return str;}public void setStr(String str) {this.str = str;}@Overridepublic String toString() {return "Hello{" +"str='" + str + '\'' +'}';}
}
beans.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"><bean id="hello" class="com.WPS.pojo.Hello"><property name="str" value="Wupeisheng" /></bean></beans>
测试
/*** 测试类,用于演示如何使用Spring IoC容器来创建和管理Bean。*/
public class MyTest {/*** 主方法,程序的入口点。** @param args 命令行参数*/public static void main(String[] args) {// 创建一个ClassPathXmlApplicationContext对象,用于从类路径下的beans.xml文件中加载Bean定义ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");// 从Spring IoC容器中获取id为"hello"的Bean,并将其转换为Hello类型Hello hello = (Hello) context.getBean("hello");// 打印Hello对象的字符串表示形式,通常重写了toString方法以显示有意义的信息System.out.println(hello.toString());}}
5. 总结
通过使用Spring的IoC容器,我们可以将对象的创建和管理交给Spring,从而降低代码之间的耦合度,提高代码的可维护性和可测试性。具体实现方式包括:
使用Set方法注入依赖
使用构造函数注入依赖
通过Spring配置文件管理Bean的创建和依赖注入
这种方式使得程序从主动创建对象转变为被动接受对象,实现了控制反转的设计思想。