1.基本配置
1.1入口類
創(chuàng)建一個(gè)Spring Boot項(xiàng)目通常會(huì)生成一個(gè)帶有@SpringBootApplication啟動(dòng)類搜囱。該啟動(dòng)類里面有一個(gè)main方法盹靴。在方法中使用SpringApplication.run啟動(dòng)Spring Boot應(yīng)用項(xiàng)目惹骂。
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Configuration
SpringBootApplication是Spring Boot的核心注解榜贴。它是一個(gè)組合注解鉴分。
/**
* Indicates a {@link Configuration configuration} class that declares one or more
* {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration
* auto-configuration} and {@link ComponentScan component scanning}. This is a convenience
* annotation that is equivalent to declaring {@code @Configuration},
* {@code @EnableAutoConfiguration} and {@code @ComponentScan}.
*
* @author Phillip Webb
* @author Stephane Nicoll
* @since 1.2.0
*/
@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 {
//...
}
代碼注釋清晰的說(shuō)明劣砍,這個(gè)@SpringBootApplication相當(dāng)于在類上同時(shí)聲明
@Configuration惧蛹、@EnableAutoConfiguration、@ComponentScan
@EnableAutoConfiguration讓Spring Boot 根據(jù)類路徑中的jar包依賴為當(dāng)前項(xiàng)目進(jìn)行自動(dòng)配置刑枝,并且會(huì)自動(dòng)掃描入口類的同級(jí)包以及下級(jí)包中的Bean香嗓。eg:項(xiàng)目中添加org.springframework.boot:spring-boot-starter-web jar包,那么項(xiàng)目會(huì)自動(dòng)添加Tomcat和Spring MVC的依賴装畅。
建議@SpringBootApplication放置在groupId+arctifactId組合的包名下靠娱。
1.2 SpringBootApplication的重
- exclued 排除特定的自動(dòng)配置類
/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
@AliasFor(annotation = EnableAutoConfiguration.class, attribute = "exclude")
Class<?>[] exclude() default {};
1.4定制Banner
- 一、修改banner
在項(xiàng)目的src/main/resources下新建一個(gè)banner.txt
- 二掠兄、關(guān)閉banner
在main里的內(nèi)容修改為:
- 1.使用SpringApplicationBuilder
new SpringApplicationBuilder(Application.class).bannerMode(Banner.Mode.OFF).run(args);
- 2.使用SpringApplication
SpringApplication springApplication = new SpringApplication();
springApplication.setBannerMode(Banner.Mode.OFF);
springApplication.run(args);
- 3.在application.properties設(shè)置(推薦)
spring.main.banner-mode=off
1.5 Spring Boot的配置文件
Spring Boot使用一個(gè)全局的配置文件application.properties或application.yml像云。放置在src/main/resources目錄或者類路徑的/config下锌雀。(就是resources下的config文件)
Spring Boot的全局配置文件的作用是對(duì)一些默認(rèn)配置的配置值進(jìn)行修改。
- 修改端口號(hào)及訪問(wèn)路徑
server.port=8010
server.context-path=/strivelearn
1.6 starter pom
Spring Boot為我們提供了簡(jiǎn)化企業(yè)級(jí)開發(fā)絕大多場(chǎng)景的starter pom,只要使用了應(yīng)用場(chǎng)景所需要的starter pom迅诬,相關(guān)的技術(shù)配置將會(huì)消失汤锨,就可以得到Spring Boot為我們提供的自動(dòng)配置的Bean。
這些starter pom 可以看官網(wǎng)文檔 13.1. Spring Boot application starters
1.7 使用xml配置
Spring Boot提倡零配置百框,即無(wú)xml配置闲礼,但是在實(shí)際項(xiàng)目中,可能有一些特殊要求你必須使用xml配置铐维,這時(shí)候我們可以通過(guò)Spring Boot提供的@ImportResource來(lái)加載xml配置
2.外部配置
Spring Boot 允許使用properties文件或者命令行參數(shù)作為外部配置柬泽。
2.1 命令行參數(shù)配置
Spring Boot可以基于jar包運(yùn)行的,打成 jar包的程序可以直接通過(guò)下面的命令運(yùn)行嫁蛇。
java -jar xx.jar
2.2 常規(guī)屬性配置
在Spring Boot 里锨并,我們只需要在application.properties定義屬性。直接使用@Value注入睬棚。
- 在properties里面定義屬性
user.name=strivelearn
user.password=123456
- 后臺(tái)代碼
@RestController
public class HomeController {
@Value("${user.name}")
private String username;
@Value("${user.password}")
private String password;
@RequestMapping("index")
public String index() {
return "Hello World." + username + ";" + password;
}
}
2.3類型安全的配置
如果這種配置很多的話第煮,使用@Value一個(gè)一個(gè)的注入會(huì)顯得格外麻煩∫值常可以使用@ConfigurationProperties將properties屬性和一個(gè)Bean及其屬性關(guān)聯(lián)包警,從而實(shí)現(xiàn)類型安全配置。
- 1.創(chuàng)建pojo
@Component
@ConfigurationProperties(prefix = "usr")
public class UserProperties {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
- 代碼調(diào)用
@Autowired
private UserProperties userProperties;
@RequestMapping("index")
public String index() {
//return "Hello World." + username + ";" + password;
return userProperties.getName() + ";" + userProperties.getPassword();
}
通過(guò)@ConfigurationProperties加載properties文件內(nèi)的配置底靠,通過(guò)perfix屬性指定文件內(nèi)的配置害晦。也可以通過(guò)locations指定properties文件的位置。
3.日志配置
Spring Boot默認(rèn)使用Logback作為日至框架暑中。
配置日志文件
logging.file=log.log
配置日志級(jí)別
logging.level.org.springframework.web=debug
4.Profile配置
Profile是Spring用來(lái)針對(duì)不同的環(huán)境對(duì)不同的配置提供支持的壹瘟。全局Profile配置使用
appplication-{profile}.properties
通過(guò)在application.properties中設(shè)置
spring.profile.active=xx
來(lái)指定活動(dòng)的Profile。
application-qa.properties
application-stage.properties
application-prod.properties
spring.profile.active=qa
5.Spring Boot運(yùn)行原理
Spring Boot主要幫助我們解決了自動(dòng)配置的問(wèn)題鳄逾。而其源碼在spring-boot-autoconfigure-x jar中稻轨。
5.1查看當(dāng)前項(xiàng)目的啟動(dòng)日志
1.運(yùn)行jar時(shí)增加 --debug參數(shù)
2.在application.properties中設(shè)置屬性
debug=ture
- 3.在啟動(dòng)設(shè)置的VM arguments設(shè)置參數(shù)
-Ddebug