Spring Boot基本使用

Spring Boot 精要

1, 自动配置
自动配置需要的bean
2, 起步依赖
指定所需要的功能,引入需要的包(包括版本,经过测试,放心使用)
3, 命令行界面
Spring Boot CLI 自动检测使用了哪些类,知道要向Class path添加哪些起步依赖
4, Actuator
提供在运行时检视应用程序内部情况的能力:

  • Spring上下文配置的Bean
  • Spring Boot 自动配置做的决策
  • 应用程序取到的环境变量,系统属性,配置属性,和命令行参数
  • 应用程序线程的当前状态
  • 应用程序最近处理过的HTTP请求的追踪情况
  • 各种和内存用量,垃圾回收,Web请求以及数据源用量相关的指标

Spring Boot 条件化配置

加入Spring Boot 时,会加入 spirng-boot-autoconfigure 的jar文件,其中包含很多配置类,利用spring的条件化配置选择是否自动配置:

编写自己的条件:

实现Condition接口,覆盖matches() 方法

package com.luty.serviceCenter;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class JdbcTemplateCondition implements Condition {

    @Override
    public boolean matches(ConditionContext Context, AnnotatedTypeMetadata arg1) {
        try {
            Context.getClassLoader().loadClass("org.springframework.jdbc.core.JdbcTemplate");
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}

使用条件话配置化注解

条件化注解 配置生效条件
@ConditionOnBean 配置了某个特定Bean

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容