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

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

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

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

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

1SpringBootApplication 2@ComponentScan(basePackages = {"com.example"}) 3public class DemoApplication { 4 5 public static void main(String[] args) { 6 SpringApplication.run(DemoApplication.class, args); 7 } 8}
点赞
收藏

评论区

加载中...

相关推荐

SpringBoot包扫描之多模块多包名扫描和同类名扫描冲突解决

前言我们在开发springboot项目时候,创建好SpringBoot项目就可以通过启动类直间启动,运行一个web项目,非常方便简单,不像我们之前通过SpringSpringMvc要运行启动一个web项目还需要要配置各种包扫描和tomcat才能启动我将应用分成了parentcommoncomponentapp这种模式,1.parent是一个单纯的p

Springboot中如何在Utils类中使用@Autowired注入bean

Springboot中如果希望在Utils工具类中,使用到我们已经定义过的Dao层或者Service层Bean,可以如下编写Utils类:1\.使用@Component注解标记工具类StatisticsUtils:2\.使用@Autowired(@Autowired和@Resource的区别不再介绍)注入我们需要的bean:3\.在工具类中

SpringBoot和Spring中的注解

SpringBoot1、@SpringBootApplication开启组件扫描和自动配置。相当于Spring中的@Configuration、@ComponentScan、和SpringBoot中的@EnableAutoConfiguration2、@EnableAutoConfiguration启用S

MapperScannerConfigurer 作用详解

自动扫描将Mapper接口生成代理注入到Spring<!DAO接口所在包名,Spring会自动查找其下的类<beanclass"org.mybatis.spring.mapper.MapperScannerConfigurer"<propertyname"basePackage"va

SpringBoot和Spring中的注解

SpringBoot1、@SpringBootApplication开启组件扫描和自动配置。相当于Spring中的@Configuration、@ComponentScan、和SpringBoot中的@EnableAutoConfiguration2、@EnableAutoConfiguration启用S

Spring学习总结(2)——Spring的常用注解

本文汇总了Spring的常用注解,以方便大家查询和使用,具体如下:使用注解之前要开启自动扫描功能其中basepackage为需要扫描的包(含子包)。?(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.jb51.net%2Farticle%2F53586.htm%23)