• Spring的创建和使用


    Spring 就是⼀个包含了众多工具方法的 IoC 容器。既然是容器那么它 就具备两个最基本的功能: 将对象(bean)存储到容器(Spring)中; 从容器中将对象取出来。

    1.创建Spring项目

    接下来使用Maven 方式来创建⼀个 Spring 项目,创建 Spring 项目和 Servlet 类似,总共分为以下 3 步:

    1.创建⼀个普通 Maven 项目。
    2. 添加 Spring 框架⽀持(spring-context、spring-beans)。
    3. 添加启动类。

    1.1创建一个Maven项目

    在这里插入图片描述
    点击下一步:
    在这里插入图片描述
    在这里插入图片描述

    1.2添加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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    Spring项目中添加的框架有 spring-context:spring 上下文,还有 spring-beans:管理对象的模块。
    在这里插入图片描述
    添加成功:
    在这里插入图片描述

    1.3添加启动类

    在创建好的项目java 文件夹下创建⼀个启动类,包含 main 方法即可:
    在这里插入图片描述

    非必须,作业是为了方便测试spring框架的功能(存对象和取对象)。

    2.将对象(Bean)存储到Spring中

    存储Bean分为2步:
    1.先创建一个Bean.
    2.将创建的Bean注册到Spring容器中。

    2.1创建Bean

    Bean就是Java语言中的一个普通对象,实现代码如下:

    package beans;
    
    public class User {
        public void sayHi(String name){
            System.out.println("你好,"+name);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在这里插入图片描述

    2.2将Bean存储到Spring

    1.如果是第一次存储bean,那么需要先创建spring配置文件。在创建好的项目中添加 Spring 配置文件 spring-config.xml,将此文件放到 resources 的根目录下,如 下图所示:
    在这里插入图片描述
    Spring 配置文件的固定格式为以下内容:

    <?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">
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    接下来,再将 user 对象注入到 Spring 中就可以,具体操作是在 中添加如下配置:

     <bean id="user" class=" beans.user"></bean>
    
    • 1

    在这里插入图片描述

    3.获取并使用Bean

    获取并使用Bean对象,分为以下3步:
    1.得到Spring上下文对象,因为对象都交给Spring管理了,所以获取对象要从Spring中获取,那么就先得到Spring的上下文。
    2.通过Spring上下文,获取某一个指定Bean对象。
    3.使用Bean对象。

    如果取多个Bean的话重复以上2、3步骤

    3.1创建Spring上下文

    spring上下文对象有两种使用方式:
    (1)ApplicationContext
    (2)BeanFactory

    1.Spring上下文对象可使用ApplicationContext,实现代码如下:

      //先得到上下文对象
            ApplicationContext context=
                    new ClassPathXmlApplicationContext("spring-config.xml");
    
    • 1
    • 2
    • 3

    在这里插入图片描述
    2.使用BeanFactory来作为Spring的上下文,如下所示:

    import beans.User;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.beans.factory.xml.XmlBeanFactory;
    import org.springframework.core.io.ClassPathResource;
    
    /**
     * 通过BeanFactory获取bean
     */
    public class App2 {
        public static void main(String[] args) {
            //1.先得到spring获取bean的对象
            BeanFactory beanFactory=new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
            //2.获取某个bean
           User user= (User) beanFactory.getBean("user");
           //3.使用bean
            user.sayHi("李四");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述
    ApplicationContext 和 BeanFactory 效果是⼀样的,ApplicationContext 属于 BeanFactory 的子类。
    3.ApplicationContext VS BeanFactory
    (1)继承关系和功能方面来说:Spring容器有两个顶级的接口:BeanFactory和ApplicationContext.其中BeanFactory提供了基础的访问容器的能力,而ApplicationContext属于Beanfactory的子类,它除了继承了BeanFactory的所有功能之外,它还拥有独特的特性,还添加了对国际化支持、资源访问支持、以及事件传播方面的支持。
    (2)从性能方面来说:ApplicationContext是一次性加载并初始化所有的Bean对象,而BeanFactory是需要哪个才去加载哪个,因此更加轻量。
    在这里插入图片描述

    3.2 从Spring上下文中获取指定的Bean对象

    //2.从spring中得到bean对象
            User user= (User) context.getBean("user");
    
    • 1
    • 2

    在这里插入图片描述

    3.2.1 注意事项

    Bean的ID要一一对应,如下图:
    在这里插入图片描述

    3.2.2 getBean的常见用法

    1.根据id获取bean
    在这里插入图片描述
    2.根据类型获取bean
    (1)用法:
    在这里插入图片描述
    在这里插入图片描述
    优点:写法简单
    缺点:如果spring中一个类型存在多个实例。那么程序就会报错。
    (2)二者的区别:当有一个类型被重复注入到spring-config.xml中时,只能使用根据id获取了,如下:
    根据id获取:
    在这里插入图片描述
    在这里插入图片描述
    根据类型获取会报错:
    在这里插入图片描述
    3.根据id+类型获取bean(推荐使用)
    在这里插入图片描述

    3.3 使用Bean

    import beans.User;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    /**
     * Spring启动类
     */
    public class App {
        public static void main(String[] args) {
            //1.先得到上下文对象
            ApplicationContext context=
                    new ClassPathXmlApplicationContext("spring-config.xml");
            //2.从spring中得到bean对象
            User user= (User) context.getBean("user");
            //3.使用对象
            user.sayHi("张三");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述
    总结
    加粗样式

  • 相关阅读:
    ES6解析赋值
    python+django餐厅外卖点餐系统Vue307
    定时执行专家 - 循环触发的危险操作,例如:电脑循环关机、循环重启、循环注销等,请谨慎尝试
    vue3 自定义loading
    JavaScript:实现binarySearch二分查找算法(附完整源码)
    数据建设实践之大数据平台(二)安装zookeeper
    TikTok为什么无法关注其他人?
    操作系统——并发相关问题
    SPI 实验
    C#的托盘窗体显示与隐藏效果 - 开源研究系列文章
  • 原文地址:https://blog.csdn.net/weixin_51970219/article/details/125583077