Spring Boot的@Service和@Autowired和@ComponentScan注解

Stella981
• 阅读 776

Spring Boot的@Service和@Autowired和@ComponentScan注解

SpringBoot项目的Bean装配默认规则是根据Application类(SpringBoot项目入口类)所在的包位置从上往下扫描,即只扫描该类所在的包及其子包。

1.如果需要扫描其上层包中的类,则将其移动到其上层包中

2.使用@ComponentScan注解,在SpringBoot启动类上注解配置,需要扫描的包。

Spring Boot的@Service和@Autowired和@ComponentScan注解

SpringBootApplication
@ComponentScan(basePackages = {"com.example"})
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
点赞
收藏
评论区
推荐文章
kenx kenx
2年前
SpringBoot包扫描之多模块多包名扫描和同类名扫描冲突解决
前言我们在开发springboot项目时候,创建好SpringBoot项目就可以通过启动类直间启动,运行一个web项目,非常方便简单,不像我们之前通过SpringSpringMvc要运行启动一个web项目还需要要配置各种包扫描和tomcat才能启动我将应用分成了parentcommoncomponentapp这种模式,1.parent是一个单纯的p
Easter79 Easter79
2年前
Springboot中如何在Utils类中使用@Autowired注入bean
Springboot中如果希望在Utils工具类中,使用到我们已经定义过的Dao层或者Service层Bean,可以如下编写Utils类:1\.使用@Component注解标记工具类StatisticsUtils:2\.使用@Autowired(@Autowired和@Resource的区别不再介绍)注入我们需要的bean:3\.在工具类中
Easter79 Easter79
2年前
SpringBoot和Spring中的注解
SpringBoot1、@SpringBootApplication开启组件扫描和自动配置。相当于Spring中的@Configuration、@ComponentScan、和SpringBoot中的@EnableAutoConfiguration2、@EnableAutoConfiguration启用S
Stella981 Stella981
2年前
MapperScannerConfigurer 作用详解
自动扫描将Mapper接口生成代理注入到Spring<!DAO接口所在包名,Spring会自动查找其下的类<beanclass"org.mybatis.spring.mapper.MapperScannerConfigurer"<propertyname"basePackage"va
Stella981 Stella981
2年前
SpringBoot和Spring中的注解
SpringBoot1、@SpringBootApplication开启组件扫描和自动配置。相当于Spring中的@Configuration、@ComponentScan、和SpringBoot中的@EnableAutoConfiguration2、@EnableAutoConfiguration启用S
Wesley13 Wesley13
2年前
Spring学习总结(2)——Spring的常用注解
本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下:使用注解之前要开启自动扫描功能其中basepackage为需要扫描的包(含子包)。?(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.jb51.net%2Farticle%2F53586.htm%23)
Easter79 Easter79
2年前
SpringBoot注解大全
一、注解(annotations)列表@SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。其中@ComponentScan让springBoot扫描到Configuration类并把它加入到程序上下文。@Configuratio
Stella981 Stella981
2年前
SpringBoot 核心配置
1\.入口类和@SpringBootApplication  SpringBoot的项目一般都会有\Application的入口类,入口类中会有main方法,这是一个标准的Java应用程序的入口方法。  @SpringBootApplication注解是SpringBoot的核心注解,它其实是一个组合注解:  !(https
Stella981 Stella981
2年前
SpringBoot注解大全
一、注解(annotations)列表@SpringBootApplication:包含了@ComponentScan、@Configuration和@EnableAutoConfiguration注解。其中@ComponentScan让springBoot扫描到Configuration类并把它加入到程序上下文。@Configuratio
Stella981 Stella981
2年前
Reflections框架,类扫描工具
Reflections是一个能提供一站式服务的对象。巧用Reflections库实现包扫描(扫描某个包中某个接口实现、注解等)它扫描工程的classpath,为元数据建索引,允许你运行时查询元数据信息。此外,他还能为你项目的lib库中所有的jar包保存和收集元数据。