• Spring 中 Bean 对象的存储和取出


    由于 Spring 拥有对象的管理权,所以我们也需要拥有较为高效的对象存储和取出的手段,下面我们来分别总结一下:

    存对象

    配置文件

    在存储对象之前,我们需要先配置一下 Spring 的扫描目录,这样 Spring 即可在正确的目录中识别出我们想要给交给 Spring 进行管理的对象

    首先在项目中的 resources 目录下添加一个 xml 文件,名字可以自定义

    在这里插入图片描述

    随后在这个 xml 中添加如下代码,这段代码可以直接复制,但是要修改倒数第二行的 base-package 中的路径改成自己的路径

    
    <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="dir">content:component-scan>
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    类注释

    在 Spring 中我们可以通过类注释或者方法注释来进行对象的存储,其中类注释是用的比较多的,类注释总共有 5 种:

    • @Conntroller,业务逻辑层
    • @Service,服务器存储
    • @Repository,仓库存储,可以用来操作数据库
    • @Component,项目组件层
    • @Configuration,配置层

    这五种类注释的作用都是可以直接将这个类对象储存到 Spring 中,但是却有 5 种类注释,这么做的原因是:能够让我们看到类注释就能得知这个类的大致功用

    命名规则

    类注释中有两套命名规则

    • 如果当前类名中前两个字母都是大写,那么存入 Spring 中的对象名字默认是类名
    • 否则名字为第一个字母转为小写后的类名

    除了上面 5 种类注释之外,还有方法注释

    方法注释

    方法注释就是将注释@Bean加到方法上方,并且必须搭配类方法使用

    作用:将这个方法返回的对象存入到 Spring 中

    例如

    @Controller
    public class Student {
        @Bean
        private Student getStudent() {
            return new Student();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    通过 @Bean 注释存入 Spring 中的对象名字默认是方法名,我们也可以直接在 @Bean 后面直接加上参数指定这个对象的名字,例如

    @Controller
    public class Student {
        @Bean(name="sb")  // 设置对象的名字
        private Student getStudent() {
            return new Student();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    取对象

    在 Spring 中,获取 Spring 中的对象也有另一种叫法:对象装配
    主要有三种对象装配的方式

    属性注入

    我们可以直接在成员变量中添加 @Autowired 注释, @Autowired 会优先匹配 Spring 中类名相同的对象,例如下面代码中的 user,Autowired 会优先按照 user 的类型在 spring 中进行匹配

    @Controller
    public class Student {
        @Autowired
        private User user;
    
        @Bean(name="sb")
        private Student getStudent() {
            return new Student();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    这种注入方式很简单也很快,但是缺点不少:

    • 可移植性差,这个用法是建立在 IoC 容器上的,移植到其他框架上可能无法使用
    • 无法注入不可变的对象,也就是 final 修饰的对象
      因为 final 修饰的变量只有两个初始化时机,第一个是直接赋值,第二个是在类的构造方法中赋值,显然这种对象注入的方式无法满足其中一个条件
    • 由于使用简单,容易滥用,所以比较容易被违背单一设计原则(即一个类只干一件事)

    setter 注入

    首先创建一个要注入的对象,然后通过「右键 — generate — setter」生成一个 setter 方法,最后在这个 setter 方法头顶上添加 @Autowired 注释

    @Controller
    public class Student {
    
        private User user;
    
        @Autowired  // 通过 setter 注入
        public void setUser(User user) {
            this.user = user;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    setter 注入的缺点:

    • 由于是 setter 方法,所以可能多次调用,这就意味着这个对象可以被多次修改
    • 依旧无法注入 final 修饰的对象,原因同上

    构造方法注入

    顾名思义,这是在构造方法上添加 @Autowired 注释来注入对象,如下

    @Controller
    public class Student {
        private User user;
    
        @Autowired
        public Student(User user) {
            this.user = user;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    构造方法注入的优点:

    • 由于是构造方法,只会执行一次,所以对象不会被重复更改
    • 可以注入 final 修饰的对象(在构造方法中赋值)
    • JDK本身就支持构造方法,所以有更低的门槛,说人话:具有更强的可移植性
  • 相关阅读:
    聚焦算力技术发展,中科驭数成功举办“新一代智算中心”技术生态沙龙
    AI应用开发入门10:登录验证功能低代码开发实战
    紧跟热点:教你如何快速掌握ChatGPT
    国内主机整车EEA架构汇总
    【go】报错整理与解决
    OC-NSSet(集合)
    失业在家的6个月,我通过外包全款买了房:你看不起的行业,往往很赚钱
    阿里云 OSS
    新建SpringCloud电商前端Vant项目
    Vue2:组件及组件通信
  • 原文地址:https://blog.csdn.net/weixin_63519461/article/details/127855362