从 Spring 的创建到 Bean 对象的存储、读取

这篇具有很好参考价值的文章主要介绍了从 Spring 的创建到 Bean 对象的存储、读取。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

创建 Spring 项目:

1.创建一个 Maven 项目:

 2.添加 Spring 框架支持:

3.配置资源文件:

4.添加启动类:

Bean 对象的使用:

1.存储 Bean 对象:

1.1 创建 Bean:

1.2 存储 Bean 到容器内:

2.获取 Bean 对象:

2.1 创建 Spring 上下文:

2.2 获取指定 Bean 对象:

ApplicationContext 和 BeanFactory 的区别:

ApplicationContext:

BeanFactory:

总结:

三种常用的 getBean :

根据 id 获取:

对象类型获取:

id + 对象类型获取:

总结


从 Spring 的创建到 Bean 对象的存储、读取


创建 Spring 项目:

        创建一个 Spring 项目分为四步:

  1.  创建一个普通 Maven 项目;
  2.  添加 Spring 框架支持;
  3.  配置资源文件;
  4.  添加启动类;

1.创建一个 Maven 项目:

前提声明:个人尽量使用 idea2021,因为2022版之后的相关插件是收费的;

从 Spring 的创建到 Bean 对象的存储、读取

从 Spring 的创建到 Bean 对象的存储、读取

 2.添加 Spring 框架支持:

        将这段配置依赖添加到 Spring 项目的 pom.xml 文件中:刷新下载到本地仓库

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.24</version>
        </dependency>
    </dependencies>

3.配置资源文件:

从 Spring 的创建到 Bean 对象的存储、读取

        在 resources 包中创建一个 xxx.xml 文件,再把 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"
       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">
    <content:component-scan base-package="com.spring.demo"></content:component-scan>
</beans>

4.添加启动类:

        在 java 文件下创建一个类,专门用来启动测试:

public class School {
    public static void main(String[] args) {

    }
}

Bean 对象的使用:

1.存储 Bean 对象:

        原理:Spring 框架有 spring-context 来管理 spring 上下文,还有 spring-beans 来管理对象的模块;

        什么是 Bean对象?项目中重复使用的对象都可以视为 Bean 对象,存储 Bean 对象也就是把项目所需的对象放入 Spring 容器中;

1.1 创建 Bean:

        现在创建了一个普通的 Student 类,其自带一个 sayHi() 方法,由于以后我还要频繁使用这个类,那么就可以将其视为一个 Bean:

public class Student {
    public void sayHi() {
        System.out.println("hi student");
    }
}

1.2 存储 Bean 到容器内:

        存储bean也相当于“声明”的作用,先打开刚才在 resources包里创建的 spring-config.xml 配置文件:

从 Spring 的创建到 Bean 对象的存储、读取

存储 Bean 的格式:

<bean id="" class=""><bean>

如果 bean 在多级包内,class属性设置时就要注意路径;

现在将 Student 这个类作为 bean 添加进去:注意路径

从 Spring 的创建到 Bean 对象的存储、读取

<!--    将Bean对象(com.spring.demo.com.spring.demo.Student)
存到 Spring容器中,它的 id 为 student-->
    <bean id="student" class="com.spring.demo.Student"></bean>

2.获取 Bean 对象:

        获取 bean 对象分为三步:

  1. 得到 Spring 上下文对象:因为 Bean 对象交给了 Spring 来管理,所以得先得到 Spring 才能有权限操作容器;
  2. 通过 Spring 上下文获取容器内的 Bean 对象;
  3. 使用 Bean 对象;

2.1 创建 Spring 上下文:

        这里需要了解两个接口:ApplicationContext BeanFactory

        语法:

ApplicationContext context = 
                new ClassPathXmlApplicationContext("spring-config.xml");
 BeanFactory beanFactory =
                new XmlBeanFactory(new ClassPathResource("spring-config.xml"));

2.2 获取指定 Bean 对象:

        这里借助 ApplicationContext 来演示:

public class School {
    public static void main(String[] args) {
        // 1.得到 Spring 上下文
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        // 2.获取指定 Bean 对象
        Student student = (Student) context.getBean("student");
        // 3.使用 Bean 对象
        student.sayHi();
    }
}

从 Spring 的创建到 Bean 对象的存储、读取

至此 Spring 的创建、Bean 的存储和获取已经做了基本介绍;


ApplicationContext 和 BeanFactory 的区别:

        在上述代码中再添加一个 Teacher 类作为一个新的 Bean,添加到容器中:

从 Spring 的创建到 Bean 对象的存储、读取

