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 有點變化,暫時不折騰了

單元測試

相關文章
相關標籤/搜索