这是spring的xml配置文件
class="test.service.ZookeeperService">
127.0.0.1
/limit_02
lbx
class="test.service.LimitService">
http://127.0.0.1
这是ZookeeperService ,应用启动的时候会走这里加载zookeeper一些配置这里就能拿到参数,但是afterPropertiesSet这个方法不知道为什么会运行两次,第二次这些zkPath,applicationName,zookeeperHost就为null了
@Service
public class ZookeeperService implements InitializingBean {
private String zkPath;
private String applicationName;
private String zookeeperHost;
public void afterPropertiesSet() throws Exception {
init();
setActionInfo();
initZkWatcher();
}
public String getZookeeperHost() {
return zookeeperHost;
}
public void setZookeeperHost(String zookeeperHost) {
this.zookeeperHost = zookeeperHost;
}
public String getApplicationName() {
return applicationName;
}
public void setApplicationName(String applicationName) {
this.applicationName = applicationName;
}
public String getZkPath() {
return zkPath;
}
public void setZkPath(String zkPath) {
this.zkPath = zkPath;
}
}
这是LimitService,程序运行到这里url就为null,拿不到不知道为什么
@Service
public class LimitService{
private String url= null;
public Map rateLimitAction(String actionName, String userCode) {
Map reMap = new HashMap();
paramMap.put("serverUrl ", url);
return reMap;
}
public String getRateLimitServerUrl() {
return rateLimitServerUrl;
}
public void setRateLimitServerUrl(String rateLimitServerUrl) {
this.rateLimitServerUrl = rateLimitServerUrl;
}
}
回答
你xml配置了一次,@Service又配置了一次。并且@Service配置的时候,属性也没有注入,当然就是null了。xml和注解使用其中一个就好了