Factory model - enterprise micro-channel application configuration code optimization

A. Why use the factory model to optimize the code

  Existing enterprise applications, micro-channel, Id plurality of different application configurations, a variety of different applications require customized configurations, such as a micro channel in an enterprise push message is usually used to read @ComfigurationProperties profile configuration, and then using different injection timings related to tasks @AutoWired configuration class, there is such a drawback, configuration, and timing of the different tasks of the coupling is too high, a plurality of applications arranged based well management, the use of different configuration factory to produce configuration, to reduce the coupling between the configuration and the timing task, different configurations manageable;

 

II. Before optimized code

@Data 
@Component 
@ConfigurationProperties (prefix = "wxapp1" )
 public  class WxApp1 { 

    / ** Enterprise Id * / 
    Private String corpid; 

    / ** Enterprise key * / 
    Private String corpsecret; 

    / ** enterprise application ID * / 
    Private Integer agentId which; 

    / ** message push URL * / 
    Private String sendMessageUrl; 
}
@Data 
@Component 
@ConfigurationProperties (prefix = "wxapp2" )
 public  class WxApp2 { 

    / ** Enterprise Id * / 
    Private String corpid; 

    / ** Enterprise key * / 
    Private String corpsecret; 

    / ** enterprise application ID * / 
    Private Integer agentId which; 

    / ** message push URL * / 
    Private String sendMessageUrl; 
}

Push relevant news

@Slf4j
@Component
public class WxMessageTask {

    @Autowired
    private WxApp1 app1;

    @Scheduled(cron = "0 0 10 1/1 * ?")
    public void pushMessage(){
        app1.xxx
        .....
        .....
    }    

III. After the code optimization

Enterprise micro-channel application enumeration class

@Getter 
@AllArgsConstructor 
public  enum WxAppEnum { 

    CORP_DAILY ( . 1, "the application. 1" ), 

    WG_DATA ( 2, "Application 2" ); 

    / ** enterprise micro channel codes * / 
    Private  int code;
     / ** enterprise applications micro channel * / 
    Private String wxAppName; 
}

Enterprise micro-channel configuration enumerate configName to inject the Spring bean name

@Getter
@AllArgsConstructor
public enum  WxAppConfigEnum {

    WG_DATA_CONFIG(WxAppEnum.WG_DATA,"wgDataWechatConfig"),
    CORP_DAILY_CONFIG(WxAppEnum.CORP_DAILY,"corpWechatConfig");

    private WxAppEnum wxAppEnum;
    private String configName;

    /**
     * 获取微信应用配置枚举
     * @param valueName
     * @return
     */
    public static WxAppConfigEnum valueOfConfigName(String valueName){
        return Stream.of(values()).filter(x->x.getConfigName().equals(valueName))
                .findFirst()
                .orElse(null ); 
    } 

    / ** 
     * acquires an enumeration of micro channel 
     * @param valueName 
     * @return 
     * / 
    public  static WxAppEnum getWxAppEnum (String valueName) { 
        Optional The <WxAppConfigEnum> = optional Optional.ofNullable (valueOfConfigName (valueName));
         return optional. ? isPresent () optional.get () getWxAppEnum ():. null ; 
    } 
}

Factory configuration

@Data
@Component
public class WxAppConfigFactory {

    public static final Map<WxAppEnum,IWxAppBaseConfig> configMap =new HashMap<>();

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 初始化微信应用配置
     */
    @PostConstruct
    public void init(){
        applicationContext.getBeansOfType(IWxAppBaseConfig.class)
                .entrySet()
                .stream()
                .filter(x-> !ObjectUtil.isNull(WxAppConfigEnum.valueOfConfigName(x.getKey())))
                .forEach(x->configMap.put(WxAppConfigEnum.getWxAppEnum(x.getKey()),x.getValue()));
    }

    /**
     * 获取配置
     * @param configValue
     * @return
     */
    public static IWxAppBaseConfig getConfig(WxAppEnum configValue){
        return configMap.get(configValue);
    }
}

 

Guess you like

Origin www.cnblogs.com/gabriel-y/p/12311866.html