@SpringBootApplication
在hello world中已经启动了一个最精简的springboot 应用.编写的代码来看,它与非spring boot应用区别有两个地方:
(1)在QuickStartApplication类上添加了注解@SpringBootApplication;
(2)是main方法调用SpringApplication.run(QuickStartApplication.class,args)
开始写程序:
用IDEA进行开发,所以使用快捷键也是IDEA的.案例代码:
@SpringBootApplicationpublic class QuickStartApplication {public static void main(String[]args){ SpringApplication.run(QuickStartApplication.class,args);}}进入到@SpringBootApplication的源码,可以看到组合三个注解:@ComponentScan,@EnableAutoConfiguration,@SpringBootConfiguration.分析这三个注解.
2.1@ComponentScanspring里有四大注解:@Service,@Repository,@Component,@Controller用来定义一个bean.@ComponentScan注解就是用来自动扫描被这些注解标识的类,最终生成ioc容器里的bean.可以通过设置@ComponentScan basePackages,includeFilters,excludeFilters属性来动态确定自动扫描范围,类型已经不扫描的类型.默认情况下:它扫描所有类型,并且扫描范围是@ComponentScan注解所在配置类包及子包的类,在hello word 工程里,添加了一个componentscan 分支,说明这个情况.QuickStartApplication,CurrentPackageController类的package是com.simos.componentscan.SubPackageController的package 是com.simos.componentscan.controller.而OutPackageController的package是com.simos.controller.启动后只有:SubPackageController,CurrentPackageController被扫描生成bean,而OutPackageController却没有被扫描到.下面三个截图:
current.png
sub.png
使用@SpringBootApplication注解,就说明使用了@ComponentScan的默认配置,这就建议你把使用@SpringBootApplication注解的类放置在root package(官方表述)下,其他类都置在root package的子包里面,这样bean就不会被漏扫描.
2.2@SpringBootConfiguration
这个注解的作用与@Configuration作用相同,都是用来声明当前类是一个配置类.可以通过@Bean注解生成IOC容器管理的bean.在QuickStartApplication中定义bean,并在@HelloController中注入使用
@SpringBootApplicationpublic class QuickStartApplication {public static void main(String[]args){ SpringApplication.run(QuickStartApplication.class,args); }@Beanpublic BeanTest beanTest(){ return new BeanTest(); }}下面是@HelloController
@RestControllerpublic class HelloController {@AutowiredBeanTest 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'的完成动态加载实现方法源码如下:
总结
springboot是通过注解@EnableAutoConfiguration的方式,去查找,过滤,加载所需的configuration,@ComponentScan自定义的bean,@SpringBootConfiguration使得被@SpringBootApplication注解的类声明为注解类.因此@SpringBootApplication的作用等价于同时组合使用@EnableAutoConfiguration,@ComponentScan,@SpringBootConfiguration.