歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Spring Boot入門學習筆記

Spring Boot入門學習筆記

日期:2017/3/1 9:11:15   编辑:Linux編程

1. 如果你的配置文件寫在根目錄,則使用@ComponentScan不需要任何參數,所有配置的組件(@Component,@Service, @Repository, @Controller 等)都會自動注冊為Spring Beans。

2. @SpringBootApplication注解等價於使用@Configuration,@EnableAutoConfiguration和@ComponentScan的默認屬性(也可以進行自定義屬性):

package com.example.myproject;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

3. 如果你要注冊listener,不管application的創建方式,可以添加META-INF/spring.factories文件到你的項目,並使用org.springframework.context.ApplicationListerner鍵。
例:

org.springframework.context.ApplicationListener=com.example.project.MyListener

4. 如果你想在SpringApplication啟動後運行一些代碼,可以實現ApplicationRunner或CommandLineRunner接口,這兩個接口提供一個run方法,此方法會在SpringApplication.run(...)完成前調用。

5. SpringApplication將在以下位置讀取application.properties屬性文件。

* 當前目錄下的/config子目錄
* 當前目錄
* classpath下的/config
* 根目錄

6. 靜態內容默認的位置:/static , /public , /resources , /META-INF/resources

7. 模板引擎文件默認的位置:src/main/resources/templates(即根目錄下的templates)

8. 默認錯誤界面:如果界面為靜態界面,則文件名為對應的錯誤狀態碼,如404.html,將其放於靜態目錄下的error目錄下。如果為動態頁面,則放於動態界面目錄下的error目錄下,名字為錯誤狀態碼。

9. 自定義嵌入式servlet容器的配置:
Network setting: server.port,
Session setting: server.session.persistence
server.session.timeout
server.session.cookie.*
如果要通過編程方式配置,可以繼承EmbeddedServletContainerCustomizer接口

import org.springframework.boot.context.embedded.*;
import org.springframework.stereotype.Component;

@Component
public class CustomizationBean implements EmbeddedServletContainerCustomizer {
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.setPort(9000);
}
}

Spring Boot+Nginx+Tomcat+SSL配置筆記 http://www.linuxidc.com/Linux/2016-01/127134.htm

Spring Boot 的詳細介紹:請點這裡
Spring Boot 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved