fix(nacos): fix issue #859

pull/920/head
chuntaojun 5 years ago
parent d2684c2882
commit 5162d02b4e

@ -90,11 +90,8 @@ class SampleController {
@Value("${user.age:25}")
Integer age;
private final NacosConfigManager nacosConfigManager;
public SampleController(NacosConfigManager nacosConfigManager) {
this.nacosConfigManager = nacosConfigManager;
}
@Autowired
private NacosConfigManager nacosConfigManager;
@RequestMapping("/user")
public String simple() {

@ -17,52 +17,25 @@
package com.alibaba.cloud.nacos;
import com.alibaba.nacos.api.config.ConfigService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Objects;
/**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
*/
public class NacosConfigManager implements ApplicationContextAware {
public ConfigService getConfigService() {
return ServiceHolder.getInstance().getService();
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
NacosConfigProperties properties = applicationContext
.getBean(NacosConfigProperties.class);
ServiceHolder holder = ServiceHolder.getInstance();
if (!holder.alreadyInit) {
ServiceHolder.getInstance().setService(properties.configServiceInstance());
}
}
static class ServiceHolder {
private ConfigService service = null;
public class NacosConfigManager {
private boolean alreadyInit = false;
@Autowired
private NacosConfigProperties properties;
private static final ServiceHolder holder = new ServiceHolder();
private static ConfigService service = null;
ServiceHolder() {
}
static ServiceHolder getInstance() {
return holder;
}
void setService(ConfigService service) {
alreadyInit = true;
this.service = service;
}
ConfigService getService() {
return service;
public ConfigService getConfigService() {
if (Objects.isNull(service)) {
service = properties.configServiceInstance();
}
return service;
}
}

@ -18,68 +18,33 @@ package com.alibaba.cloud.nacos;
import com.alibaba.nacos.api.naming.NamingMaintainService;
import com.alibaba.nacos.api.naming.NamingService;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Objects;
/**
* @author <a href="mailto:liaochunyhm@live.com">liaochuntao</a>
*/
public class NacosNamingManager implements ApplicationContextAware {
public class NacosNamingManager {
public NamingService getNamingService() {
return ServiceHolder.getInstance().getNamingService();
}
private static NamingService namingService = null;
private static NamingMaintainService namingMaintainService = null;
public NamingMaintainService getNamingMaintainService() {
return ServiceHolder.getInstance().getNamingMaintainService();
}
@Autowired
private NacosDiscoveryProperties discoveryProperties;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
NacosDiscoveryProperties properties = applicationContext
.getBean(NacosDiscoveryProperties.class);
ServiceHolder holder = ServiceHolder.getInstance();
if (!holder.alreadyInit[0]) {
holder.setNamingService(properties.namingServiceInstance());
}
if (!holder.alreadyInit[1]) {
holder.setNamingMaintainService(properties.namingMaintainServiceInstance());
public NamingService getNamingService() {
if (Objects.isNull(namingService)) {
namingService = discoveryProperties.namingServiceInstance();
}
return namingService;
}
static class ServiceHolder {
private NamingService namingService = null;
private NamingMaintainService namingMaintainService = null;
final boolean[] alreadyInit = new boolean[2];
private static final ServiceHolder HOLDER = new ServiceHolder();
ServiceHolder() {
}
static ServiceHolder getInstance() {
return HOLDER;
}
public NamingService getNamingService() {
return namingService;
}
void setNamingService(NamingService namingService) {
alreadyInit[0] = true;
this.namingService = namingService;
}
NamingMaintainService getNamingMaintainService() {
return namingMaintainService;
}
void setNamingMaintainService(NamingMaintainService namingMaintainService) {
alreadyInit[1] = true;
this.namingMaintainService = namingMaintainService;
public NamingMaintainService getNamingMaintainService() {
if (Objects.isNull(namingMaintainService)) {
namingMaintainService = discoveryProperties.namingMaintainServiceInstance();
}
return namingMaintainService;
}
}

Loading…
Cancel
Save