public class Teacher {
    public Teacher() {
        System.out.println("do teacher init");
    }

    public void sayHi() {
        System.out.println("hi teacher");
    }
}
    <bean id="student" class="com.spring.demo.Student"></bean>
    <bean id="teacher" class="com.spring.demo.Teacher"></bean>

此时我们的容器中有两个 Bean:一个是Student、一个是Teacher;

        分别使用 ApplicationContext 和 BeanFactory 来操作:都只尝试获取容器中 Student 这个 Bean,来看看它们的效果有什么不同.

ApplicationContext:

        原代码不用变: 

public class School {
    public static void main(String[] args) {
        // 1.得到 Spring 上下文
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        // 2.获取指定 Bean 对象
        Student student = (Student) context.getBean("student");
        // 3.使用 Bean 对象
        student.sayHi();
    }
}

BeanFactory:

        创建一个新的启动类:School2,获取 Spring 上下文和 ApplicationContext 不一样,其他也不用变。

public class School2 {
    public static void main(String[] args) {
        // 1.使用 BeanFactory 来获取 Spring 上下文 
        BeanFactory beanFactory =
                new XmlBeanFactory(new ClassPathResource("spring-config.xml"));
        // 2. 从 Spring 容器中获取 bean 对象
        Student student = (Student) beanFactory.getBean("student");
        student.sayHi();
    }
}

从 Spring 的创建到 Bean 对象的存储、读取

         对比运行结果,明明它们两个都时获取和使用 id 为 “student” 的 Bean 对象,ApplicationContext 对于上下文却把 Teacher 也拿了出来,BeanFactory 只拿到了 Student;

总结:

相同点:

  • ApplicationContext 和 BeanFactory 都是获取容器中Bean对象的;

不同点:

  •  ApplicationContext 是一次性加载并初始化容器里的所有 Bean 对象(饿汉模式),而 BeanFactory 是需要哪个才去加载哪个(懒汉模式);
  • ApplicationContext 其实是 BeanFactory 的子类,子类不仅继承了父类的所有功能外,还拥有自己独特的功能;而ClassPathXmlApplicationContext 又属于 ApplicationContext的子类;
  • BeanFactory 也是最早被设计出来的,设计之初由于机器硬件造价昂贵,就只加载需要的对象;而 ApplicationContext 是之后设计出来的,人们为了尽可能提高效率,就想到一次性加载出所有 Bean 对象;

三种常用的 getBean :

        getBean() 方法有很多重载,对比以下常用的三种:

  1. 根据 id 获取;
  2. 对象类型获取;
  3. id,对象类型获取;

根据 id 获取:

    <bean id="student" class="com.spring.demo.Student"></bean>
    <bean id="teacher" class="com.spring.demo.Teacher"></bean>
        // 2.获取指定 Bean 对象
        Student student = (Student) context.getBean("student");
        Teacher teacher = (Teacher) context.getBean("teacher");

这种方式显然比较粗暴,不可取,存在强转;另外如果通过id获取的对象为null,也会出现异常;

对象类型获取:

    <bean id="student" class="com.spring.demo.Student"></bean>
    <bean id="teacher" class="com.spring.demo.Teacher"></bean>
        // 2.获取指定 Bean 对象
        Student student = context.getBean(Student.class);
        Teacher teacher = context.getBean(Teacher.class);

这种方式虽然不粗暴,但存在一个问题:

        当同一类型的 Bean 在容器中注册了两次,编译器就会报 NoUniqueBeanDefinitionException 异常;

id + 对象类型获取:

        这种方式安全性较高;

    <bean id="student" class="com.spring.demo.Student"></bean>
    <bean id="student2" class="com.spring.demo.Student"></bean>

    <bean id="teacher" class="com.spring.demo.Teacher"></bean>
        // 2.获取指定 Bean 对象
        Student student = context.getBean("student2",Student.class);
        Teacher teacher = context.getBean("teacher",Teacher.class);

思考:
        既然刚才提到了同一个类型可能在容器中注册多次,虽然它们只是 id 不同,那它们指向的是否为同一块空间呢?

        代码验证一下:

    <bean id="student" class="com.spring.demo.Student"></bean>
    <bean id="student2" class="com.spring.demo.Student"></bean>

    <bean id="teacher" class="com.spring.demo.Teacher"></bean>
public class School {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
       
        Student student1 = context.getBean("student", Student.class);
        Student student2 = context.getBean("student2", Student.class);
       
        System.out.println(student1 == student2);
        System.out.println(student1.equals(student2));
    }
}

从 Spring 的创建到 Bean 对象的存储、读取

