Spring简介与IOC容器

一、Spring简介

开源:源码公开、免费试用;

简化:简化企业级开发。

解耦:耦合度降低、可插拔,便于后续维护更新升级拓展。

二、Sping核心模块

Spring 框架的这些模块可以满足一切企业级应用开发的需求,在开发过程中可以根据需求有选择性地使用所需要的模块。

官网Spring | Home

Core 核心模块:提供了 Spring 框架的基本组成部分,包括 IoC 和 DI 功能。

Context 上下文模块:建立在核心和 Beans 模块的基础之上,它是访问定义和配置任何对象的媒介。ApplicationContext 接口是上下文模块的焦点。

三、Spring IOC

1、基础概念

容器:Spring 是一个容器,因为它包含并且管理应用对象的生命周期

控制反转:IOC (Inversion of Control),指的是将对象的创建权交给 Spring 去创建。

使用 Spring 之前,对象的创建都是由我们自己在代码中 new 创建。而使用 Spring之后。对象的创建都是由给了 Spring 框架。

IOC (Inversion of Control) 是指在程序开发中,对象实例的创建不再由调用者管理,而是由 Spring 容器创建。Spring 容器会负责控制程序之间的关系,而不是由程序代码直接控制,因此,控制权由程序代码转移到了 Spring 容器中,控制权发生了反转,这就是 Spring 的 IOC 思想

IOC容器的概念:IOC 容器就是具有依赖注入功能的容器,IOC 容器负责实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。应用程序无需直接在代码中 new 相关的对象,应用程序由 IOC 容器进行组装。在 Spring 中 BeanFactory 是 IOC 容器的实际代表者。

Bean的概念在 Spring 中,被 Spring 容器所管理的对象称之为”Bean”对象。一个 Spring 的 Bean 对象可以是任何形式的 POJO。

依赖注入:DI (Dependency Injection),是指依赖的对象不需要手动调用 setXX 方法去设置,而是通过配置赋值。

2、核心API

SpringIOC容器类型:Spring 提供了两种 IoC 容器,分别为 BeanFactoryApplicationContext

BeanFactory 是基础类型的 IoC 容器。它由org.springframework.beans.facytory.BeanFactory 接口定义,并提供了完整的 IoC服务支持。简单来说, BeanFactory 就是一个管理 Bean 的工厂,它主要负责初始化各种Bean,并调用它们的生命周期方法。

ApplicationContext 是 BeanFactory 的子接口,也被称为应用上下文。

ClassPathXmlApplicationContext:该 类 从 类 路 径 ClassPath 中 寻 找 指 定 的 XML 配 置 文 件 , 找 到 并 装 载 完 成ApplicationContext 的实例化工作。

<span style="background-color:#f8f8f8"><span style="color:#333333"><span style="color:#000000">ApplicationContext</span> <span style="color:#000000">applicationContext</span> <span style="color:#981a1a">=</span> <span style="color:#770088">new</span>   <span style="color:#000000">ClassPathXmlApplicationContext</span>(<span style="color:#008855">String</span> <span style="color:#000000">configLocation</span>);</span></span>

四、Spring IOC容器的使用

使用Spring之前的痛点:对象自己创建,每次修改源码都需要重新编译,耦合度过高,不便于后续更新升级。

下面以找对象为例。

public interface GirlFriend {
    void say();
}

实现类:


public class GirlfriendImplWJF implements GirlFriend{
    @Override
    public void say() {
        System.out.println("小峰峰");
    }
}

实现类2:

public class GirlfriendImplYh implements GirlFriend{
    @Override
    public void say() {
        System.out.println("小涵涵");
    }
}

 

2、通过 IOC 容器管理 Bean 对象

2.1 配置主配置文件application_context.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">
    <!--scope属性值:singleton表示单例模式(默认值),prototype 表示原型-->
    <bean id="girlFriendWJF" class="com.dyh.pojo.impl.GirlfriendImplWJF"
          scope="singleton">

    </bean>
    <bean id="girlFriendYh" class="com.dyh.pojo.impl.GirlfriendImplYh"
          scope="prototype">

    </bean>
</beans>

2.2 测试TestGirlfriendSpringIOC.java

package com.dyh.test;

import com.dyh.pojo.iter.GirlFriend;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestGirlfriendSpringIOC {
    public static void main(String[] args) {
        // ac 是一个Spring IoC 容器,需要的时候去容器中获取,不需要new
        ApplicationContext ac = new ClassPathXmlApplicationContext("application_context.xml");
        //GirlFriend girlFriend = (GirlFriend) ac.getBean("girlFriendWJF");
        /*GirlFriend girlFriend = (GirlFriend) ac.getBean("girlFriendYh");
        girlFriend.say();
        System.out.println(girlFriend);
        girlFriend = (GirlFriend) ac.getBean("girlFriendYh");
        System.out.println(girlFriend);*/
        GirlFriend girlFriend = (GirlFriend) ac.getBean("girlFriendWJF");
        girlFriend.say();
        System.out.println(girlFriend);
        girlFriend = (GirlFriend) ac.getBean("girlFriendWJF");
        System.out.println(girlFriend);
    }
}

