【Spring】(Spring是什么 容器 IoC 传统程序开发 控制反转式程序 Spring IoC DI Spring创建和使用 创建Spring项目 存储Bean对象 获取并使用Bean)


Spring是什么

Spring Framework简称Spring,它是一个开源框架而且支持广泛的应用场景,它可以让Java企业级的应用程序开发起来更简单.总而言之:Spring是包含了众多工具方法的IoC容器.

容器

容乃物品的装置.

  • List/Map 数据存储容器
  • Tomcat Web容器

IoC

Spring是一个IoC容器.IoC 是"控制反转"的意思,也就是说Spring是一个"控制反转"的容器.

传统程序开发

比如说开发一辆车:

public class App {
    public static void main(String[] args) {
        Car car = new Car(34);
        car.init();
    }
}

//车
public class Car {
    private Framework framework;
    public Car(int size){
        framework = new Framework(size);
    }
    public void init(){
        System.out.println("执行了 Car 的init方法");
        // 依赖车身
        framework.init();
    }
}

//车身
public class Framework {
    private Bottom bottom;
    public Framework(int size){
        bottom = new Bottom(size);
    }
    public void init(){
        System.out.println("执行了 Framework 的init方法");
        // 依赖底盘
        bottom.init();
    }
}

//底盘
public class Bottom {
    private Tire tire;
    public Bottom(int size){
        tire = new Tire(size);
    }
    public void init(){
        System.out.println("执行了 Bottom 的init方法");
        // 依赖轮胎
        tire.init();
    }
}

//车轮
public class Tire {
    private int size = 20;

    public Tire(int size){
        this.size = size;
    }
    public void init(){
        System.out.println("执行了轮胎初始化方法,size: " + this.size);
    }
}

在这里插入图片描述

上面的代码存在耦合问题,当一个类发生改变了,另一个类就不得不去改变.如下:

控制反转式程序

举个例子:现在A类引用B类,B类引用C类,C类引用D类,之前的做法是在A类引用B类的时候直接去new,直接去new的话就存在一个问题,当它的构造方法发生变化的时候.整个调用链都要进行变化,当我们不再去new,而是把当前的对象传入进来,此时虽然说整个调用链依然是A类引用B类,B类引用C类,C类引用D类,但是当D发生改变的时候,整个调用链,A,B,C是不需要进行任何代码的修改,从而解决了代码的耦合性.

public class App {
    public static void main(String[] args) {
        Tire tire = new Tire(30);
        Bottom bottom = new Bottom(tire);
        Framework framework = new Framework(bottom);
        Car car = new Car(framework);
        car.init();
    }
}

//车
public class Car {
    private Framework framework;
    public Car(Framework framework){
        this.framework = framework;
    }
    public void init(){
        System.out.println("执行 Car");
        //依赖车身
        framework.init();
    }
}

//车身
public class Framework {
    private Bottom bottom;
    public Framework(Bottom bottom){
        this.bottom = bottom;
    }
    public void init() {
        System.out.println("执行 Framework");
        //依赖底盘
        bottom.init();
    }
}

//底盘
public class Bottom {
    private Tire tire;

    public Bottom(Tire tire){
        this.tire = tire;
    }

    public void init(){
        System.out.println("执行 Bottom");
        // 依赖轮胎
        tire.init();
    }
}

//轮胎
public class Tire{
    private int size = 20;

    public Tire(int size) {
        this.size = size;
    }

    public void init(){
        System.out.println("轮胎 size: " + this.size);
    }
}

在这里插入图片描述

Spring IoC

Spring就是是一个IoC容器,它是用来帮我们管理对象的生命周期的,它具备下面两个功能:

  • 将对象存入到容器
  • 从容器中取出对象

Spring帮我们做了生命周期的托管,也就是对象的创建和销毁的权利都交给Spring来管理了,我们在需要用的地方把它引入进去就行了,至于是如何创建,怎么传参我们不需要关注,Spring会帮我们处理.我们学习Spring就是学习如何将对象存入到Spring中,再从Spring中获取对象的过程.

DI

DI 是 Dependency Injection 的缩写,翻译成中⽂是“依赖注⼊”的意思。

  • DI(依赖注入):在程序运行期间,动态的将某个对象引入到当前的(行为)机制.
  • 从广义来说IoC = DI 从不同维度来描述同一个问题.从狭义来说IoC是一种设计思想,DI是具体的实现技术.

Spring创建和使用

创建Spring项目

创建Spring项目和Servlet类似,分为下面三个步骤:

  • 创建一个普通Maven项目
  • 添加Spring框架支持(spring-context,spring-beans)
  • 添加启动类

创建一个Maven项目

在这里插入图片描述

添加Spring框架

在项⽬的 pom.xml 中添加 Spring 框架的⽀持,xml 配置如下:

<dependencies>
 	<dependency>
 		<groupId>org.springframework</groupId>
 		<artifactId>spring-context</artifactId>
 		<version>5.2.3.RELEASE</version>
 	</dependency>
 
 	<dependency>
 		<groupId>org.springframework</groupId>
 		<artifactId>spring-beans</artifactId>
 		<version>5.2.3.RELEASE</version>
 	</dependency>
</dependencies>

在这里插入图片描述

添加启动类

在这里插入图片描述

存储Bean对象

先创建一个Bean然后将创建的Bean注册到Spring容器中.

创建Bean

创建一个普通的类,在创建一个普通的对象即可.

在这里插入图片描述

注册到容器

在resources下创建一个sping配置文件.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:content="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
</beans>

在这里插入图片描述
创建好后将Bean对象配置到 spring配置文件中.具体是将User对象注册到Spring中,如下操作:

<beans 
       <bean id="user" class="User"></bean>
</beans>

获取并使用Bean

- 得到Spring上下文对象使用:ApplicationContext

ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

xml名字要对应
在这里插入图片描述

- 从Spring中取出Bean对象

  1. 根据名称获取Bean
User user = (User) context.getBean("user");

在这里插入图片描述

  1. 根据bean类型获取 Bean
User user = context.getBean(User.class);

当Spring中存在相同的对象时,使用类型来获取Bean的方式就会报错.
在这里插入图片描述

  1. 根据 Bean 名称 + 类型 获取Bean对象
User user = context.getBean("user",User.class);

- 使用Bean(可选)

System.out.println(user.sayHi(););

验证代码:
在这里插入图片描述

ApplicationContext 和BeanFactory关系:

相同点:

  1. 都可以得到Spring上下文对象;
  2. 都是来自Spring的顶级接口

不同点:

  1. 继承关系和功能: ApplicationContext 属于BeanFactory 的子类; BeanFactory 只有最基础访问Bean的能力,而ApplicationContext 除了拥有BeanFactory 功能之外,还包含了更多的功能,如:国际化支持,资源访问,事件传播.
  2. 从性能方面来说:ApplicationContext 是一次性加载并初始化所有的Bean对象,而BeanFactory 是需要哪个才去加载哪个,执行Bean获取时,比较慢.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

马尔科686

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值