最近遇到一个问题 , 就是获取配置类里面的配置信息的时候 获取不到数据
public ReceiveOrderInfoResponse receiveOrder(ReceiveOrderInfoRequest request) throws ExpressErrorException {
JdlConfig config = this.jdlService.getConfig();
//config = null ;
request.getWaybillDTO().setJosPin(config.getPin());
request.getWaybillDTO().setAppKey(config.getAppKey());
request.getWaybillDTO().setCustomerCode(config.getCustomerCode());
return this.jdlService.execute(request,ReceiveOrderInfoResponse.class);
}
后面发现原因是 配置类 jdlConfig 没有注册到容器中 于是我就写了一个 工厂类 用于将配置类 注册到容器中
package com.plz.kd.jdl.config;
import org.apache.commons.collections4.Factory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
@Component
public class ConfigFactory implements FactoryBean<JdlConfig> {
private static JdlConfig jdlConfig = null;
//若有需要优化的地方 , 请大家多多指点 谢谢!
@Override
public JdlConfig getObject() throws Exception {
if (jdlConfig == null) {
synchronized (JdlConfig.class) {
if (jdlConfig == null) {
jdlConfig = new JdlConfig();
}
}
}
return jdlConfig;
}
@Override
public Class<?> getObjectType() {
return JdlConfig.class;
}
}
问题解决
还有另外一种方式 就是在启动类 那里注册 但是我不知道怎么写 希望有大佬指点一下