五、Spring IOC 容器创建 Bean 对象的三种方式

1、通过构造方法创建Bean

上面案例就是,也就是如下配置,会调用GirlfriendImplYh类默认的无参构造方法实实例化对象得到id为girlFriendYh的一个Bean。

<bean id="girlFriendYh" class="com.dyh.pojo.impl.GirlfriendImplYh"
          scope="prototype">

    </bean>

2、通过静态工厂

2.1 静态工厂类

public class GirlFriendFactory {
    public static Girlfriend getGirlFriendFJC(){
        return new GirlfriendFJC();
    }
    public static Girlfriend getGirlFriendCWS(){
        return new GirlfriendCWS();
    }
}

2.2 application_context.xml配置

静态工厂内部的方法都为静态方法,静态方法可以通过对象名直接方法,故不需要配置工厂相关的bean,只需要配置class和factory-method,也就是告诉Spring容器创建对象的时候使用那个工厂的哪个静态方法,如下所示:

   <!--表示使用静态工厂GirlFriendFactory的getGirlFriendFJC方法创建id为gf_Factory_fjc的对象-->
    <bean id="gf_Factory_fjc" class="com.dyh.factory.GirlFriendFactory"
          factory-method="getGirlFriendFJC">

    </bean>
    <bean id="gf_Factory_cws" class="com.dyh.factory.GirlFriendFactory"
          factory-method="getGirlFriendCWS">

    </bean>

2.3 测试

public class TestGirlfriendSpringIoC {
    public static void main(String[] args) {
        ApplicationContext ac =
                new ClassPathXmlApplicationContext("application_context.xml");
        // 从Spring IoC容器中获取对象
        Girlfriend girlFriend  = (Girlfriend) ac.getBean("gf_Factory_cws");
        girlFriend.say();
        System.out.println(girlFriend);
    }
}

3、通过动态工厂

3.1 动态工厂

public class DynamicGirlFriendFactory {
    public Girlfriend getGirlFriendFJC(){
        return new GirlfriendFJC();
    }
    public Girlfriend getGirlFriendCWS(){
        return new GirlfriendCWS();
    }
}

3.2 application_context.xml配置

注意动态工厂内部是成员方法,调用成员方法之前,需要先实例化工厂,所以,需要配置工厂的bean。然后通过调用此工厂的方法,实例化得到对应的bean。

  <!--配置动态工厂 -->
    <bean id="dynamicGirlFriendFactory" class="com.dyh.factory.DynamicGirlFriendFactory"></bean>
    <!--通过此动态工厂得到GirlFrend对象-->
    <bean id="gf_dynFactory_cws" factory-bean="dynamicGirlFriendFactory" factory-method="getGirlFriendCWS">
        
    </bean>
    <bean id="gf_dynFactory_fjc" factory-bean="dynamicGirlFriendFactory" factory-method="getGirlFriendFJC">
        
    </bean>

3.3 测试TestGirlfriendDynamicFactory

public class TestGirlfriendDynamicFactory {
    public static void main(String[] args) {
        ApplicationContext ac =
                new ClassPathXmlApplicationContext("application_context.xml");
        // 从Spring IoC容器中获取对象
        Girlfriend girlFriend = (Girlfriend) ac.getBean("gf_dynFactory_fjc");
        girlFriend.say();
        System.out.println(girlFriend);
    }
}

六、在 Spring IOC 容器中获取 Bean 对象的方式

1、通过 id 或 name 获取 Bean 对象

Girlfriend girlFriend = (Girlfriend) ac.getBean("gf_dynFactory_fjc");

2、通过类型获取 Bean 对象

Girlfriend girlFriend = (Girlfriend) ac.getBean(Class clazz);

获取 Bean 对象

public class GirlfriendOYY implements Girlfriend {
    @Override
    public void say() {
        System.out.println("OMG");
    }
}

配置bean

<bean id="gfOYY" class="com.dyh.pojo.impl.GirlfriendOYY"></bean>

测试

public class TestGirlfriendGetByClass {
    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("application_context.xml");
        //GirlfriendFJC bean = ac.getBean(GirlfriendFJC.class);
        GirlfriendOYY bean = ac.getBean(GirlfriendOYY.class);
        bean.say();
    }
}

运行结果

 原因:

 

总结

1、理解Spring框架的作用,理解IOC(面试题)。

2、代码实现:

ApplicationContext ac = new ClassPathXmlApplicationContext("application_context.xml");

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值