首页 > 其他分享 >结合springboot条件注入@ConditionalOnProperty以及@ConfigurationProperties来重构优化代码

结合springboot条件注入@ConditionalOnProperty以及@ConfigurationProperties来重构优化代码

时间:2022-10-16 00:12:40浏览次数:52  
标签:springboot ConditionalOnProperty smtp +- ConfigurationProperties Value config ap

@ConditionalOnProperty实现按需注入bean

短信工具类 SmsUtil

zhenghe-common是一个基础包。

SmsUtil坐落在zhenghe-common里。先看看Smsutil的面目。

package com.emax.zhenghe.common.util;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
 
@Slf4j
@Configuration
public class SmsUtil {
    @Value("${smtp.config.username}")
    private String userName;
 
    @Value("${smtp.config.requestAddress}")
    private String requestAddress;
 
    @Value("${smtp.config.password}")
    private String passWord;
 
    @Value("${smtp.config.oem.username:oem}")
    private String oemUserName;
 
    @Value("${smtp.config.oem.password:oemsdfaaskdlkfk15673%!@4}")
    private String oemPassWord;
 
    /**
     * 发送短信
     * @param phone 手机号
     * @param msg 短信内容
     * @return
     */
    public String sendSMS(String phone, String msg) {
        StringBuilder sbParam = new StringBuilder();
        StringBuilder url = new StringBuilder();
 
        try {
            sbParam.append("?account=").append(userName);
            sbParam.append("&pwd=").append(URLEncoder.encode(passWord, "UTF-8")).append("&mphone=").append(phone).append("&content=")
                    .append(URLEncoder.encode(msg, "UTF-8"));
            url.append(requestAddress)
                    .append(sbParam.toString());
            Integer result = HttpClientUtils.requestByGetMethod(url.toString());
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("发送短信异常", e);
        }
 
        return "信息发送成功!";
    }
 
    /**
     * 发送短信
     */
    public String sendSMS(String phone, String msg,String userName,String passWord) {
        ...
    }
 
    /**
     * 发送短信
     */
    public String sendOemSMS(String phone, String msg, String smsSign) {
        StringBuilder smsSignBuilder = new StringBuilder();
        smsSignBuilder.append("【")
                .append(smsSign)
                .append("】")
                .append(msg);
        return sendSMS(phone, smsSignBuilder.toString(), oemUserName, oemPassWord);
    }
}

 

这里要说的是上面通过@Value注入的field。

存在的问题是:这要求所有依赖了zhenghe-common并扫描com.emax.zhenghe.common.util包的应用,都必须定义那些短信相关的properties配置。

需要改进的是:这些properties都以smtp.config开头,所以使用spring-beans的@Value显得啰嗦,不如使用spring-boot的@ConfigurationProperties。

 

先看问题-有的应用不涉及到收发短信,可以不用配置短信相关的这些properties。

解决办法就是spring-boot-autoconfigure的@ConditionalOnProperty注解。

@ConditionalOnProperty注解是spring-boot-autoconfigure下“条件注入”的重要成员,根据配置参数,来决定是否需要创建bean。它主要是通过自身的两个属性来控制自动配置是否生效,这两个属性分别是name、havingValue。只有当配置文件(application.properties或者bootstrap.yml)中和name相同的属性的值和注解上havingValue的值相同时,该配置文件才会生效。

敲黑板,@ConditionalOnProperty并不继承@Configuration,它只是控制bean是否生效的,所以@Configuration注解还是需要加在SmsUtil头上的。

如下是@ConditionalOnProperty的javadoc

仔细看javadoc,就能找到答案。这里,我们的改造方案是

@ConditionalOnProperty(prefix = "smtp.config",name = "requestAddress")

下面方式也行,但是不如上面的易读。

@ConditionalOnProperty( name = "smtp.config.requestAddress")

 

@Configuration是spring-context-**.jar的成员,继承@Component。

 

 

 

 

spring-context包里下面的这些注解,你一定知道。

+- context

| +- annotation

| | +- Bean

| | +- ComponentScan

| | +- Configuration

| | +- Import

| | +- Lazy

| | +- Primary

| | +- Profile

| | +- Scope

+- stereotype

| +- Service

| +-  Component

| +- Controller

| +- Repository

