And*_*roj 54 java spring dependency-injection autowired
我有DataPrepareService为报告准备数据,我有一个包含报告类型的枚举,我需要将ReportService注入Enum或从enum访问ReportService.
我的服务:
@Service
public class DataPrepareService {
// my service
}
Run Code Online (Sandbox Code Playgroud)
我的枚举:
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
}
}
Run Code Online (Sandbox Code Playgroud)
我试着用
@Autowired
DataPrepareService dataPrepareService;
Run Code Online (Sandbox Code Playgroud)
,但它没有用
如何将我的服务注入枚举?
小智 59
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);
}
}
[...]
}
Run Code Online (Sandbox Code Playgroud)
如果你将内部类改为静态,那么周期的回答是有效的,所以春天可以看到它
wee*_*ens 11
也许是这样的:
public enum ReportType {
@Component
public class ReportTypeServiceInjector {
@Autowired
private DataPrepareService dataPrepareService;
@PostConstruct
public void postConstruct() {
for (ReportType rt : EnumSet.allOf(ReportType.class))
rt.setDataPrepareService(dataPrepareService);
}
}
REPORT_1("name", "filename"),
REPORT_2("name", "filename"),
...
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
29740 次 |
最近记录: |