Spring Boot 特性:

1.能够快速创建基于Spring的应用程序;
2.能供直接使用java main 方法启动内置的Tomcat或者Jetty服务器运行Spring Boot程序;
3.提供约定的starter POM来简化Maven的配置,让Maven的配置变得更简单;
4.根据项目的Maven依赖配置,Spring Boot自动配置Spring、Spring MVC等等;
5.提供了程序的健康监控等功能;
6.基本可以完全不使用XML配置文件,采用注解进行配置。

Spring Boot四大核心:

1.自动配置:针对很多Spring应用程序和常用的应用功能,Spring Boot能自动提供相关配置;
2.starter 组件:starter 是 SpringBoot 的一个重要的组成部分,它相当于一个集成的模块;
3.Actuator:让你能深入运行中的Spring Boot应用程序,监控程序的内部信息;
4.命令行界面:这个为Spring Boot的可选特性,主要是针对Groovy的。

springboot starter 组件

starter 是 SpringBoot 的一个重要的组成部分,它相当于一个集成的模块,比如你想用 Mybatis 和 lombok,但是在 pom 文件中需要写两个依赖,如果你将他们集成为一个 starter(或者将更多你需要的依赖集成进去),那么你只需要在 pom 文件中写一个 starter 依赖就可以了,这对于一个可复用模块的开发和维护都极为有利。

同时,在 maven 中引入 starter 依赖之后,SpringBoot 就能自动扫描到要加载的信息并启动相应的默认配置,它遵循“约定大于配置”的理念。

项目结构

Spring 官方建议自定义的 starter 使用 xxx-spring-boot-starter 命名规则,以区分 SpringBoot 生态提供的 starter。

spring boot有哪些组件 spring boot的组件_java

在 pom 文件中加入实现 starter 所需要的依赖。
<!-- 编写 starter 组件必要的依赖-->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter</artifactId>
     <version>2.1.6.RELEASE</version>
     <!-- 设置依赖传递 为  不传递 -->
     <optional>true</optional>
 </dependency>
 <!-- application.yaml 配置文件的自动提示需要的依赖 -->
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-configuration-processor</artifactId>
     <version>2.1.6.RELEASE</version>
 </dependency>
 <!-- Redis 操作客户端 测试工程使用 -->
 <dependency>
     <groupId>org.redisson</groupId>
     <artifactId>redisson</artifactId>
     <version>3.13.1</version>
 </dependency>
创建配置信息实体类

application.yaml 配置文件自动提示 的配置信息实体类
需要提供 get set 我这里没有提供

import org.springframework.boot.context.properties.ConfigurationProperties;
// @ConfigurationProperties(prefix = "gp.daozhuang") 注解的作用是将相同前缀的配置信息通过配置项名称映射成实体类。
@ConfigurationProperties(prefix = "gp.daozhuang")
public class RedissonProperties {
	// 需要提供  get  set  我这里没有提供
    private String host = "localhost";
    private int port = 6379;
    private int timeout; //超时时间
    private boolean ssl;
}

编写 META-INF 文件下 文件: additional-spring-configuration-metadata.json

{
  "properties": [
    {
      "name": "gp.daozhuang.host",
      "type": "java.lang.String",
      "description": "redis的服务器地址",
      "defaultValue": "localhost"
    },{
      "name": "gp.daozhuang.port",
      "type": "java.lang.Integer",
      "description": "redis服务器的端口",
      "defaultValue": 6379
    }
  ]
}
创建配置类
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: DaoZhuang
 * @Date: 2020/7/17
 */
 // 使使用 @ConfigurationProperties 注解的类生效。 在上一步中使用到了
@EnableConfigurationProperties(RedissonProperties.class)
//@ConditionalOnProperty
@Configuration
public class RedisConfiguration {
    @Bean
    public RedisClient redisClient(RedissonProperties redissonProperties){
        return new RedisClient(redissonProperties.getHost(),redissonProperties.getPort());
    }
}
@Conditional 注解来控制Configuration是否生效

@Conditional是Spring4新提供的注解,它的作用是按照一定的条件进行判断,满足条件给容器注册bean。

以下是所用的 Conditional 注解 用以判断条件 本次工程没有用到

spring boot有哪些组件 spring boot的组件_spring_02

编写具体实现类
import org.redisson.Redisson;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
/**
 * @Author: DaoZhuang
 * @Date: 2020/7/17
 */
public class RedisClient {
    private String host;
    private Integer port;
    
    public RedisClient(String host,Integer port){
        this.host = host;
        this.port = port;
    }

    public String redisCli(){
        Config config=new Config();
        config.useSingleServer().
                setAddress("redis://"+ host +":" + port).
                setConnectTimeout(10000);
        RedissonClient redissonClient = Redisson.create(config);
        RList<Object> nameList = redissonClient.getList("nameList");
        nameList.add("ddd");
        
        nameList.forEach(System.out::println);
        
        redissonClient.shutdown();
        return "";
    }
}
在测试工程 springautobeandemo 中 测试

在pom文件中引入上边的工程
注意 引入的依赖

/**
 * @Author: DaoZhuang
 * @Date: 2020/7/17
 */
@RestController("")
@RequestMapping
public class RedisTest {
	// 直接注入  自己编写的实现类  直接使用  会有默认的配置
    @Autowired
    private RedisClient redisClient;

    @GetMapping("index")
    public String index(){
        return redisClient.redisCli();
    }

}

在 application.yaml 文件中 配置自己定义的 配置项

gp:
  daozhuang:
    host:
    port:
    ssl:
    timeout: