• bean无法被注入的常见问题和springboot的三种扫描并加载Bean方式


    一、bean无法被注入的常见问题

    某个类中的成员,如果是采用@Autowired注入Spring Bean,则当前类的实例,必须也是Spring Bean才能成功注入,即该实例不能用new xxx()来获得,这种方式获得的实例无法调用@Autowired注入的Bean,应该也采用@Autowired注入,注意在类上使用@Component或者@Service注解。
    在这里插入图片描述
    其他注入失败的原因,注意这句话:“SpringBoot项目的Bean装配默认规则是根据Application类所在的包位置从上往下扫描!“Application类”是指SpringBoot项目入口类。这个类的位置很关键:如果Application类所在的包为:com.boot.app,则只会扫描com.boot.app包及其所有子包,如果service或dao所在包不在com.boot.app及其子包下,则不会被扫描!即, 把Application类放到dao、service所在包的上级,com.boot.Application知道这一点非常关键”。
    或者在启动类上加上@ComponentScan注解显式标明要扫描的位置。
    启动类一定要放在业务代码的包之上。
    在这里插入图片描述

    二、springboot的三种扫描并加载Bean方式

    前言,默认情况下,springboot加载Bean对象给IOC容器管理,默认基础路径就是当前标注@SpringBootApplication启动类路径。
    一:@ComponentScan(“com.xxx”)
    当我们在springboot的启动类上面加上@ComponentScan(“com.xxx”)就是告诉spring去扫描根路径是:com.xxx下面的所有spring注解管理的Bean对象(其它第三方依赖jar包以com.xxx开头也会被扫描加载)将会被spring IOC容器管理。
    如下面spring注解管理的Bean对象:

    @Component
    @Service
    @Controller
    @Repository
    
    • 1
    • 2
    • 3
    • 4

    例子:

    @SpringBootApplication
    @ComponentScan("com.xxx")
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application .class, args);
            System.out.println();
            System.out.println("(♥◠‿◠)ノ゙  启动成功   ლ(´ڡ`ლ)゙");
            System.out.println();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    二:@Import({Xxx.class})
    当我们在springboot的启动类上面加上@Import({Xxx.class})就是告诉spring去直接加载Xxx.class的Bean对象(其它第三方依赖jar包类是Xxx.class也会被加载)将会被spring IOC容器管理。
    例子:

    @SpringBootApplication
    @Import({Xxx.class})
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application .class, args);
            System.out.println();
            System.out.println("(♥◠‿◠)ノ゙  启动成功   ლ(´ڡ`ლ)゙");
            System.out.println();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    三:spring.factories
    此方式就是经常面试问及的springboot自动装配@EnableAutoConfiguration原理。
    @EnableAutoConfiguration会扫描并加载:项目中第三方依赖jar包里面resources/META-INF/spring.factories文件中的全路径类,将会被spring IOC容器管理。
    假设A项目是一个springboot项目(依赖B项目),如下:

    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application .class, args);
            System.out.println();
            System.out.println("(♥◠‿◠)ノ゙  A项目启动成功   ლ(´ڡ`ლ)゙");
            System.out.println();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    B项目的resources/META-INF/spring.factories文件,如下:

    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
      com.xxx.A,\
      com.xxx.B
    
    
    • 1
    • 2
    • 3
    • 4
    public class A {
      //省略
    }
    public class B {
      //省略
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    那么,A项目会加载所依赖的B项目下面的A.class、B.class两个类,将会被spring IOC容器管理。

  • 相关阅读:
    android 后台运行service实现和后台的持续交互
    Jmeter之单接口的性能测试
    50-C语言-输入n个数,并且从中输出奇数,按升序排列
    iOS开发之机器学习框架MediaPipe(4)
    什么是大语言模型以及如何构建自己的大型语言模型?
    使用Python PySNMP模块获取设备指标
    Vue3中组件通讯的方式
    Ubuntu处理依赖问题
    Zebec Protocol 薪酬支付工具 WageLink 上线,掀新一轮薪酬支付浪潮
    HTML拖拽
  • 原文地址:https://blog.csdn.net/weixin_42408447/article/details/125437192