金三银四旗开得胜!《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)
相关文章
相关标签/搜索