1. 程式人生 > >java將配置信息寫在數據庫(利用反射)

java將配置信息寫在數據庫(利用反射)

static source style ali sys @override back country tco

Demo出處:

1. package com.fpx.pcs.prealert.process.xml.service.impl;

public class CainiaoPushMessageServiceImpl implements ICainiaoPushMessageService {

@Resource

private CommonImpl commonImpl;

@Override
public ResultMessage execute(String xml, String msg_type, String packageCarrierType, String countryCode) {

CaiNiaoCallBackConfig config=commonImpl.getConfig("CaiNiaoCallBackConfig",CaiNiaoCallBackConfig.class);

}

2.---CaiNiaoCallBackConfig 類---------------------

@data

public class CaiNiaoCallBackConfig implements Serializable{

private static final long serialVersionUID=1L;

private String CONSO3PLcooperationCode;

private String CONSO3PLencryprDigest;

private String CONSO3PLreturnUrl;

}

3.getConfig 寫法

public <T> T getConfig(String configName,Class<T> targetClass){

List<SysParameterPO> configs=getListByName(configName);

if(CollectionUtils.isEmpty(configs)){

return null;

}

T t=null;

try{

t=targetClass.newInstance();

for(SysParameterPO config :configs){

try{

Method method=t.getClass().getMethod("set"+config.getCode(),String.class);

if(method !=null){

method.invoke(t,config.getvalue());

}

catch(Exception e)

}

catch(Exception e)

{

}

}

}

catch(InstantiationException | illegeAccessException e){

e.printStatckTrace();

}

catch(SecurityException e)

{

e.printStackTrace();

}

}

java將配置信息寫在數據庫(利用反射)