SpringBoot 高级特性

前言

  • 在实际开发中,我们有多种环境,来回切换会很麻烦,为了方便开发,使用Profile来进行对环境的切换
  • 使用过SpringBoot的同学都知道我们的SpringBoot项目由一个个start组成,SpringBoot项目存在多种以starter结尾的依赖
    比较常见的有spring-boot-starter-webmybatis-spring-boot-starter等,我们也可以根据自己的需要自定义新的starter

starter有什么作用呢?

在我们的日常开发工作中,经常会有一些独立于业务之外的配置模块,我们经常将其放到一个特定的包下,然后如果另一个工程需要复用这块功能的时候,需要将代码硬拷贝到另一个工程,重新集成一遍,麻烦至极。如果我们将这些可独立于业务代码之外的功配置模块封装成一个个starter,复用的时候只需要将其在pom中引用依赖即可,Spring Boot为我们完成自动装配,简直不要太爽。
原文链接:https://blog.csdn.net/o9109003234/article/details/106168843

Profile环境切换

概念

概念:为了方便多环境适配,简化Profile功能

功能

为了方便我们使用**@value**注解可以取得配置文件中的值,冒号 “:" 后的值为默认值

@RestController
public class HelloController {

    @Value("${person.name:李四}")
    private String name;

    @GetMapping("/")
    public String name() {

        return "hello" + name;
    }
}

这里列举出三种开发环境,其中application后不带后缀的配置文件为默认配置文件,永远都会执行加载

  • local:本地环境
  • pro: 生产环境
  • te: 测试环境

在这里插入图片描述
想要切换对应环境时只需在profilesactive中设置即可
注意:默认配置文件与环境配置文件同时生效,在二者之中同名配置项相同的情况下profile的配置优先
在这里插入图片描述

外部化配置

配置信息不仅可以从配置文件中获取,还可以通过下面几种常用方式获取

  • Java属性文件
  • Yaml文件
  • 环境变量
  • 命令行参数
    命令行参数方式可通过打包配置文件,在打包文件下运行cmd命令行
    执行 Java -jar xxx.jar --spring.profiles.active=xxx 命令来指定执行哪个开发环境的jar包,比第一种直接执行配置文件激活的方式更加灵活,可在执行最后阶段修改想要部署到服务器中的环境,且可以修改配置文件的任意值,命令行优先
    在这里插入图片描述

这里我们取系统中Maven的环境变量和此电脑的操作系统名称,可以看到能够返回Maven安装地址和操作系统名称
如下图所示

    @Value("${person.name:李四}")
    private String name;

    @GetMapping("/")
    public String name() {

        return "hello" + name;
    }

    @Value("${MAVEN_HOME}")
    private String msg;

    @Value("${os.name}")
    private String osName;

    @GetMapping("/getMsg")
    public String getMsg() {

        return msg + "......" + osName;
    }

在这里插入图片描述

SpringBoot自定义Starter

首先创建两个项目,一个为空项目,一个为SpringBoot项目,名称随意填写
在这里插入图片描述
然后以本项目为例:在chy-hello-spring-boot-starter pom 中引入 chy-hello-spring-boot-starter-autoconfigure

    <dependencies>
        <dependency>
            <groupId>com.chy</groupId>
            <artifactId>chy-hello-spring-boot-starter-autoconfigure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

接下来编写HelloService方法

public class HelloService {

    @Autowired
    HelloProperties helloProperties;

    public String sayHello(String name) {

        return helloProperties.getPrefix()  + name + "." + helloProperties.getSuffix();
    }

}

这里的getPrefixgetSuffixHelloProperties中的变量,可以看到我们注入了HelloProperties

@ConfigurationProperties("chy.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;
    }
}

通过@ConfigurationProperties 可以将 prefixsuffix与配置文件中的值绑定
接下来我们就可以编写自动配置类了

@Configuration
@ConditionalOnMissingBean(HelloService.class)
@EnableConfigurationProperties(HelloProperties.class)
public class HelloServiceAutoConfiguration {
    
    @Bean
    public HelloService helloService() {
        HelloService helloService = new HelloService();
        return helloService;
    }

}

@ConditionalOnMissingBean : 如果容器中没有配置 HelloService 就导入HelloService组件@EnableConfigurationProperties : 导入HelloProperties

这些步骤做完之后,就可以将两个项目打包,安装到本地Maven仓库了
在这里插入图片描述
这个时候就可以创建新项目测试包的功能了
需要创建一个SpringBoot项目,勾选Web开发场景,创建项目
项目创建好之后就可以导入文章上面的依赖了
因为SpringBoot实现项目启动自动加载配置类使用的是autoconfigure包下META-INF/spring.factories中的EnableAutoConfiguration的值。
这时我们就可以去到autoconfigure包将META-INF/spring.factories复制出 将要导入的包名改为本项目引入的配置包,如图所示
在这里插入图片描述
找到 # Auto Configure 复制两行既可,替换成本项目要导入的自动配置类

# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.chy.hello.service.auto.HelloServiceAutoConfiguration

再次执行clean,install操作,并在测试项目中重新导入依赖
接下来就可以编写Controller类实现效果了
注:prefixsuffix需要在配置文件中配置

@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String testAuto() {
        String s = helloService.sayHello("陈宇");
        return s;
    }
}

applicaton.properties中代码

chy.hello.prefix=nihao
chy.hello.suffix=chy

最终结果如图所示
在这里插入图片描述
如果想尝试自己往容器中注入组件,也可以编写配置类实现

@Configuration
public class MyConfig {

    public HelloService helloService() {
        HelloService helloService = new HelloService();
        return helloService;
    }
}

打断点观察程序执行流程,可以发现执行的是MyConfig中的HelloService方法-原因@ConditionalOnMissingBean注解

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值