前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring 全家桶之 Spring Framework 5.3(一)

Spring 全家桶之 Spring Framework 5.3(一)

作者头像
RiemannHypothesis
发布2022-08-19 15:32:44
8270
发布2022-08-19 15:32:44
举报
文章被收录于专栏:Elixir

一、Spring Framework Overview

Spring 官方文档 Version 5.3.13

Spring makes it easy to create Java enterprise applications. Spring is open source.

Spring Framework

  • Spring简化了企业级应用的开发,通过Spring的核心IoC容器管理JavaBean,降低耦合
  • Spring是开源框架
  • Spring是非入侵式的,Spring开发的应用中对象不依赖Spring API
  • Spring DI依赖注入,是IOC控制反转的经典体现
  • Spring AOP 面向切面编程
  • Spring 组件化,由Spring IoC管理的JavaBean可以通过xml文件配置或者注解来实现

狭义的Spring即是指Spring Framework本身,即Core Container 核心容器,随着时间的推移,以Core Container为核心发展出了许多模块,广义的Spring即指众多Spring的模块,如Spring MVC, Spring Boot,Spring Data,Spring Cloud等,点击https://spring.io/projects 可以查看到Spring的众多模块

The term "Spring" means different things in different contexts. It can be used to refer to the Spring Framework project itself, which is where it all started. Over time, other Spring projects have been built on top of the Spring Framework. Most often, when people say "Spring", they mean the entire family of projects. This reference documentation focuses on the foundation: the Spring Framework itself.

Spring Framework 模块划分

image.png
image.png

The IoC Container

Ioc 即 Inversion of Control,控制反转,由IoC 容器负责创建Bean代替通过new关键字创建

DI 即 Dependency Injection,依赖注入,IoC容器能知道哪个组件或类运行时需要另外一个组件或类,通过反射的方式注入

image.png
image.png

创建 IoC Container

创建maven项目 ➡️ 添加maven依赖 ➡️ 创建配置文件 ➡️ 测试

代码语言:javascript
复制
<properties>
    <spring-version>5.3.13</spring-version>
</properties>


<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-version}</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
</dependencies>
entity包中新增实体类Person
代码语言:javascript
复制
public class Person {
    
    private String lastName;
    private Integer age;
    private String gender;
    private String email;
    // 省略getter/setter/toString方法
}
resource目录下创建配置文件beans.xml

xml示例文件可官方文档中找到

代码语言:javascript
复制
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    将Person实体类注册到IoC容器中
    <bean id="person" class="com.citi.entity.Person">
    </bean>

</beans>

3⃣️ 编写测试类

代码语言:javascript
复制
public class ContainerTest {

    // 测试从容器中获取Bean
    @Test
    public void testStark(){
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");
        Person stark = (Person)context.getBean("stark");
        System.out.println(stark);
    }
}

输出结果

image.png
image.png

以上就完成了将Bean注册到容器,并从容器中获取Bean,整个过程没有使用new关键字来获取Bean,而是通过xml配置文件,将bean的属性全部写入xml文件中,在通过ApplicationContext接口的getBean()方法获取Bean

The org.springframework.context.ApplicationContext interface represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the beans.

Several implementations of the ApplicationContext interface are supplied with Spring. In stand-alone applications, it is common to create an instance of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext.

ApplicationContext是IoC容器接口,负责实例化、配置和组装Bean,它有两个实现类ClassPathXmlApplicationContext和FileSystemXmlApplicationContext 这两个类实例化时需要传入配置文件,但是传入配置文件的方式不同

代码语言:javascript
复制
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");
ApplicationContext context1 = new FileSystemXmlApplicationContext("src/main/resources/beans.xml");

Bean 是什么时候实例化的? 在Person类中增加无参数构造方法,并添加输出语句

代码语言:javascript
复制
public class Person {

    private String lastName;
    private Integer age;
    private String gender;
    private String email;

    //无参构造方法
    public Person() {
        System.out.println("无参构造方法被调用");
    }
    // 省略getter/setter/toString方法
}       

在ContainerTest类中增加输出语句

代码语言:javascript
复制
public class ContainerTest {

    // 测试从容器中获取Bean
    @Test
    public void testStark(){
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");
        System.out.println("容器已创建完成");
        Person stark = (Person)context.getBean("stark");
        System.out.println(stark);
    }
}

执行测试

image.png
image.png

Bean是在容器初始化是创建的,不是在getBean时创建的

在ContainerTest方法中在获取一次Bean,验证两次获取的对象是否相等

代码语言:javascript
复制
// 测试从容器中获取Bean
@Test
public void testStark(){
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");
    System.out.println("容器已创建完成");
    Person stark = (Person)context.getBean("stark");
    Person stark1 = (Person)context.getBean("stark");

    System.out.println(stark == stark1);
}

输出结果

image.png
image.png

结果为true,说明Person类只实例化了一次,就是在容器初始化的时候。

IoC 容器总结

  • ApplicationContext是IoC容器接口,有两个实现类ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,通过getBean()获取容器中的bean
  • 往容器中注册一个bean,bean的创建是由容器完成的,bean在容器初始化时就已经被创建完成
  • 同一个bean在容器中是单实例的
  • 容器中没有这个bean,会报错bean创建异常“No bean named 'xxx' is defined”
  • IoC容器在创建bean时,xml中property会利用类的setter设置bean的属性值
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-12-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、Spring Framework Overview
    • Spring Framework
      • The IoC Container
        • 创建 IoC Container
          • entity包中新增实体类Person
          • resource目录下创建配置文件beans.xml
        • IoC 容器总结
        相关产品与服务
        容器服务
        腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档