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

@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的值相同时,该配置文件才会生效。

spring-boot-autoconfigure”条件注入”在spring-boot-autoconfigure-XXX.jar包里,package是org.springframework.boot.autoconfigure.condition,”条件注入”常用成员还有ConditionalOnResource、ConditionalOnBean、ConditionalOnClass、ConditionalOnMissingBean、ConditionalOnMissingClass。

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

如下是@ConditionalOnProperty的javadoc

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

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

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

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

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

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

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

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) {
        ...

    }
}

Original: https://www.cnblogs.com/buguge/p/16795409.html
Author: buguge
Title: 结合springboot条件注入@ConditionalOnProperty以及@ConfigurationProperties来重构优化代码

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/685556/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球