SpringMvc多环境配置

一. 加入文件夹与自己相关的配置文件到resources目录下

resources
  config_common
  config_dev
  config_prod
    configprd.properties
  config_test
    configtest.properties
configtest.properties 文件下示例值
  config.publicKey=MIGfMA0G
  config.privatekey=MIICdwI

二. spring-context.xml加入配置文件

	<!-- 测试环境配置文件 -->
	<beans profile="test">
		<context:property-placeholder location="classpath*:config_common/*.properties, classpath*:config_test/*.properties"/>
	</beans>

	<!-- 生产环境配置文件 -->
	<beans profile="prod">
		<!-- 生产环境配置文件 -->
		<context:property-placeholder location="classpath*:config_common/*.properties, classpath*:config_prod/*.properties"/>
	</beans>

三. web.xml

  <!-- 多环境配置 在上下文context-param中设置profile.default的默认值 -->
  <context-param>
    <param-name>spring.profiles.default</param-name>
    <param-value>test</param-value>
  </context-param>
  <!-- 多环境配置 在上下文context-param中设置profile.active的默认值 -->
  <!-- 设置active后default失效,web启动时会加载对应的环境信息 -->
  <context-param>
    <param-name>spring.profiles.active</param-name>
    <param-value>test</param-value>
  </context-param>

四.加载配置

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
/**
 * SpringContextHelper用来启动时看是那个环境_可以不加
 */
@Component
public class SpringContextHelper implements ApplicationContextAware {

    private static final Logger log = LoggerFactory.getLogger(SpringContextHelper.class);
    private static ApplicationContext context = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
        log.info("当前环境配置是:{}", getEnvProfile());
    }

    public String getEnvProfile(){
        Environment env = context.getEnvironment();
        String[] profile=env.getActiveProfiles();
        String s=null;
        for (int i=0; i<profile.length;i++){
            s = profile[0];
        }
        return s;
    }
}

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Config {
	/** 默认 */
	@Value("${config.publicKey}")
	public String publicKey;
	@Value("${config.privateKey}")
	public String privateKey;
      
        //get set 代码略
}

五 读取配置

public enum EnumPartnerCodeRES {
    DEFAULT("getPublicKey","getPrivateKey"),
    ;

    private String publicKey;
    private String privateKey;
    
    private EnumPartnerCodeRES(String publicKey, String privateKey) {
    	 this.publicKey = publicKey;  
         this.privateKey = privateKey; 
	}

	public String getPublicKey() {
		return publicKey;
	}

	public void setPublicKey(String publicKey) {
		this.publicKey = publicKey;
	}

	public String getPrivateKey() {
		return privateKey;
	}

	public void setPrivateKey(String privateKey) {
		this.privateKey = privateKey;
	}
}
        //这是一个@Service类下的一个方法

	@Autowired
	private Config config;

        @Override
	public String getPublicKey(String partnerCodeEnumKey) {
		log.info("获取合作伙伴的公钥代码");
		String publicKey = "";
		for (EnumPartnerCodeRES partnerCodeEnum: EnumPartnerCodeRES.values()){
			    
	            if(partnerCodeEnum.name().equals(partnerCodeEnumKey)){
	            	Class cls =  config.getClass();
	            	try {
	            		Method method =   cls.getMethod(  partnerCodeEnum.getPublicKey(), new Class[0]);
						publicKey =(String) method.invoke(config, new Object[]{});
					} catch ( Exception e) {
						log.error("获取值异常",e);
						e.printStackTrace();
					}
	                break;
	            }
	    }
		if(publicKey == null ) {
			log.info("获取配置数据失败!");
			throw new RuntimeException("获取合作伙伴的公钥代码,系统配置异常");
		}
		return publicKey;
	}
posted @ 2022-08-26 11:33  tlblog  阅读(806)  评论(0编辑  收藏  举报