【Spring】初次使用IoC,ApplicationContext和BeanFactory的区别

1. 准备spring的jar包以及需要解决的问题程序

  • spring官网: http://spring.io/

  • 下载地址:
    http://repo.springsource.org/libs-release-local/org/springframework/spring

  • 解压后spring的目录

    • docs :API 和开发规范
    • libs :jar 包和源码.
    • schema :约束.
  • 将jar包拷贝至工程中的lib目录。
    当然我用的是Maven工程,所以只需要编辑pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.siyi</groupId>
    <artifactId>spring_02factory</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
    </dependencies>
</project>

在这里插入图片描述

  • 业务层接口和实现类
/**
 * 账户业务层的接口
 */
public interface IAccountService {

    /**
     * 模拟保存账户
     */
    public void saveAccount();
}
/**
 * 账户的业务层实现类
 */
public class AccountServiceImpl implements IAccountService {

    private IAccountDao accountDao = new AccountDaoImpl();//此处的依赖关系由后面依赖注入解决

    public void saveAccount() {
        accountDao.saveAccount();
    }
}
  • 持久层接口和实现类
/**
 * 账户的持久层接口
 */
public interface IAccountDao {
    /**
     * 模拟保存账户
     */
    public void saveAccount();
}
/**
 * 账户的持久层实现类
 */
public class AccountDaoImpl implements IAccountDao {

    public void saveAccount() {
        System.out.println("保存了账户");
    }
}

2. 配置XML文件

创建一个xml文件bean.xml,并编辑bean.xml
给配置文件导入约束:
/spring-framework-5.0.2.RELEASE/docs/spring-framework-reference/html5/core.html

<?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">
        
</beans>
  • 让 spring 管理资源,在配置文件中配置 service 和 dao
<?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="accountService" class="com.siyi.service.impl.AccountServiceImpl"></bean>

    <bean id="accountDao" class="com.siyi.dao.impl.AccountDaoImpl"></bean>
</beans>
  • bean 标签:用于配置让 spring 创建对象,并且存入 ioc 容器之中
    • id 属性:对象的唯一标识。
    • class 属性:指定要创建对象的全限定类名

3. 实现使用spring的IoC解决耦合问题

现在我们测试一下:

package com.siyi.ui;

import com.siyi.dao.IAccountDao;
import com.siyi.service.IAccountService;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * 模拟一个表现层,用于调用业务层
 */
public class Client {
    /**
     * 获取spring的IoC核心容器,并根据id获取对象
     * @param args
     */
    public static void main(String[] args) {
        //1. 获取核心容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//        ApplicationContext ac = new FileSystemXmlApplicationContext("E:\\practice\\spring_Demo\\src\\main\\resources\\bean.xml");
//        //2. 根据id获取Bean对象
        IAccountService accountService = (IAccountService)ac.getBean("accountService");
        IAccountDao accountDao = ac.getBean("accountDao",IAccountDao.class);

//        Resource resource = new ClassPathResource("bean.xml");
//        BeanFactory bf = new XmlBeanFactory(resource);
//        IAccountService accountService = (IAccountService)bf.getBean("accountService");
//        IAccountDao accountDao = bf.getBean("accountDao",IAccountDao.class);
        accountService.saveAccount();
        System.out.println(accountService);
        System.out.println(accountDao);
    }
}

运行结果如下:

4. ApplicationContext的常用实现类

  • ClassPathXmlApplicationContext: 它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。
  • FileSystemXmlApplicationContext: 它可以加载磁盘任意路径下的配置文件(必须有访问权限)
  • AnnotationConfigApplicationContext: 它是用于读取注解创建容器.

5. ApplicationContext和BeanFactory的区别

  • ApplicationContext: 单例对象适用
    它在构建核心容器时,创建对象采用的策略是采用立即加载的方式。也就是说,只要一读完配置文件马上就创建配置文件中配置的对象。
  • BeanFactory: 多例对象适用
    它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才是真正的创建对象。

  • BeanFactory 才是 Spring 容器中的顶层接口。
    ApplicationContext 是它的子接口。
  • BeanFactory 和 ApplicationContext 的区别:
    创建对象的时间点不一样。
    • ApplicationContext:只要一读取配置文件,默认情况下就会创建对象。
    • BeanFactory:什么使用什么时候创建对象。
发布了476 篇原创文章 · 获赞 152 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_41879343/article/details/104968987