Spring Boot QuickStart (2) - 基础

环境:Spring Boot 1.5.4java

基于 Spring Boot 建立一个命令行应用,先来个最基本的体验,体验一下:mysql

  • 配置管理(配置文件加载,多环境配置文件)spring

  • 日志sql

  • 单元测试app

建立项目

比较好的两种方法:框架

  1. 经过 https://start.spring.io/ 网站,生成项目框架,导入到 IDEmaven

  2. IDEA 有Spring Boot的插件,直接按照提示建立ide

  3. 其余spring-boot

建立个最基本的应用,包含了devtools,logging,test,以及maven插件:单元测试

...
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.3.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
...
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
    
    ...

配置管理

修改 banner

Spring Boot 的默认 banner:

.   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.3.RELEASE)

resources 目录下建立一个 banner.txt 文件能够修改,而且还提供了一些参数,能够配色。

固然也能够在配置文件或入口处关闭:

spring.main.banner-mode=off

public static void main(String[] args) {
   SpringApplication application = new SpringApplication(HelloApplication.class);
   application.setBannerMode(Banner.Mode.OFF);
   application.run(args);
}

关闭 banner 居然还弄这么多方式,我也是醉了,其实只是展现一下在入口处还能够进行不少应用的配置罢了。

自定义属性

若是不是特殊的应用场景,就只须要在 application.properties 中完成一些属性配置就能开启各模块的应用。

application.properties:

mysql.host=default
mysql.user=default_user
mysql.mix=${mysql.host}/${mysql.user}

如上所示:参数之间也能够使用变量直接引用来使用

定义 MysqlProperties Class:

@Component
public class MysqlProperties {
    @Value("${mysql.host:localhost}")
    private String host;

    @Value("${admin.user:root}")
    private String user;
    
    // 省略getter、setter、toString
}

@Value 注解未免有点蛋疼

能够使用 @ConfigurationProperties 注解直接配置个属性前缀,同时还能够加载一个额外的 .properties 文件

app.properties:

app.name=hello
app.version=1.0

定义 AppProperties Class:

@Component
@PropertySource("classpath:app.properties")
@ConfigurationProperties(prefix = "app")
public class AppProperties {
    private String name;
    private String version;
    
    // 省略getter、setter、toString
}

命令行运行

Spring Boot 默认 Application 定义了 main 方法入口,因此要实现一个命令行运行的应用,须要实现 CommandLineRunner 接口,覆写 run 方法,这样命令行参数就经过变长参数 strings 接受到。

@SpringBootApplication
public class HelloApplication implements CommandLineRunner {

    @Override
    public void run(String... strings) throws Exception {
    }
}

多环境配置

Spring Boot中多环境配置文件名须要知足application-{profile}.properties的格式,其中{profile}对应你的环境标识,如:

application-dev.properties:开发环境
application-test.properties:测试环境

同时,须要在application.properties文件中经过spring.profiles.active属性来设置,其值对应{profile}值,而且能够设置多个。

其次,经过命令行参数 --spring.profiles.active=test 能够切换多环境。好比:

java -jar xxx.jar --spring.profiles.active=test

日志

Spring Boot 默认使用 Logback 做为第一选择,默认集成了 slf4j,而且支持配置使用 Log4j:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

log4j2 貌似和 log4j 有点变化,暂时不折腾了

单元测试