第二章、Spring IOC

第二章、Spring IOC

控制反转:Inversion of Controller(IoC)

依赖注入:Dependency injection(DI)

在这里插入图片描述

一、Spring IoC的概念及作用

(一)Spring IoC的概念及作用

ioc指的是控制反转,指的就是以前我们获取一个对象时采用的是自己创建一个的方式,这是一个主动的过程。

而控制反转后,当我们需要对象时就跟工厂要,而工厂来帮我们创建或者查找对象,这是一个被动的过程。

这种被动接收对象的方式就是控制反转。

IoC的作用是削减计算机程序的耦合(解除代码中的依赖关系)

(二)程序的耦合

​ 耦合度讲的是模块模块之间,代码代码之间的关联度,模块间的耦合度是指模块之间的依赖关系,包括控制关系、调用关系、数据传递关系。模块间联系越多,其耦合性越强,同时表明其独立性越差。

​ 通过对系统的分析把他分解成一个一个子模块,子模块提供稳定的接口,达到降低系统耦合度的的目的,模块模块之间尽量使用模块接口访问,而不是随意引用其他模块的成员变量。


(三)解决程序耦合

1、通过反射机制,降低程序耦合

​ 在最初使用jdbc 时,是通过反射来注册驱动的,代码如下:

Class.*forName*("com.mysql.jdbc.Driver");

​ 此时,我们的类中不再依赖具体的驱动类,此时就算删除 mysql 的驱动 jar 包,依然可以编译。

2、工厂模式解耦合

		/**
     * 根据bean的名称获取Bean的对象
     */
    public static Object getBean(String beanName) {
    
    
        Object obj = null;
        try {
    
    
            String beanAllName = properties.getProperty(beanName);
            obj = Class.forName(beanAllName);
        } catch (ClassNotFoundException e) {
    
    
            e.printStackTrace();
        }
        return obj;
    }

3、单例模式解耦合

​ 单例可以让程序的对象在Java运行期间只有一个,节省了内存,有利于Java垃圾回收。然而单例有线程安全问题,但是我们平常一般不会讲变量定义在属性上,而是定义在方法内部,这样就有力的避免了单例模式的线程安全问题。

public class BeanFactory {
    
    

    private static Properties properties;
    private static Map<String, Object> beans;

    static {
    
    
        try {
    
    
            properties = new Properties();
            properties.load(BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties"));

            beans = new HashMap<>();

            Set<String> keys = properties.stringPropertyNames();

            for (String key : keys) {
    
    
                String beanName = properties.getProperty(key);
                beans.put(key,Class.forName(beanName).newInstance());
            }
        } catch (IOException | ClassNotFoundException e) {
    
    
            e.printStackTrace();
        } catch (IllegalAccessException e) {
    
    
            e.printStackTrace();
        } catch (InstantiationException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
        }
    }
    public static Object getBean(String beanName){
    
    
        return beans.get(beanName);
    }
}

二、Spring IoC

(一)Spring容器

​ 在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对它进行实例化. 只有在容器实例化后, 才可以从 IOC 容器里获取 Bean 实例并使用.
Spring 提供了两种类型的 IOC 容器实现
BeanFactory: IOC 容器的基本实现。
ApplicationContext: 提供了更多的高级特性. 是 BeanFactory 的子接口。
BeanFactorySpring 框架的基础设施,面向 Spring 本身,ApplicationContext 面向使用 Spring 框架的开发者,几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory


(二)ApplicationContext

ApplicationContext 的主要实现类:

1、ClassPathXmlApplicationContext:从类路径下加载配置文件

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("conf/applicationContext.xml");
HelloService helloService = (HelloService) context.getBean("helloService");

2、FileSystemXmlApplicationContext: 从文件系统中加载配置文件

FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("file:///E:/mytext/0525-spring2/src/main/resources/conf/applicationContext.xml");
HelloService helloService = (HelloService) context.getBean("helloService");

​ 第一步生成工厂对象。加载完指定路径下 bean 配置文件后,利用框架提供的 FileSystemXmlApplicationContext API 去生成工厂 bean。FileSystemXmlApplicationContext 负责生成和初始化所有的对象,比如,所有在 XML bean 配置文件中的 bean。

​ 第二步利用第一步生成的上下文中的 getBean() 方法得到所需要的 bean。 这个方法通过配置文件中的 bean ID 来返回一个真正的对象。一旦得到这个对象,就可以利用这个对象来调用任何方法。

3、XmlWebApplicationContext:从Spring在web应用中载配置文件

XmlWebApplicationContext context = new XmlWebApplicationContext();
//设置配置文件的位置
context.setConfigLocation("classpath:applicationContext.xml");
//调用refresh()方法
context.refresh();
//从容器得到bean对象
HelloService helloService = (HelloService) context.getBean("helloService");

猜你喜欢

转载自blog.csdn.net/yubo_830/article/details/106341422