金三銀四旗開得勝!《SpringCloud超級入門(1)

目錄java

Spring Boot Starter項目建立git

自動建立客戶端web

使用 Starter面試

使用註解開啓 Starter 自動構建redis

使用配置開啓 Starter 自動構建spring

配置 Starter 內容提示sql

Spring Boot 的便利性體如今,它簡化了不少煩瑣的配置,這對於開發人員來講是一個福音,經過引入各類 Spring Boot Starter 包能夠快速搭建出一個項目的腳手架。mongodb

目前提供的 Spring Boot Starter 包有:springboot

  • spring-boot-starter-web:快速構建基於 Spring mvc?的 Web 項目,使用 Tomcat 作默認嵌入式容器。mybatis

  • spring-boot-starter-data-redis:操做 Redis。

  • spring-boot-starter-data-mongodb:操做 Mongodb。

  • spring-boot-starter-data-jpa:操做 Mysql。

  • spring-boot-starter-activemq:操做 Activemq。

  • ……

自動配置很是方便,當咱們要操做 Mongodb 的時候,只須要引入 spring-boot-starter-data-mongodb 的依賴,而後配置 Mongodb 的連接信息 spring.data.mongodb.uri=mongodb://localhost/test 就可使用 MongoTemplate 來操做數據,MongoTemplate 的初始化工做所有交給 Starter 來完成。

自動配置麻煩的是當出現錯誤時,排查問題的難度上升了。自動配置的邏輯都在 Spring Boot Starter 中,要想快速定位問題,就必須得了解 Spring Boot Starter 的內部原理。接下來咱們本身動手來實現一個 Spring Boot Starter。

Spring Boot Starter項目建立


建立一個項目 spring-boot-starter-demo,pom.xml 配置代碼以下所示。

<dependencies>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

    <dependency>

        <groupId>org.projectlombok</groupId>

        <artifactId>lombok</artifactId>

        <optional>true</optional>

    </dependency>

</dependencies>

建立一個配置類,用於在屬性文件中配置值,至關於 spring.data.mongo 這種形式,代碼以下所示。

import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.Data;

@Data

@ConfigurationProperties("spring.user")

public class UserPorperties {

    private String name;

}

再定義一個 Client,至關於 MongoTemplate,裏面定一個方法,用於獲取配置中的值,代碼以下所示。

public class UserClient {

    private UserPorperties userPorperties;

    public UserClient() {

    }

    public UserClient(UserPorperties p) {

        this.userPorperties = p;

    }

    public String getName() {

        return userPorperties.getName();

    }

}

自動建立客戶端


一個最基本的 Starter 包定義好了,但目前確定是不能使用 UserClient,由於咱們沒有自動構建 UserClient 的實例。接下來開始構建 UserClient,代碼以下所示。

@Configuration

@EnableConfigurationProperties(UserPorperties.class)

public class UserAutoConfigure {

    @Bean

    @ConditionalOnProperty(prefix = "spring.user", value = "enabled", havingValue = "true")

    public UserClient userClient(UserPorperties userPorperties) {

        return new UserClient(userPorperties);

    }

}

Spring Boot 會默認掃描跟啓動類平級的包,假如咱們的 Starter 跟啓動類不在同一個主包下,如何能讓 UserAutoConfigure 生效?

在 resources 下建立一個 META-INF 文件夾,而後在 META-INF 文件夾中建立一個 spring.factories 文件,文件中指定自動配置的類:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

com.cxytiandi.demo.UserAutoConfigure

Spring Boot 啓動時會去讀取 spring.factories 文件,而後根據配置激活對應的配置類,至此一個簡單的 Starter 包就實現了。

使用 Starter


如今能夠在其餘的項目中引入這個 Starter 包,代碼以下所示。

<dependency>

<groupId>com.cxytiandi</groupId>

<artifactId>spring-boot-starter-demo</artifactId>

<version>0.0.1-SNAPSHOT</version>

</dependency>

引入以後就直接可使用 UserClient,UserClient 在項目啓動的時候已經自動初始化好,代碼以下所示。

@RestController

public class UserController {



@Autowired

private UserClient userClient;



@GetMapping("/user/name")

public String getUserName() {

return userClient.getName();

}

}

屬性文件中配置 name 的值和開啓 UserClient:

spring.user.name=zhangsan



# 面試題總結

**[面試文件獲取方式:戳這裏免費下載(助你面試無憂)](https://gitee.com/vip204888/java-p7)**

**其它面試題(springboot、mybatis、併發、java中高級面試總結等)**

![](https://upload-images.jianshu.io/upload_images/22932333-3db05687eb281f7e?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![](https://upload-images.jianshu.io/upload_images/22932333-49fccfe54f90c9de?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![](https://upload-images.jianshu.io/upload_images/22932333-ac5bc139f5195399?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
相關文章
相關標籤/搜索