首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Spring Boot 核心筆記:自動配置、啟動器、屬性文件、靜態資源、模板引擎、錯誤頁面、嵌入式容器定制

     編輯:Java 綜合教學

一、核心註解:@SpringBootApplication

@SpringBootApplication
// 等價於組合註解:
// @Configuration        // 當前類為配置類
// @EnableAutoConfiguration // 啟用自動配置(核心魔法)
// @ComponentScan         // 掃描當前包及子包下的 @Component/@Service/@Repository/@Controller

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

二、組件掃描默認規則

  • 主類所在包及其所有子包會被掃描。
  • 標註 @Component、@Service、@Repository、@Controller、@Configuration 的類自動註冊為 Bean。
  • 可自定義:@ComponentScan(basePackages = "com.example")。

三、自定義 Listener 註冊

在 src/main/resources/META-INF/spring.factories 添加:

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

Spring 啟動時自動讀取並註冊,無需代碼配置。

四、啟動後執行邏輯:CommandLineRunner / ApplicationRunner

@Component
public class StartupRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        System.out.println("應用啟動完成,參數:" + Arrays.toString(args));
        // 初始化緩存、啟動定時任務、發送啟動通知等
    }
}
  • CommandLineRunner:接收 String[] args
  • ApplicationRunner:接收 ApplicationArguments(更豐富的參數解析)
  • 多個 Runner 可通過 @Order 排序

五、配置文件加載優先級

Spring Boot 按以下順序加載 application.properties/yml,後者覆蓋前者:

  1. 當前目錄下的 /config 子目錄
  2. 當前目錄
  3. classpath 下的 /config
  4. classpath 根目錄

支持 Profile 區分:application-dev.properties、application-prod.yml,啟動時 --spring.profiles.active=dev。

六、靜態資源映射默認路徑

請求 /xxx 時,按順序查找:

  1. classpath:/static/
  2. classpath:/public/
  3. classpath:/resources/
  4. classpath:/META-INF/resources/

可配置:spring.mvc.static-path-pattern=/assets/**、spring.resources.static-locations=classpath:/custom/。

七、模板引擎文件默認位置

  • Thymeleaf/FreeMarker/Velocity 等:src/main/resources/templates/
  • 控制器返回視圖名 hello → 解析為 templates/hello.html

八、錯誤頁面定制

優先匹配精確狀態碼,再匹配 5xx、4xx,最後 error。

九、嵌入式 Servlet 容器定制

9.1 屬性配置方式

server.port=9000
server.session.timeout=30m
server.session.persistence=true
server.session.cookie.http-only=true
server.session.cookie.secure=true

9.2 編程方式:EmbeddedServletContainerCustomizer

@Component
public class CustomContainerConfig implements EmbeddedServletContainerCustomizer {
    @Override
    public void customize(ConfigurableEmbeddedServletContainer container) {
        container.setPort(9000);
        container.setSessionTimeout(30, TimeUnit.MINUTES);
        // 可設置 SSL、壓縮、錯誤頁面、JSP 支持等
    }
}

Spring Boot 2.0+ 遷移為 WebServerFactoryCustomizer<ConfigurableServletWebServerFactory>。

十、總結:Spring Boot 核心價值

  1. 約定優於配置:零 XML、自動配置、開箱即用。
  2. 啟動器依賴:spring-boot-starter-web 等聚合常用依賴,版本兼容性保證。
  3. 外部化配置:多環境、多來源、類型安全綁定(@ConfigurationProperties)。
  4. 生產級監控:Actuator 提供健康檢查、指標、信息端點。
  5. 可執行 JAR:內嵌容器,java -jar 即運行,容器化部署友好。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved
類型靜態頁面動態模板
位置static/error/templates/error/
命名404.html, 5xx.html404.ftl, 5xx.ftl
通用error.htmlerror.ftl