【Spring框架】我终于读懂了ApplicationContext(案例详解)

1年前 (2022) 程序员胖胖胖虎阿
265 0 0

【辰兮要努力】:hello你好我是辰兮,很高兴你能来阅读,昵称是希望自己能不断精进,向着优秀程序员前行!

博客来源于项目以及编程中遇到的问题总结,偶尔会有读书分享,我会陆续更新Java前端、后台、数据库、项目案例等相关知识点总结,感谢你的阅读和关注,希望我的博客能帮助到更多的人,分享获取新知,大家一起进步!

吾等采石之人,应怀大教堂之心,愿大家奔赴在各自的热爱里…

文章目录

      • 一、基础概念
      • 二、案例学习

一、基础概念

本期重点分享ApplicationContext基础概念以及应用场景

Spring容器最基本的接口就是BeanFactory。BeanFactory负责配置、创建、管理Bean;

ApplicationContext由BeanFactory派生而来;BeanFactory的许多功能需要编程实现,而在ApplicationContext中则可以通过配置的方式实现;

【Spring框架】我终于读懂了ApplicationContext(案例详解)

ApplicationContext因此也称之为Spring上下文。Spring容器负责管理Bean与Bean之间的依赖关系。


二、案例学习

本篇同步会复习一下Spring作用域相关知识singleton + prototype

直接上案例看看ApplicationContext到底可以帮助我们做什么?

创建一个user对象

@Data
public class User {
    private String id;
    private String name;
    private Integer age;
}

创建一个applicationContext.xml的配置文件
【Spring框架】我终于读懂了ApplicationContext(案例详解)

<?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:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <!--    singleton-->
    <bean id="UserSingleton" class="com.chenxi.demo.po.User" scope="singleton">
        <property name="id" value="1"/>
        <property name="name" value="辰兮"/>
        <property name="age" value="22"/>
    </bean>

    <!--    prototype-->
    <bean id="UserPrototype" class="com.chenxi.demo.po.User" scope="prototype" >
        <property name="id" value="2"/>
        <property name="name" value="辰兮要努力"/>
        <property name="age" value="23"/>
    </bean>

</beans>

创建一个测试类帮助我们学习ApplicationContext

import com.chenxi.demo.po.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @program: ApplicationContextDemo
 * @description: ApplicationContext入门学习
 * @author: 辰兮要努力
 * @create: 2021-11-10 22:07:54
 */
public class ApplicationContextDemo {

    //打印日志
    private static final Logger logger = LoggerFactory.getLogger(ApplicationContextDemo.class);

    public static void main(String[] args) {
        /**
         *
         * ApplicationContext体系结构
         * 主要实现类:
         * ClassPathXmlApplicationContext:默认从类路径加载配置文件
         * FileSystemXmlApplicationContext:默认从文件系统中装载配置文件
         */
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

        /**
         * Singleton:单实例(在容器启动完成之前就已经创建好了,保存在容器中了,任何时候获取都是获取之前创建好的那个对象)
         */
        User user = (User) applicationContext.getBean("UserSingleton");
        User user1 = (User) applicationContext.getBean("UserSingleton");
        /**
         * 换一种写法:<T> T getBean(String var1, Class<T> var2) throws BeansException;
         */
        User user2 =  applicationContext.getBean("UserSingleton",User.class);

        logger.info("user.hashCode()是:{}",user.hashCode());
        logger.info("user1.hashCode()是:{}",user1.hashCode());

        logger.info("user是:{}",user);
        logger.info("user1是:{}",user1);
        logger.info("user == user1 :{}",user == user1);



        /**
         * Prototype:多实例(容器启动默认不会创建多实例bean对象,只有在获取的时候才创建,每次获取都会创建一个新的实例对象)
         */
        User user3 = (User) applicationContext.getBean("UserPrototype");
        User user4 = (User) applicationContext.getBean("UserPrototype");

        logger.info("user3.hashCode()是:{}",user3.hashCode());
        logger.info("user4.hashCode()是:{}",user4.hashCode());
        logger.info("user3是:{}",user3);
        logger.info("user4是:{}",user4);
        logger.info("user3 == user4 :{}",user3 == user4);

    }
}

执行代码:控制台日志

22:00:32.921 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user.hashCode():1446002
22:00:32.922 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user1.hashCode():1446002
22:00:32.922 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user是:User(id=1, name=辰兮, age=22)
22:00:32.922 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user1是:User(id=1, name=辰兮, age=22)
22:00:32.922 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user == user1 :true
22:00:32.923 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user3.hashCode():266875004
22:00:32.923 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user4.hashCode():266875004
22:00:32.923 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user3是:User(id=2, name=辰兮要努力, age=23)
22:00:32.923 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user4是:User(id=2, name=辰兮要努力, age=23)
22:00:32.923 [main] INFO com.chenxi.demo.utils.ApplicationContextDemo - user3 == user4 :false

代码逻辑

创建Spring的工厂类

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

通过工厂解析XML获取Bean实例

User user = (User) applicationContext.getBean("UserSingleton");

拓展补充

1、对象相等则hashCode一定相等;
2、hashCode相等对象未必相等

如上是我为什么会将两个对象用 == 来比较,刚好可以复习一下spring作用域相关知识点


ApplicationContext总结

如果说BeanFactory是Spring的心脏,那么ApplicationContext就是完整的身躯了。

ApplicationContext由BeanFactory派生而来,提供了更多面向实际应用的功能。

【Spring框架】我终于读懂了ApplicationContext(案例详解)
Context我们通常解释为上下文环境,用“容器”来表述它更容易理解一些,ApplicationContext则是“应用的容器”


非常感谢你阅读到这里,如果这篇文章对你有帮助,希望能留下你的点赞👍 关注❤️ 分享👥 留言💬thanks!!!

2021年11月10日22:16:39 愿你们奔赴在自己的热爱里!

相关文章

暂无评论

暂无评论...