什么是SpringBoot
SpringBoot是一个自动配置框架,设计思想:默认优于配置的原则,让Spring开发变得更新的方便快捷
Spring,SpringMVC,Springboot的区别
Spring是一个轻量级的自动配置框架,方便用于web开发 SpringMVC是一个用于web开发的mvc框架,原生的与Spring整合 SpringBoot是一个为了简化Spring配置的脚手架,让人们更加专注于业务开发
SpringBoot与SpringCloud的区别
SpringBoot常用Starter
| starter | 作用 |
|---|---|
| web-starter | 提供SpringMVC和内嵌Tomcat |
| redis-starter | redis客户端 |
| mybatis-starter | Mybatis数据库连接 |
SpringBoot有哪几种读取配置的方式
application.properties配置文件
profile.name=SpringBoot Profile
profile.desc=SpringBoot Desc
- 使用@Value
@Value("${profile.name}")
private String name;
- 使用@ConfigurationProperties
@Data
@Component
@ConfigurationProperties("profile")
public class Profile {
private String name;
private String desc;
}
- 使用@PropertySource
@PropertySource("classpath:application.properties")
注意:默认只支持properties格式文件读取
- 使用Environment
@Autowired
private Environment environment;
@Override
public void afterPropertiesSet() throws Exception {
log.info("profile.getName[{}]", environment.getProperty("profile.name"));
}
- 使用Properties(原生方式读取)
Properties properties = new Properties();
InputStreamReader isr = new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream("application.properties"));
properties.load(isr);
log.info("profile.name[{}]", properties.getProperty("profile.name"));
Springboot自动配置原理
通过EnableAutoConfiguration注解,SpringBoot会自动扫描start中的spring.factories文件中的配置类,完成自动配置
SpringBoot配置加载顺序
properties,yaml,系统环境变量,命令行参数
SpringBoot初始化环境变量流程
运行SpringBoot有哪几种方式
- 打包成jar包,使用 java -jar xx.jar的方式
- 打包成war包,放到tomcat运行
- 用maven插件运行,maven spring-boot:run
- 执行main方法
SpringBoot中如何解决跨越问题 实现WebMvcConfigurer,重写addCorsMappings方法
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("*")
.allowCredentials(true)
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.maxAge(3600);
}
如何禁用特定的启动类
使用@SpringBootApplication注解的exclede属性
Springboot常用注解
重要注解(主要注解) @SpringBootApplication,@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan
@ImportAutoConfiguration
注解普通类 @Controller @RestController @Service @Repository @Component
注解配置类 @Configuration @Import @ImportResource
注解字段 @Value
注解方法 @Bean @Condition @ConditionOnClass @ConditionOnMissBean