码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • SpringBoot SpringBoot 原理篇 1 自动配置 1.6 bean 的加载方式【四】


    SpringBoot

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

    SpringBoot 原理篇

    文章目录

        • SpringBoot
        • SpringBoot 原理篇
        • 1 自动配置
          • 1.6 bean 的加载方式【四】
            • 1.6.1 @Import

    1 自动配置

    1.6 bean 的加载方式【四】
    1.6.1 @Import

    OK,前面咱们已经学习了三种初始化bean 的方式了,中间还学习了关于 proxyBeanMethods 属性的知识

    在这里插入图片描述

    接下来就来看看第四种

    先来一个新的配置类

    package com.dingjiaxiong.config;
    
    /**
     * ClassName: SpringConfig4
     * date: 2022/10/24 14:52
     *
     * @author DingJiaxiong
     */
    
    
    public class SpringConfig4 {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    非常干净的配置类

    再来一个新的运行类

    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: App4
     * date: 2022/10/24 13:42
     *
     * @author DingJiaxiong
     */
    
    public class App4 {
    
        public static void main(String[] args) {
    
            ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig4.class);
    
            String[] names = ctx.getBeanDefinitionNames();
            for (String name : names) {
                System.out.println(name);
            }
        }
    
    }
    
    • 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

    直接运行看看

    在这里插入图片描述

    OK, 没啥问题

    第四种方式是使用 @Import

    package com.dingjiaxiong.config;
    
    import com.dingjiaxiong.bean.Dog;
    import org.springframework.context.annotation.Import;
    
    /**
     * ClassName: SpringConfig4
     * date: 2022/10/24 14:52
     *
     * @author DingJiaxiong
     */
    
    @Import(Dog.class)
    public class SpringConfig4 {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    再次运行

    在这里插入图片描述

    可以看到,这个东西确实上来了

    看看是不是真的加载到了

    在这里插入图片描述

    OK,没问题,确实加载到了Dog

    而且这种方式加载出来的类是一个非常标准的全路径类名,而且是大写开头的

    【所以这种方式的作用体现在哪里?】

    之前我们加注解、写配置,现在使用直接导入,原本的类咱们压根儿没动过

    在这里插入图片描述

    可以进行有效的解耦

    Spring 倡导无侵入式 编程或者叫无入侵式编程

    说加就加、看不到动过的痕迹

    就现在这种直接导入的方式会是一种非常好用的最佳实践,以后咱们有了一个外部类,可以完全不做改变的把搞成一个bean放入我们的容器

    OK, 回顾一下

    使用@Import注解导入要注入的bean对应的字节码

    在这里插入图片描述

    【@Import 的扩展】

    我现在给它导入一个配置类

    在这里插入图片描述

    package com.dingjiaxiong.config;
    
    import com.dingjiaxiong.bean.Dog;
    import org.springframework.context.annotation.Import;
    
    /**
     * ClassName: SpringConfig4
     * date: 2022/10/24 14:52
     *
     * @author DingJiaxiong
     */
    
    @Import({Dog.class, DbConfig.class})
    public class SpringConfig4 {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    直接运行

    在这里插入图片描述

    可以看到直接就上来了俩,而且是全路径类名,而且它里面的bean 也被加载了

    如果把配置类上的注解拿掉

    在这里插入图片描述

    这就变成Dog 了

    直接运行看看效果

    在这里插入图片描述

    DbConfig 上来是应该的,和Dog 原理一样,而且可以看到,它里面定义的bean 还是上来了

    就是这样

    在这里插入图片描述

    OK,就是这样

  • 相关阅读:
    TDengine 官网换了新“皮肤”,来看看这个风格是不是你的菜
    无线互动会议室方案的视频显示系统
    [HeMIM]Cl,[AeMIM]Br,[CeEIM]Cl,([HO-PECH-MIM]Cl,[HOOC-PECH-MIM]Cl改性酚醛树脂
    Worthington植物原生质体制备丨纤维素酶方案
    2020 MIT6.s081 Lab: xv6 lazy page allocation
    道可云元宇宙每日资讯|上海多个热门元宇宙文旅项目迎来消费高峰
    数据治理在数据提取中的角色:确保数据质量和安全
    理解 Paimon changelog producer
    springboot项目中添加大屏页面
    WebRTC[52] - WebRTC 带宽分配逻辑详解
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/128031735
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号