天天看點

Spring Boot一些基礎配置

來源:http://mrbird.cc/Spring-Boot basic config.html

定制Banner

Spring Boot項目在啟動的時候會有一個預設的啟動圖案:

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

複制

我們可以把這個圖案修改為自己想要的。在src/main/resources目錄下建立banner.txt檔案,然後将自己的圖案黏貼進去即可。ASCII圖案可通過網站http://www.network-science.de/ascii/一鍵生成,比如輸入mrbird生成圖案後複制到banner.txt,啟動項目,eclipse控制台輸出如下:

_   _   _   _   _   _ / \ / \ / \ / \ / \ / \( m | r | b | i | r | d ) \_/ \_/ \_/ \_/ \_/ \_/ ...2017-08-12 10:11:25.952  INFO 7160 --- [main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup2017-08-12 10:11:26.057  INFO 7160 --- [main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)2017-08-12 10:11:26.064  INFO 7160 --- [main] com.springboot.demo.DemoApplication : Started DemoApplication in 3.933 seconds (JVM running for 4.241)           

複制

banner也可以關閉,在main方法中:

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

複制

全局配置檔案

在src/main/resources目錄下,Spring Boot提供了一個名為application.properties的全局配置檔案,可對一些預設配置的配置值進行修改。

附:application.properties中可配置所有官方屬性

自定義屬性值

Spring Boot允許我們在application.properties下自定義一些屬性,比如:

mrbird.blog.name=mrbird's blogmrbird.blog.title=Spring Boot           

複制

定義一個BlogProperties Bean,通過@Value("${屬性名}")來加載配置檔案中的屬性值:

@Componentpublic class BlogProperties {
    @Value("${mrbird.blog.name}")    private String name;
    @Value("${mrbird.blog.title}")    private String title;
    // get,set略}           

複制

編寫IndexController,注入該Bean:

@RestControllerpublic class IndexController {    @Autowired    private BlogProperties blogProperties;
    @RequestMapping("/")    String index() {        return blogProperties.getName()+"——"+blogProperties.getTitle();    }}           

複制

啟動項目,通路http://localhost:8080

在屬性非常多的情況下,也可以定義一個和配置檔案對應的Bean:

@ConfigurationProperties(prefix="mrbird.blog")public class ConfigBean {    private String name;    private String title;    // get,set略}           

複制

通過注解@ConfigurationProperties(prefix="mrbird.blog")指明了屬性的通用字首,通用字首加屬性名和配置檔案的屬性名一一對應。

除此之外還需在Spring Boot入口類加上注解@EnableConfigurationProperties({ConfigBean.class})來啟用該配置:

@SpringBootApplication@EnableConfigurationProperties({ConfigBean.class})public class Application {
    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}           

複制

之後便可在IndexController中注入該Bean,并使用了:

@RestControllerpublic class IndexController {    @Autowired    private ConfigBean configBean;
    @RequestMapping("/")    String index() {        return configBean.getName()+"——"+configBean.getTitle();    }}           

複制

屬性間的引用

在application.properties配置檔案中,各個屬性可以互相引用,如下:

mrbird.blog.name=mrbird's blogmrbird.blog.title=Spring Bootmrbird.blog.wholeTitle=${mrbird.blog.name}--${mrbird.blog.title}           

複制

自定義配置檔案

除了可以在application.properties裡配置屬性,我們還可以自定義一個配置檔案。在src/main/resources目錄下建立一個test.properties:

test.name=KangKangtest.age=25           

複制

定義一個對應該配置檔案的Bean:

@Configuration@ConfigurationProperties(prefix="test")@PropertySource("classpath:test.properties")@Componentpublic class TestConfigBean {    private String name;    private int age;    // get,set略}           

複制

注解@PropertySource("classpath:test.properties")指明了使用哪個配置檔案。要使用該配置Bean,同樣也需要在入口類裡使用注解@EnableConfigurationProperties({TestConfigBean.class})來啟用該配置。

通過指令行設定屬性值

在運作Spring Boot jar檔案時,可以使用指令java -jar xxx.jar --server.port=8081來改變端口的值。這條指令等價于我們手動到application.properties中修改(如果沒有這條屬性的話就添加)server.port屬性的值為8081。

如果不想項目的配置被指令行修改,可以在入口檔案的main方法中進行如下設定:

public static void main(String[] args) {    SpringApplication app = new SpringApplication(Application.class);    app.setAddCommandLineProperties(false);    app.run(args);}           

複制

使用xml配置

雖然Spring Boot并不推薦我們繼續使用xml配置,但如果出現不得不使用xml配置的情況,Spring Boot允許我們在入口類裡通過注解@ImportResource({"classpath:some-application.xml"})來引入xml配置檔案。

Profile配置

Profile用來針對不同的環境下使用不同的配置檔案,多環境配置檔案必須以application-{profile}.properties的格式命,其中{profile}為環境辨別。比如定義兩個配置檔案:

application-dev.properties:開發環境

server.port=8080 application-prod.properties:生産環境

server.port=8081 至于哪個具體的配置檔案會被加載,需要在application.properties檔案中通過spring.profiles.active屬性來設定,其值對應{profile}值。

如:spring.profiles.active=dev就會加載application-dev.properties配置檔案内容。可以在運作jar檔案的時候使用指令java -jar xxx.jar --spring.profiles.active={profile}切換不同的環境配置。

source code

每天

進步一點點