springboot官网声称:springboot是设计用来让你的应用快速运行起来,并尽可能的减少你的配置文件
springboot解决了spring的什么问题?
- 起步依赖(-starter),自动导入需要的jar包
- 自动配置,无需繁琐的配置
常用功能
1、 @SpringBootApplication是Spring Boot项目的核心注解,主要目的是开启自动配置。
2、@RestController是@Controller和@ResponseBody的组合注解
3、@ConfigurationProperties(prefix = “person”)是将配置文件中以person开头的属性值通过setXX()方法注入到实体类对应属性中
4、@PropertySource注解结合@Configuration注解配置类的方式来实现加载自定义配置文件
@Configuration
@PropertySource("classpath:test.properties") // 指定自定义配置文件位置和名称
@EnableConfigurationProperties(MyProperties.class) // 开启对应配置类的属性注入功
能
@ConfigurationProperties(prefix = "article")
public class MyProperties {
private int author;
private String name;
// 省略属性getXX()和setXX()方法
// 省略toString()方法
}
以上配置如果使用@Component而非@Configuration,可以省略@EnableConfigurationProperties
article.name=talk about farm
article.author=changhf
通过locations
指定properties的位置,默认为application.properties,可以不配置locations
@Data
@Component
@ConfigurationProperties(prefix="article",locations={"classpath:config/app.properties})
public class ArticleSetting{
private String name;
private String author;
}
5、springboot实现请求转发
@RequestMapping(value = "hello", method = RequestMethod.GET)
public void redirect(HttpServletResponse response) throws IOException {
response.sendRedirect("/monitor/reportData");
}
6、spring-boot-starter-parent是一个特殊的starter,它用来提供相关的maven默认依赖,常用的包依赖可以省去version标签
spring-boot-maven-plugin是springboot的编译插件
springboot可以基于jar包运行
java jar xx.jar --server.prot=9090
7、默认情况下,springboot采用logback
作为日志框架,日志配置文件固定位logback-spring.xml
8、通过在application.yml设置spring.profies.active=prod
来指定活动的profile
9、关闭特定的自动配置
@SpringBootApplication(
exclude = {
BatchAutoConfiguration.class,
JdbcTemplateAutoConfiguration.class,
DataSourceAutoConfiguration.class
}
)
10、所有@Enable*,如@EnableAsync都有一个@Import注解,分别有一个配置类,@Import是用来导入配置类的
11、springboot配置文件yml中可以用last-name或last_name来映射lastName
12、springboot同时支持多种模板引擎技术,通常情况下springboot会一次轮询每种模板引擎是否能处理指定的视图名。这主要看pom.xml中添加了哪种模板技术。比较常用的有Thymeleaf、Freemarker
13、为了使用事务,需要在pom中添加spring-boot-starter-jdbc依赖
事务上下文:controller中调用service方法的时候自动开启事务上下文,调用此方法后自动结束而提交事务。
14、通过maven命令运行springboot项目,mvn spring-boot:run