将 Spring bean 注入枚举并在枚举功能接口中调用它
Inject Spring bean into enum and invoke it inside enums functional interface
好吧,我想我正在尝试实现更多目标,但我想为我的应用程序中的所有字典值创建一个聚合器 class。因此我创建了一个有两个参数的枚举——第一个是字典名称,第二个是 Supplier
。我选择 Supplier
是因为每个字典的提供值计算方式不同。到目前为止,我想到了这个:
public enum DictionaryAggregator {
STATUS_X("statusX", StatusX::getDictionary),
STATUS_Y("statusY", new Supplier<Map<String, ?>>() {
@Override
public Map<String, ?> get() {
return <would like to reference to statusYCache which is inside DictionaryAggregatorServiceInjector>;
}
});
private final String dictionaryName;
private final Supplier<Map<String, ?>> dictionary;
public String getDictionaryName() {
return dictionaryName;
}
public Supplier<Map<String, ?>> getDictionary() {
return dictionary;
}
DictionaryAggregator(String dictionaryName, Supplier<Map<String, ?>> dictionary) {
this.dictionaryName = dictionaryName;
this.dictionary = dictionary;
}
@Component
static class DictionaryAggregatorServiceInjector {
@Autowired
private StatusYService statusYService;
}
}
但无论我尝试过什么,我都想不出如何从 StatusYCache
服务中提供值。理想情况下,我想要实现的是 STATUS_Y
的 Supplier
可以轻松地从 StatusYService
.
调用方法
我想我应该更频繁地使用我的大脑。我想到了这个:
public enum DictionaryAggregator {
STATUS_X("statusX", StatusX::getDictionary),
STATUS_Y("statusY", new Supplier<Map<String, ?>>() {
@Override
public Map<String, ?> get() {
return new StatusYDictionaryClass(DictionaryAggregatorServiceInjector.shipmentStatusVos).getDictionary();
}
});
private final String dictionaryName;
private final Supplier<Map<String, ?>> dictionary;
public String getDictionaryName() {
return dictionaryName;
}
public Supplier<Map<String, ?>> getDictionary() {
return dictionary;
}
DictionaryAggregator(String dictionaryName, Supplier<Map<String, ?>> dictionary) {
this.dictionaryName = dictionaryName;
this.dictionary = dictionary;
}
@Component
static class DictionaryAggregatorServiceInjector {
static List<StatusYObj> statusYList;
@Autowired
public StatusYService statusYService;
@PostConstruct
public void init() {
statusYList = statusYService.getStatusYList();
}
}
}
似乎有效。
好吧,我想我正在尝试实现更多目标,但我想为我的应用程序中的所有字典值创建一个聚合器 class。因此我创建了一个有两个参数的枚举——第一个是字典名称,第二个是 Supplier
。我选择 Supplier
是因为每个字典的提供值计算方式不同。到目前为止,我想到了这个:
public enum DictionaryAggregator {
STATUS_X("statusX", StatusX::getDictionary),
STATUS_Y("statusY", new Supplier<Map<String, ?>>() {
@Override
public Map<String, ?> get() {
return <would like to reference to statusYCache which is inside DictionaryAggregatorServiceInjector>;
}
});
private final String dictionaryName;
private final Supplier<Map<String, ?>> dictionary;
public String getDictionaryName() {
return dictionaryName;
}
public Supplier<Map<String, ?>> getDictionary() {
return dictionary;
}
DictionaryAggregator(String dictionaryName, Supplier<Map<String, ?>> dictionary) {
this.dictionaryName = dictionaryName;
this.dictionary = dictionary;
}
@Component
static class DictionaryAggregatorServiceInjector {
@Autowired
private StatusYService statusYService;
}
}
但无论我尝试过什么,我都想不出如何从 StatusYCache
服务中提供值。理想情况下,我想要实现的是 STATUS_Y
的 Supplier
可以轻松地从 StatusYService
.
我想我应该更频繁地使用我的大脑。我想到了这个:
public enum DictionaryAggregator {
STATUS_X("statusX", StatusX::getDictionary),
STATUS_Y("statusY", new Supplier<Map<String, ?>>() {
@Override
public Map<String, ?> get() {
return new StatusYDictionaryClass(DictionaryAggregatorServiceInjector.shipmentStatusVos).getDictionary();
}
});
private final String dictionaryName;
private final Supplier<Map<String, ?>> dictionary;
public String getDictionaryName() {
return dictionaryName;
}
public Supplier<Map<String, ?>> getDictionary() {
return dictionary;
}
DictionaryAggregator(String dictionaryName, Supplier<Map<String, ?>> dictionary) {
this.dictionaryName = dictionaryName;
this.dictionary = dictionary;
}
@Component
static class DictionaryAggregatorServiceInjector {
static List<StatusYObj> statusYList;
@Autowired
public StatusYService statusYService;
@PostConstruct
public void init() {
statusYList = statusYService.getStatusYList();
}
}
}
似乎有效。