Factory object and object singleton under springboot

Keywords: SpringBoot Spring

The directory structure is as follows

Interface class

public interface EtkClient {

     static EtkClient getInstance(){
         return null;
     }

    String getToken();
    String getTokenKey();
    String getUrl();
    String getReturnMode();
    String getShipway();
    String getType();

}

Factory class

@Component
public class EtkFactory {


    public static EtkClient getEtkByName(String specificName){
        switch (specificName){
            case "ETK" : return EtkCurrencyVo.getInstance();
            case "ETK-AM" : return EtkAmVo.getInstance();
            case "OMALL" : return EtkOmallVo.getInstance();
        }
        throw new RuntimeException("Configuration file error, please check the configuration file!");
    }
}

object

package com.ecms.cbc.etkFactory;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@Configuration
@PropertySource("classpath:application-etk.properties")
@ConfigurationProperties(prefix = "etk.am")
public class EtkAmVo implements EtkClient{

    private static  String token;
    private static String tokenKey;
    private static String type;
    private static String url;
    private static String returnMode;
    private static String shipway;
    public static EtkAmVo etkAmVo ;

    public static synchronized EtkClient getInstance() {
        if(etkAmVo == null){
            etkAmVo = new EtkAmVo( token,  tokenKey,  type,  url,  returnMode,  shipway);
        }
        return etkAmVo;
    }

    public EtkAmVo() {
    }

    public EtkAmVo(String token, String tokenKey, String type, String url, String returnMode, String shipway) {
        EtkAmVo.token = token;
        EtkAmVo.tokenKey = tokenKey;
        EtkAmVo.type = type;
        EtkAmVo.url = url;
        EtkAmVo.returnMode = returnMode;
        EtkAmVo.shipway = shipway;
    }

    @Override
    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        System.out.println("++++++++++++++++++++++++++++++++++++"+token);
        EtkAmVo.token = token;
    }

    @Override
    public String getTokenKey() {
        return tokenKey;
    }

    public void setTokenKey(String tokenKey) {
        EtkAmVo.tokenKey = tokenKey;
    }

    @Override
    public String getType() {
        return type;
    }

    public void setType(String type) {
        EtkAmVo.type = type;
    }

    @Override
    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        EtkAmVo.url = url;
    }

    @Override
    public String getReturnMode() {
        return returnMode;
    }

    public void setReturnMode(String returnMode) {
        EtkAmVo.returnMode = returnMode;
    }

    @Override
    public String getShipway() {
        return shipway;
    }

    public void setShipway(String shipway) {
        EtkAmVo.shipway = shipway;
    }
}

Springboot supports external configuration files, so business configuration and program configuration can be introduced separately according to business needs. However, the default configuration file format name of springboot is application-xxx.properties, and the file location is under resources folder.

The program configuration of multi module project is used in application.properties

spring.profiles.active=web,service

Business configuration usage class usage

spring.profiles.include=xxx

In this way, the structure of the configuration file can be kept as clear as possible

 

Posted by gdhanasekar on Wed, 27 Nov 2019 08:01:15 -0800