而 @Value @Autowired 在 spring-beans-**.jar包里(package:org.springframework.beans.factory.annotation); @Mapping @PutMapping @ResponseBody @RestController etc., 在spring-web-**.jar包里(package:org.springframework.web.bind.annotation)。

 

BTW,@Service 与 @Component 的区别

 

再说改进项:@ConfigurationProperties取代@Value

org.springframework.boot.context.properties.ConfigurationProperties在spring-boot-**.jar里。用法很easy,指定prefix即可。
需要指明的是,setter方法还是要有的。这里使用lombok的@Setter注解。
其中,oemUserName与property配置项的名称不一致,需要单独用@Value指定,并且value必须用全名 : @Value("${smtp.config.oemName:null}")。

改造后的SmsUtil

package com.emax.zhenghe.common.util;
 
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
 
@Slf4j
//↓↓ 声明bean
@Configuration(value = "smsUtil")
@ConditionalOnProperty(prefix = "smtp.config", name = "requestAddress")
//↓↓ properties配置
@ConfigurationProperties(prefix = "smtp.config")
@Setter//必须有setter才能为field赋上值
public class SmsUtil {
    private String userName;
 
    private String requestAddress;
 
    private String password;
 
    @Value("${smtp.config.oemName:null}")
    private String oemUserName;
 
    private String oemPassWord;
 
    /**
     * 发送短信
     */
    public String sendSMS(String phone, String msg) {
        ...
    }
 
    /**
     * 发送短信
     */
    public String sendSMS(String phone, String msg,String userName,String passWord) {
        ...
    }
 
    /**
     * 发送短信
     */
    public String sendOemSMS(String phone, String msg, String smsSign) {
        ...
    }
}

 

标签:springboot,ConditionalOnProperty,smtp,+-,ConfigurationProperties,Value,config,ap
From: https://www.cnblogs.com/buguge/p/16795409.html

相关文章

  • SpringBoot Logback统一日志处理
    一、日志 1、配置日志级别日志记录器(Logger)的行为是分等级的。如下表所示:分为:OFF、FATAL、ERROR、WARN、INFO、DEBUG、ALL默认情况下,springboot从控制台打印出来的......
  • Docker部署springboot项目
    建立Dockerfile文件FROMjava:8       基于jdk创建VOLUME/tmp      创建临时文件目录ADDch3-boot.jarch3-boot.jar      复制......
  • springboot如何处理矩阵参数类型的url
    矩阵参数类型的url如何处理首先要开启这个功能在webconfig类中创建Webconfigurer类并且设置urlPathHelper类中的removeSemicolonContent为false@BeanpublicWe......
  • springboot导出数据到Excel表格,使用EasyExcel
    1.导入依赖导出方法需要使用到fastJson的依赖,这里也直接导入点击查看代码<!--阿里的easyexcel--><dependency><groupId>com.alibaba</groupId>......
  • 微信直接登录(springboot+wx)
    方法1源码地址快速开始配置文件编写weixin:appid:wx5bbcac13dbec9fe0secret:a1ed9ae66689bbaf50b0e04630ec1472业务的编写,前端传来一个code,发送到......
  • SpringBoot之Logback 日志文件配置
    Logback概述Logback是由log4j的创始人设计的另一个开源的日志组件,官方网站是:http://logback.qos.ch。它当前分为下面下个模块:logback-core:其他两个模块的基础模块l......
  • springboot MP代码生成器
    1、需要的依赖和版本号(我这个是项目完成后的全部依赖,只参照需要的依赖即可)<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0......
  • springboot整合easyExcel实现不固定列导入
    1、pom.xml文件引入easyExcel<!--阿里开源easyExcel依赖--><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><versio......
  • SpringBoot整合
    1、JedisPoolConfig(这个是配置连接池)2、RedisConnectionFactory这个是配置连接信息,这里的RedisConnectionFactory是一个接口,我们需要使用它的实现类,在SpringData......
  • 基于springboot高速公路收费管理系统的设计设计与实现-计算机毕业设计源码+LW文档
    摘  要通篇文章的撰写基础是实际的应用需要,然后在架构系统之前全面复习大学所修习的相关知识以及网络提供的技术应用教程,以高速公路收费管理的实际应用需要出发,架构系......