• SpringBoot SpringBoot 原理篇 1 自动配置 1.7 bean 的加载方式【五】


    SpringBoot

    【黑马程序员SpringBoot2全套视频教程,springboot零基础到项目实战(spring boot2完整版)】

    SpringBoot 原理篇

    1 自动配置

    1.7 bean 的加载方式【五】
    1.7.1 register

    之前我们又说了一种加载bean 的方式,使用@Import 注解可以无侵入的将一个普通类变成一个bean 放到容器中进行管理。

    现在来说第五种:

    这种方式平时开发的时候不常用,但是如果是要做一些框架的话就可能会用到…【啊这】

    直接开干,先来一个运行程序

    package com.dingjiaxiong.app;
    
    import com.dingjiaxiong.config.SpringConfig33;
    import com.dingjiaxiong.config.SpringConfig4;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    /**
     * ClassName: App5
     * date: 2022/10/24 13:42
     *
     * @author DingJiaxiong
     */
    
    public class App5 {
    
        public static void main(String[] args) {
    
            ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig4.class);
    
            String[] names = ctx.getBeanDefinitionNames();
            for (String name : names) {
                System.out.println(name);
            }
            System.out.println("=========================");
            
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    直接运行一下看看

    在这里插入图片描述

    OK,出来了这些东西无所谓

    第五种方式就是在上下文对象已经初始化完毕之后,手工加载bean

    ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig4.class);
    
    • 1

    在这里插入图片描述

    不要配置类、也不要配置文件【直接编程干】

    【怎么做?】

    在这里插入图片描述

    package com.dingjiaxiong.app;
    
    import com.dingjiaxiong.bean.Cat;
    import com.dingjiaxiong.config.SpringConfig33;
    import com.dingjiaxiong.config.SpringConfig4;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    /**
     * ClassName: App5
     * date: 2022/10/24 13:42
     *
     * @author DingJiaxiong
     */
    
    public class App5 {
    
        public static void main(String[] args) {
    
            AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig4.class);
            //在上下文对象已经初始化完毕之后,手工加载bean
    
            ctx.registerBean("tom", Cat.class);
            
            String[] names = ctx.getBeanDefinitionNames();
            for (String name : names) {
                System.out.println(name);
            }
            System.out.println("=========================");
    
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    现在就手工加载了一个tom

    直接运行,看看效果

    在这里插入图片描述

    没啥毛病,上来了

    如果多复制几个, 看看会不会有冲突

    在这里插入图片描述

    运行结果

    在这里插入图片描述

    可以看到,出来了, 说明没有冲突

    那究竟是哪个留下来了,验证一下,修改一下实体类

    package com.dingjiaxiong.bean;
    
    import org.springframework.stereotype.Component;
    
    /**
     * ClassName: Cat
     * date: 2022/10/24 10:33
     *
     * @author DingJiaxiong
     */
    
    //这个注解就代表了 这个标签
    @Component("tom")
    public class Cat {
        public Cat(){
        }
    
        int age;
    
        public Cat(int age) {
            this.age = age;
        }
    
        @Override
        public String toString() {
            return "Cat{" +
                    "age=" + age +
                    '}';
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    再修改一下运行类

    package com.dingjiaxiong.app;
    
    import com.dingjiaxiong.bean.Cat;
    import com.dingjiaxiong.config.SpringConfig33;
    import com.dingjiaxiong.config.SpringConfig4;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    /**
     * ClassName: App5
     * date: 2022/10/24 13:42
     *
     * @author DingJiaxiong
     */
    
    public class App5 {
    
        public static void main(String[] args) {
    
            AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig4.class);
            //在上下文对象已经初始化完毕之后,手工加载bean
    
            ctx.registerBean("tom", Cat.class,0);
            ctx.registerBean("tom", Cat.class,1);
            ctx.registerBean("tom", Cat.class,2);
    
            String[] names = ctx.getBeanDefinitionNames();
            for (String name : names) {
                System.out.println(name);
            }
            System.out.println("=========================");
            System.out.println(ctx.getBean(Cat.class));
    
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    直接运行,看看是哪只猫

    在这里插入图片描述

    OK,效果很明显,最后一个留下来了【就像map,因为key 一样,最终只留了最后那个】

    这个很有用,假如说现在系统里面有个bean,现在再来一个,把原来那个覆盖了,这就相当于把现有东西给隐藏掉了,只有新的生效了

    即一个新的值替换老的值【这个场景就很多了,不做配置用老的值,但凡我一做,就用我们自己的值】

    最后再提一个,如果我们仅仅是想注册进去一个bean,还可更简单

    ctx.register(Mouse.class);
    
    • 1

    直接运行

    在这里插入图片描述

    并不是全路径

    OK, 回顾一下

    • 使用上下文对象在容器初始化完毕后注入bean

    在这里插入图片描述

  • 相关阅读:
    mysql批量插入数据,跳过唯一索引报错
    this.$once(‘hook:beforeDestory‘,()),销毁定时器
    由C# dynamic是否装箱引发的思考
    R语言使用dplyr包的mutate函数基于自定义的组合判断条件将dataframe中的特定数值数据列进行因子化(分组、分箱)、并生成新的数据列
    STC8单片机PWM定时器+EC11编码器实现计数
    Vue控制textarea可输入行数限制-案例
    2022年9月26日 9点 程序爱生活 纳指和恒指可能要创新低,然后迎来反弹,很大概率继续往下。
    MySQL执行计划EXPLAIN
    三、用户增长模型:S-C-I战略模型
    【MongoDB】docker部署社区版(一)
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/128048570