@SpringBootApplication注解介绍
@SpringBootApplication
在中已经启动了⼀个最精简的springboot 应⽤.编写的代码来看,它与⾮spring boot应⽤区别有两个地⽅:
(1)在QuickStartApplication类上添加了注解@SpringBootApplication;
(2)是main⽅法调⽤SpringApplication.run(QuickStartApplication.class,args)
开始写程序:
⽤IDEA进⾏开发,所以使⽤快捷键也是IDEA的.案例代码:
@SpringBootApplication
public class QuickStartApplication {
public static void  main(String[]args){
SpringApplication.run(QuickStartApplication.class,args);
}
}
进⼊到@SpringBootApplication的源码,可以看到组合三个注
解:@ComponentScan,@EnableAutoConfiguration,@SpringBootConfiguration.分析这三个注解.
2.1@ComponentScan
spring⾥有四⼤注解:@Service,@Repository,@Component,@Controller⽤来定义⼀个bean.@ComponentScan注解就是⽤来⾃动扫描被这些注解标识的类,最终⽣成ioc容器⾥的bean.可以通过设置@ComponentScan basePackages,includeFilters,excludeFilters属性来动态确定⾃动扫描范围,类型已经不扫描的类型.默认情况下:它扫描所有类型,并且扫描范围是@ComponentScan注解所在配置类包及⼦包的类,在 ⼯程⾥,添加了⼀个componentscan 分⽀,说明这个情况.QuickStartApplication,CurrentPackageController类的package是com.simosponentscan.SubPackageController的package 是ller.
⽽OutPackageController的package是ller.启动后只有:SubPackageController,C
urrentPackageController被扫描⽣成bean,⽽OutPackageController却没有被扫描到.下⾯三个截图:
current.png
sub.png
使⽤@SpringBootApplication注解,就说明使⽤了@ComponentScan的默认配置,这就建议你把使⽤@SpringBootApplication注解的类放置在root package(官⽅表述)下,其他类都置在root package的⼦包⾥⾯,这样bean就不会被漏扫描.
2.2@SpringBootConfiguration
这个注解的作⽤与@Configuration作⽤相同,都是⽤来声明当前类是⼀个配置类.可以通过@Bean注解⽣成IOC容器管理的bean.在QuickStartApplication中定义bean,并在@HelloController中注⼊使⽤
@SpringBootApplication
public class QuickStartApplication {
public static void  main(String[]args){
SpringApplication.run(QuickStartApplication.class,args);
}
@Bean
public BeanTest beanTest(){
return  new BeanTest();
}
}
下⾯是@HelloController
@RestController
public class HelloController {
@Autowired
BeanTest beanTest;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String hello(){
return "hello world!";
}
@RequestMapping(value = "/beantest",method = RequestMethod.GET)
public String beanTest(){
return "beanTest!";
}
}
2.3@EnableAutoConfiguration
@EnableAutoConfiguration是springboot实现⾃动化配置的核⼼注解,通过这个注解把spring应⽤所需的bean注⼊容器
中.@EnableAutoConfiguration源码通过@Import注⼊了⼀个ImportSelector的实现类AutoConfigurationImportSelector,这个ImportSelector最终实现根据我们的配置,动态加载所需的bean.
'AutoConfigurationImportSelector'的完成动态加载实现⽅法源码如下:
@Override
/
spring ioc注解/annotationMetadata 是@import所⽤在的注解.这⾥指定是@EnableAutoConfiguration
public String[] selectImports(AnnotationMetadata annotationMetadata) {
if (!isEnabled(annotationMetadata)) {
return NO_IMPORTS;
}
//加载XXConfiguration的元数据信息(包含了某些类被⽣成bean条件),继续跟进这个⽅法调⽤,就会发现加载的是:spring-boot-autoconfigure jar包⾥⾯MET    AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
.loadMetadata(this.beanClassLoader);
      //获取注解⾥设置的属性,在@SpringBootApplication设置的exclude,excludeName属性值,其实就是设置@EnableAutoConfiguration的这两个属性值
AnnotationAttributes attributes = getAttributes(annotationMetadata);
/
/从spring-boot-autoconfigure jar包⾥⾯META-INF/spring.factories加载配置类的名称,打开这个⽂件发现⾥⾯包含了springboot框架提供的所有配置类
List<String> configurations = getCandidateConfigurations(annotationMetadata,
attributes);
//去掉重复项
configurations = removeDuplicates(configurations);
//获取⾃⼰配置不需要⽣成bean的class
Set<String> exclusions = getExclusions(annotationMetadata, attributes);
//校验被exclude的类是否都是springboot⾃动化配置⾥的类,如果存在抛出异常
checkExcludedClasses(configurations, exclusions);
//删除被exclude掉的类
//过滤刷选,满⾜OnClassCondition的类
configurations = filter(configurations, autoConfigurationMetadata);
fireAutoConfigurationImportEvents(configurations, exclusions);
//返回需要注⼊的bean的类路径
StringArray(configurations);
}
总结
springboot是通过注解@EnableAutoConfiguration的⽅式,去查,过滤,加载所需的configuration,@ComponentScan⾃定义的
bean,@SpringBootConfiguration使得被@SpringBootApplication注解的类声明为注解类.因此@SpringBootApplication的作⽤等价于同时
组合使⽤@EnableAutoConfiguration,@ComponentScan,@SpringBootConfiguration.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。