我有为报表准备数据的DataPrepareService,并且我有一个具有报表类型的Enum,并且我需要将ReportService注入Enum或从枚举中访问ReportService。
我的服务:
@Service
public class DataPrepareService {
// my service
}
我的枚举:
public enum ReportType {
REPORT_1("name", "filename"),
REPORT_2("name", "filename"),
REPORT_3("name", "filename")
public abstract Map<String, Object> getSpecificParams();
public Map<String, Object> getCommonParams(){
// some code that requires service
}
}
我尝试使用
@Autowired
DataPrepareService dataPrepareService;
,但是没有用
如何将我的服务注入枚举?
public enum ReportType {
REPORT_1("name", "filename"),
REPORT_2("name", "filename");
@Component
public static class ReportTypeServiceInjector {
@Autowired
private DataPrepareService dataPrepareService;
@PostConstruct
public void postConstruct() {
for (ReportType rt : EnumSet.allOf(ReportType.class))
rt.setDataPrepareService(dataPrepareService);
}
}
[...]
}
如果将内部类更改为静态,则weekens的答案有效,因此spring可以看到