1.SpringBoot中的核心注解是什么?
启动类上的注解是@SpringBooApplication,它是SpringBoot的核心注解,主要包含以下三个注解。
@SpringBootConfiguration:组合了@Configuration注解,实现配置文件的功能。
@EnableAutoConfiguration:打开自动配置的功能,也可以关闭某个自动配置的选项。如关闭数据源自动配置的功能@SpringBootConfiguration(exclude={DuridSourceAutoConfiguration.class})
@ComponentScan:Spring组件扫描,从当前类所在的包以及子包扫描,之外的包扫描不到。
2.SpringBoot自动装配原理
这个要从SpringBoot项目中的启动类说起,在启动类上有@SpringBootApplication,这个注解是SpingBoot的核心注解,点进去注解会看到三个注解:@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan。
启到关键作用的是@EnableAutoConfiguration,这个注解的作用是开启自动配置的功能,点进这个注解的源码会发现,这个注解是个符合注解,包含两个比较重要的注解@AutoConfigurationPackage和@Import,前者的作用就是将主配置类所在的包以及子包的所有组件扫描到IOC容器中,最关键的还是@Import这个注解,这个注解会导入一个类AutoConfigurationImportSelector这个类,这个类内部提供一个方法getCandidateConfigurations,这个方法里面又包含了一个loadFactoryNames静态方法,这个方法会扫描导入的所有jar包下的META-INF/spring.factory文件,解析文件中自动配置类,将列表中的类创建,并放到Spring容器中。
3.SpringBoot启动流程
引入依赖:在项目中,首先添加SpringBoot相关依赖,通常只需要引入一个Spring-Boot-starter相关的依赖。
编写主类:创建一个Java类作为SpringBoot应用的主类,加上@SpringBootApplication注解,这个注解包含了多个注解的合集,其中包括@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan。
主类中的main方法:编写一个main方法作为入口,使用SpringApplication.run(xxx.class,args)来启动SpringBoot应用。
自动配置:springBoot核心配置就是自动配置。起到关键作用的就是@EnableAutoConfiguration,这个注解就是开启自动配置功能,这个注解又是一个符合注解,包含两个比较重要的@AutoConfiguration和@Import,前者就是将主配置类所在的包以及子包的所有组件扫描到IOC容器中,最关键的还是@Import,这个注解会导入一个AutoConfigurationImportSelector这个类,这个类的内部提供一个方法getCandidateConfigurations,这个方法会扫描导入的所有jar包下的META-INF/spring.factoty文件,解析文件中自动配置类,将列表中的类创建,并放到spring容器中。
组件扫描:SpringBoot会扫描主类所在的包以及其子包下的组件,包括@component,@service,@controller等注解标注的类。
启动应用:执行run方法后,SpringBoot会根据自动配置和组件扫描加载所有必要的组件,并启动应用,会初始化spring容器并执行各种初始化操作。
运行应用:一旦运行成功,你的springBoot应用就可以响应外部的请求和操作了。
4.SpringBoot中的starter
starter是一种简化项目配置和依赖管理的概念,它包含了一系列可以集成到应用里面的依赖包,使用starter可以通过添加单个依赖项引入整个功能模块,无需手动配置大量的依赖项和设置。
5.SpringBoot中如何自定义starter
1.创建一个quickstart的maven工程,starter命名格式{name}-spring-boot-starter
2.starter需要使用到SpringBoot的自动配置功能,所以引入spring-boot-autoconfiguration的依赖,同时package改为jar。
3.定义一个属性Properties类,加上@ConfigurationProperties(spring.person)括号里边写配置文件中的名称
@ConfigurationProperties(prefix = "spring.person")
public class PersonProperties {
// 姓名
private String name;
// 年龄
private int age;
// 性别
private String sex = "M";
// Getter & Setter
}
5.定义自动配置类,并将核心服务类放到Spring 核心容器中。
@Configuration
@EnableConfigurationProperties(PersonProperties.class)
@ConditionalOnClass(PersonService.class)
@ConditionalOnProperty(prefix = "spring.person", value = "enabled", matchIfMissing = true)
public class PersonServiceAutoConfiguration {
@Autowired
private PersonProperties properties;
@Bean
@ConditionalOnMissingBean(PersonService.class) // 当容器中没有指定Bean的情况下,自动配置PersonService类
public PersonService personService(){
PersonService personService = new PersonService(properties);
return personService;
}
}
6.MATE-INF/spring.factories配置自动配置类
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.mengday.helloworld.PersonServiceAutoConfiguration
7.打包 mvn clean install
8.创建一个springBoot工程并引入依赖
9.配置application.properties
spring.person.name=mengday
spring.person.age=28