记一次笔记:ComponentScan指定扫描包,导致默认包扫描器失效

导读:本篇文章讲解 记一次笔记:ComponentScan指定扫描包,导致默认包扫描器失效,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

springboot启动类启动时,会默认扫描启动类所在的目录,在启动类上添加ComponentScan去指定扫描位置,从而可以扫多个包,尤其是第三方中的jar/maven依赖中的组件

但是,当指定扫描包后,原来的包默认扫描器会随之失效,如果不注意,会导致项目的组件没办法使用,如下:

记一次笔记:ComponentScan指定扫描包,导致默认包扫描器失效

当在启动类指定扫描第三方的组件包,如下:

package com.shuizhu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = {"com.thrid"})
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ShuizhuMultipleDsApplication {

    public static void main(String[] args) {
        SpringApplication.run(ShuizhuMultipleDsApplication.class, args);
    }

}

我的controller层有个接口吗,如下:

package com.shuizhu.controller;

import com.bosera.dw.commons.controller.BaseCtrl;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 睡竹
 * @date 2022/9/20
 */
@RestController
public class TestController {
    @GetMapping("/test")
    public String test(){
        return "success";
    }
}

 当访问该接口时,会报404

还需要在启动类上,添加当前项目的包位置,如下:

package com.shuizhu;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = {"com.thrid","com.shuizhu"})
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class ShuizhuMultipleDsApplication {

    public static void main(String[] args) {
        SpringApplication.run(ShuizhuMultipleDsApplication.class, args);
    }

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/99567.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!