在枚举中使用Spring bean

时间:2019-03-06 09:00:40

标签: spring dependency-injection

我正在尝试将Spring bean注入枚举并使用bean方法。我在堆栈溢出中发现了类似的内容,但是object的值为null。

这部分代码

System.out.println("# Post constructor ")

从不执行。

问题出在哪里?

enum EnumDependencyInjection {

ENUM_DI {

    @Override
    void writeLog(String itemName) {

        logService_enum.methodFromLogServiceBean();

    }

};

abstract void writeLog(String itemName);


@Component
public static class ServiceInjector {

    @Autowired
    private ILogService logService;

    @PostConstruct
    public void postConstruct() {

      for (EnumDependencyInjection enumDI : EnumSet.allOf(EnumDependencyInjection.class)) {

        System.out.println("# Post constructor ");

        enumDI.setLogService(logService);

      }


    }

}

private static ILogService logService_enum;

void setLogService(ILogService logService) {
    logService_enum = logService;
    }

}

0 个答案:

没有答案