@ComponentScan 详解

本文介绍了@ComponentScan注解,它主要用于定义扫描路径,将标识了需要装配的类自动装配到Spring的bean容器中。默认会装配标识了@Controller、@Service、@Repository、@Component注解的类。若未成功装配,在Spring Boot多模块项目中会导致异常。

1,@ComponentScan注解是什么

其实很简单,@ComponentScan主要就是定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中。

2,@ComponentScan注解的详细使用

做过web开发的同学一定都有用过@Controller,@Service,@Repository注解,查看其源码你会发现,他们中有一个共同的注解@Component,没错@ComponentScan注解默认就会装配标识了@Controller,@Service,@Repository,@Component注解的类到spring容器中

如果未成功将需要装配的类装配到spring bean的容器中,那么将无法使用相关的类,导致异常,特别是spingboot 多模块项目,如果未扫描装配的话,就会导致异常。

package com.web.admin;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@MapperScan("com.web.mapper")
@ComponentScan("com")
public class AdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class, args);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值