starter:
? 1、這個場景需要使用到的依賴是什么?
? 2、如何編寫自動配置
```java
@Configuration //指定這個類是一個配置類
@ConditionalOnXXX //在指定條件成立的情況下自動配置類生效
@AutoConfigureAfter //指定自動配置類的順序
@Bean //給容器中添加組件
@ConfigurationPropertie結合相關xxxProperties類來綁定相關的配置
@EnableConfigurationProperties //讓xxxProperties生效加入到容器中
自動配置類要能加載
將需要啟動就加載的自動配置類,配置在META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.boot.autoconfigure.admin.SpringApplicationAdminJmxAutoConfiguration,\
org.springframework.boot.autoconfigure.aop.AopAutoConfiguration,\
```
? 3、模式:
啟動器只用來做依賴導入;
專門來寫一個自動配置模塊;
啟動器依賴自動配置;別人只需要引入啟動器(starter)
mybatis-spring-boot-starter;自定義啟動器名-spring-boot-starter
步驟:
1)、啟動器模塊
```xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atguigu.starter</groupId>
<artifactId>atguigu-spring-boot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
<!--啟動器-->
<dependencies>
<!--引入自動配置模塊-->
<dependency>
<groupId>com.atguigu.starter</groupId>
<artifactId>atguigu-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
```
2)、自動配置模塊
```xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.atguigu.starter</groupId>
<artifactId>atguigu-spring-boot-starter-autoconfigurer</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>atguigu-spring-boot-starter-autoconfigurer</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!--引入spring-boot-starter;所有starter的基本配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
</project>
```
```java
package com.atguigu.starter;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "atguigu.hello")
public class HelloProperties {
private String prefix;
private String suffix;
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
public String getSuffix() {
return suffix;
}
public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
```
```java
package com.atguigu.starter;
public class HelloService {
HelloProperties helloProperties;
public HelloProperties getHelloProperties() {
return helloProperties;
}
public void setHelloProperties(HelloProperties helloProperties) {
this.helloProperties = helloProperties;
}
public String sayHellAtguigu(String name){
return helloProperties.getPrefix()+"-" +name + helloProperties.getSuffix();
}
}
```
```java
package com.atguigu.starter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConditionalOnWebApplication //web應用才生效
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
@Autowired
HelloProperties helloProperties;
@Bean
public HelloService helloService(){
HelloService service = new HelloService();
service.setHelloProperties(helloProperties);
return service;
}
}
```
- Spring Boot 入門
- Spring Boot 簡介
- 微服務
- 環境準備
- MAVEN設置
- IDEA設置
- Spring Boot HelloWorld
- 創建一個maven工程;(jar)
- 導入spring boot相關的依賴
- 編寫一個主程序;啟動Spring Boot應用
- 編寫相關的Controller、Service
- 運行主程序測試
- 簡化部署
- Hello World探究
- POM文件
- 父項目
- 啟動器
- 主程序類,主入口類
- 使用Spring Initializer
- IDEA使用 Spring Initializer
- STS使用 Spring Starter Project快速創建項目
- 配置文件
- 配置文件
- YAML語法
- 基本語法
- 值的寫法
- 普通的值(數字,字符串,布爾)
- 對象、Map(屬性和值)(鍵值對)
- 數組(List、Set)
- 配置文件值注入
- 其他問題
- properties配置文件在idea中默認utf-8可能會亂碼
- @Value獲取值和@ConfigurationProperties獲取值比較
- 配置文件注入值數據校驗
- @PropertySource&@ImportResource&@Bean
- 配置文件占位符
- 隨機數
- 占位符獲取之前配置的值
- Profile
- 多Profile文件
- yml支持多文檔塊方式
- 激活指定profile
- 配置文件加載位置
- 外部配置加載順序
- 自動配置原理
- 自動配置原理
- 細節
- @Conditional派生注解(Spring注解版原生的@Conditional作用)
- 日志
- 日志框架
- SLF4j使用
- 如何在系統中使用SLF4j
- 遺留問題
- SpringBoot日志關系
- 日志使用
- 默認配置
- 指定配置
- 切換日志框架
- Web開發
- 簡介
- SpringBoot對靜態資源的映射規則
- 模板引擎
- 引入thymeleaf
- Thymeleaf使用
- 語法規則
- SpringMVC自動配置
- Spring MVC auto-configuration
- 擴展SpringMVC
- 全面接管SpringMVC
- 如何修改SpringBoot的默認配置
- RestfulCRUD
- 默認訪問首頁
- 國際化
- 登陸
- 攔截器進行登陸檢查
- CRUD-員工列表
- thymeleaf公共頁面元素抽取
- CRUD-員工添加
- CRUD-員工修改
- CRUD-員工刪除
- 錯誤處理機制
- SpringBoot默認的錯誤處理機制
- 如果定制錯誤響應
- 如何定制錯誤的頁面
- 如何定制錯誤的json數據
- 將我們的定制數據攜帶出去
- 配置嵌入式Servlet容器
- 如何定制和修改Servlet容器的相關配置
- 注冊Servlet三大組件【Servlet、Filter、Listener】
- 替換為其他嵌入式Servlet容器
- 嵌入式Servlet容器自動配置原理
- 嵌入式Servlet容器啟動原理
- 使用外置的Servlet容器
- 步驟
- 原理
- Docker
- 簡介
- 核心概念
- 安裝Docker
- 安裝linux虛擬機
- 在linux虛擬機上安裝docker
- Docker常用命令&操作
- 鏡像操作
- 容器操作
- 安裝MySQL示例
- SpringBoot與數據訪問
- JDBC
- 整合Druid數據源
- 整合MyBatis
- 注解版
- 配置文件版
- 整合SpringData JPA
- SpringData簡介
- 整合SpringData JPA
- 啟動配置原理
- 創建SpringApplication對象
- 運行run方法
- 事件監聽機制
- 自定義starter