java将配置信息写在数据库(利用反射)

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();

}

 

 

}

posted on 2018-07-09 14:23  chengjunde  阅读(264)  评论(0编辑  收藏  举报

导航