于是证明它们本身就是不同的对象的引用;


总结:

Spring 项目的创建及 Bean 的基本使用流程:

从 Spring 的创建到 Bean 对象的存储、读取文章来源地址https://www.toymoban.com/news/detail-457786.html

到了这里,关于从 Spring 的创建到 Bean 对象的存储、读取的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Spring项目创建与Bean的存储与读取(DL)

    第一步,创建 Maven 项目 ,Spring 也是基于 Maven 的。 由于国外源不稳定,可能让下面第二步引入 Spring 依赖会失败,所以这里先介绍如何一下配置国内镜像源。 现成的 settings.xml 文件链接:gitee 如果你已经有了 settings.xml 文件,但没有配置 mirror ,配置内容如下: 如果你是在引

    2024年02月17日
    浏览(28)
  • 【JavaEE】DI与DL的介绍-Spring项目的创建-Bean对象的存储与获取

    Spring的开发要点总结 Spring的初步了解博客:【JavaEE】JavaEE进阶:框架的学习 - Spring的初步认识_s:103的博客-CSDN博客 就不带大家回顾了~ 从框架获取的对象称为获取【Bean对象】! Java是咖啡,Bean就是\\\"咖啡豆\\\",也就是“对象” Spring项目开发就是 开业 , 放咖啡豆到罐子里 , 后

    2024年02月16日
    浏览(32)
  • Spring(二):更简单的存储与读取 Bean

    通过上一章的Spring,我们基本实现了Spring 的读取与存储,但是在操作过程中,读取与存储并没有那么得“简单” 一套流程还是很复杂,所以,本章来介绍更加简单得读取与存储。 在 Spring 中想要更简单的存储和读取对象的核⼼是使⽤注解,也就是我们接下来要学习 Spring 中的

    2024年02月15日
    浏览(27)
  • 使用Spring的五大类注解读取和存储Bean

    目录 1.存储Bean对象的注解 1.1 五大类注解 1.2 方法注解 1.3添加注解的依赖 2.注解的使用 2.1  controller注解 2. 2Service注解  2.3.Resopsitory注解 2.4Component注解 2.5Configuration注解 2.6 注解之间的关系 3.方法注解 3.1 方法注解要配合类注解来使用。 3.2 重命名Bean 4.获取Bean对象(对象装配

    2024年02月01日
    浏览(32)
  • Spring使用注解存储Bean对象

    在前一篇博客中( Spring项目创建与Bean的存储与读取(DL))介绍的是通过配置文件注册对象从而存储到 Spring 中,这种方式其实还是挺繁琐的。 实际上,在使用学习使用 Spring过程中,当我们要实现一个功能的时候,先应该考虑的是有没有相应的注解是实现对应功能的,Spring 中

    2024年02月16日
    浏览(34)
  • spring中存储和获取bean对象

    存储 Bean 分为以下 2 步: 存储 Bean 之前,先得有 Bean 才⾏,因此先要创建⼀个 Bean, 说白了也就是写一个类。 告诉spring,把这个类的对象存起来。 编写 User 类, 有 sayHi 方法。 告诉 spring ,把这个类的对象存起来。 在配置文件中可以通过 bean 标签来告诉 spring 把对象存起来。

    2024年02月15日
    浏览(34)
  • Spring——更快捷的存储 / 获取Bean对象

    本人是一个普通程序猿!分享一点自己的见解,如果有错误的地方欢迎各位大佬莅临指导,如果你也对编程感兴趣的话,互关一下,以后互相学习,共同进步。这篇文章能够帮助到你的话,劳请大家点赞转发支持一下! 上篇文章中,向Spring容器中添加对象,还要去配置文件里手动添

    2024年02月15日
    浏览(33)
  • Spring使用注解存储和读取对象

    之前我们存储Bean时,需要在spring-config.xml中添加bean注册才行,这样的方式并不简单。我们要想 更简单的存储和读取对象的核心是使用注解 1.使用类注解(五大类注解): @Controller:控制器,验证用户请求的数据正确性(安保系统) @Service:服务层,编排和调度具体执行方法的(客服

    2023年04月19日
    浏览(34)
  • Spring 更简单的读取和存储对象

    在 Spring 中要想更简单的存储和读取对象 , 核心是 使用注解 , 所以我们需要通过 Spring 中相关注解 , 来存储和读取 Bean 对象. 之前我们存储 Bean 时 , 需要在 spring-config.xml 中添加一行注释才行: 而现在我们只需一个注解就可以替代之前要写一行配置 , 不过在存储对象之前 , 我们先

    2024年02月02日
    浏览(31)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包