Spring基于XML的IoC环境搭建

一、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获取对象了,什么时候才真正的创建对象。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值