Spring @ComponentScan 注解
@ComponentScan 主要就是定义扫描的路径从中找出标识了需要装配的类自动装配到spring的bean容器中!
@ComponentScan注解的详细使用
做过web开发的同学一定都有用过@Controller,@Service,@Repository注解,查看其源码你会发现,他们中有一个共同的注解@Component,没错@ComponentScan注解默认就会装配标识了@Controller,@Service,@Repository,@Component注解的类到spring容器中
代码示例
@SpringBootApplication
@ComponentScan(basePackages = {
"com.pangugle1",
"com.pangugle2",
"com.pangugle3"
}, nameGenerator = SimpleNameGenerator.class)
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
参数说明:
- basePackages 包的路径,可以多个
- nameGenerator bean 在容器中的key生成策略