Inject bean into enum

前端 未结 8 2077
误落风尘
误落风尘 2020-11-30 02:01

I have the DataPrepareService that prepare data for reports and I have an Enum with report types, and I need to inject ReportService into Enum or have access to ReportServic

8条回答
  •  被撕碎了的回忆
    2020-11-30 02:40

    Maybe something like this:

    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"),
        ...
    }
    

提交回复
热议问题