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


    SpringBoot

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

    SpringBoot 原理篇

    1 自动配置

    1.2 bean 的加载方式【二】
    1.2.1 第二种方式

    上一次我们已经回顾了一下通过xml 配置文件的形式去定义bean

    在这里插入图片描述

    其他没啥, 就是特别繁琐【能不能简化?】 【答案是肯定的】

    于是Spring 就提供了一种实用注解的方式来定义bean

    就我们想把哪个类配置成受Spring 管控的bean,在类上面加注解就行了

    package com.dingjiaxiong.bean;
    
    import org.springframework.stereotype.Component;
    
    /**
     * ClassName: Cat
     * date: 2022/10/24 10:33
     *
     * @author DingJiaxiong
     */
    
    //这个注解就代表了 这个标签
    @Component("tom")
    public class Cat {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    package com.dingjiaxiong.bean;
    
    import org.springframework.stereotype.Service;
    
    /**
     * ClassName: Mounse
     * date: 2022/10/24 10:33
     *
     * @author DingJiaxiong
     */
    
    @Service("jerry")
    public class Mouse {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    但是这样又有了个新的问题,就这样写就能加载的话,那计算机上这么多类库,岂不是Spring 都要去扫一遍,这样工作量太大

    为了降低Spring 的工作强度,还是要配置一下【就你告诉Spring ,去哪儿找】

    applicationContext2.xml

    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           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
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
        ">
    
        
        <context:component-scan base-package="com.dingjiaxiong.bean"/>
    
    beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    来一个新的运行程序

    package com.dingjiaxiong.app;
    
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * ClassName: App2
     * date: 2022/10/24 10:43
     *
     * @author DingJiaxiong
     */
    
    public class App2 {
    
        public static void main(String[] args) {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext2.xml");
    
            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

    直接运行

    在这里插入图片描述

    可以看到, 东西还有点多

    但是tom、jerry 已经加载上了

    这就是第二种bean 的加载方式,就是使用组件扫描 + 类上面写注解 来配置

    那现在第三方的bean 怎么定义呢?总不能去改人家的源代码加注解吧

    是可以做的,先来一个配置类

    package com.dingjiaxiong.config;
    
    import com.alibaba.druid.pool.DruidDataSource;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Component;
    
    /**
     * ClassName: DbConfig
     * date: 2022/10/24 13:29
     *
     * @author DingJiaxiong
     */
    
    @Component
    public class DbConfig {
    
        @Bean
        public DruidDataSource dataSource(){
            DruidDataSource ds = new DruidDataSource();
            return ds;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    修改一下扫描

    在这里插入图片描述

    再次运行

    在这里插入图片描述

    可以看到已经上来了,而且dbConfig 也上来了,这倒不奇怪

    如果我换个注解

    在这里插入图片描述

    再次运行

    在这里插入图片描述

    好家伙,效果没变,这意味着这两个有关系,

    在这里插入图片描述

    OK,这大致就是使用注解 + 扫描配置 去加载 bean的介绍

    回顾一下

    XML+注解方式声明bean

    在这里插入图片描述

    使用@Component及其衍生注解@Controller 、@Service、@Repository定义bean

    在这里插入图片描述

  • 相关阅读:
    如何区分快解析内网穿透和Nginx端口映射?
    【简单、高效、性能好】SetFit:无需Prompts的高效小样本学习
    【LeetCode】每日一题&&两数之和&&寻找正序数组的中位数&&找出字符串中第一个匹配项的下标&&在排序数组中查找元素的第一个和最后一个位置
    电源modbus 485 测试方法之功能选择
    Java以form-data(表单)的形式调用第三方接口
    stable diffusion艰难炼丹之路
    CSDN21天学习挑战赛 - 第四篇打卡文章
    Maven安装详解
    .Net下验证MongoDB 的 Linq 模式联合查询是否可用
    [Android]从app的trace打桩原理回顾zygote的fork
  • 原文地址:https://blog.csdn.net/weixin_44226181/article/details/128031498