SpringBoot是目前软件中最主流的框架,无论是工作还是面试基本都有它的身影,SpringBoot主要解决了传统spring的重量级xml配置Bean,实现了自动装配;所以,我们也常在面试中被问到SpringBoot是如何实现自动装配。
1.1三个重要的注解
我们可以发现,在使用main()
启动SpringBoot的时候,只有一个注解@SpringBootApplication
我们可以点击进去@SpringBootApplication
注解中看看,可以发现有三个注解是比较重要的:
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
....
}
@SpringBootConfiguration
:我们点进去以后可以发现底层是Configuration注解,说白了就是支持JavaConfig的方式来进行配置(使用Configuration配置类等同于XML文件)。@EnableAutoConfiguration
:开启自动配置功能(后文详解)@ComponentScan
:这个注解,学过Spring的同学应该对它不会陌生,就是扫描注解,默认是扫描当前类下的package。将@Controller/@Service/@Component/@Repository
等注解加载到IOC容器中。
所以,Springboothappleday01Application类可以被我们当做是这样的: