文章目录
一、IoC基本介绍
IoC即 Inversion of Control(控制反转)的缩写。所谓的控制反转,我个人理解为是一种控制权移交的行为。原来的控制权掌握在我们自己手中,而现在的控制权交给别人帮我们统一进行管理。未使用Spring的IoC容器之前,我们创建一个对象可能使用
new 类名()
的方式,而使用Spring的IoC容器之后,我们把对象的创建交给Spring管理。Spring的IoC容器只是在一定程度上削减了程序之间的耦合性(依赖性),但是不能完全消除程序间的耦合性。
二、环境搭建
1、导入依赖
在pom.xml
中添加spring-context
依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
添加依赖后导入的jar包如下图所示:
2、模拟dao层和service层
public interface IAccountDao {
/**
* 模拟保存账户信息
*/
void saveAccount();
}
public class AccountDaoImpl implements IAccountDao {
public void saveAccount() {
System.out.println("保存了账户信息!");
}
}
public interface IAccountService {
void saveAccount();
}
public class AccountServiceImpl implements IAccountService {
IAccountDao accountDao = new AccountDaoImpl();
public AccountServiceImpl() {
System.out.println("对象创建了");
}
public void saveAccount() {
accountDao.saveAccount();
}
}
3、编写配置文件
<?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">
<!--把对象的创建交给Spring管理-->
<bean id="accountDao" class="com.uos.dao.impl.AccountDaoImpl"></bean>
<bean id="accountService" class="com.uos.service.impl.AccountServiceImpl"></bean>
</beans>
id
属性可以唯一标识一个bean对象,class
属性指定需要实例化类的全限定类名。
4、编写Client.java(模拟controller层)
public class Client {
public static void main(String[] args) {
// 获取核心容器对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
// 根据id获取bean对象
IAccountDao accountDao = (IAccountDao) applicationContext.getBean("accountDao");
IAccountService accountService = applicationContext.getBean("accountService", IAccountService.class);
System.out.println(accountDao);
System.out.println(accountService);
}
}
三、ApplicationContext的三个实现类
1、ClassPathXmlApplicationContext类
// 获取核心容器对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
// 根据id获取bean对象
IAccountDao accountDao = (IAccountDao) applicationContext.getBean("accountDao");
IAccountService accountService = applicationContext.getBean("accountService", IAccountService.class);
2、FileSystemXmlApplicationContext类
// 获取核心容器对象
ApplicationContext applicationContext = new FileSystemXmlApplicationContext("C:\\Users\\86183\\Desktop\\beans.xml");
// 根据id获取bean对象
IAccountDao accountDao = (IAccountDao) applicationContext.getBean("accountDao");
IAccountService accountService = applicationContext.getBean("accountService", IAccountService.class);
System.out.println(accountDao);
System.out.println(accountService);
3、AnnotationConfigApplicationContext类
四、ApplicatioContext和BeanFactory的区别
1、使用ApplicatioContext方式
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
IAccountDao accountDao = (IAccountDao) applicationContext.getBean("accountDao");
IAccountService accountService = applicationContext.getBean("accountService", IAccountService.class);
2、使用BeanFactory方式
Resource resource = new ClassPathResource("beans.xml");
BeanFactory beanFactory = new XmlBeanFactory(resource);
IAccountService accountService = (IAccountService) beanFactory.getBean("accountService");
ApplicationContext: 单例对象适用 它在构建核心容器时,创建对象采取的策略是采用立即加载 的方式。也就是说,只要一读取完配置文件马上就创建配置文件中配置的对象。
BeanFactory:多例对象适用
它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。