如何在 Spring 或 EE 上重写 Guice Interface DI Injector.getInjector().getInstance(Interface.class)

How to re-write Guice Interface DI Injector.getInjector().getInstance(Interface.class) on Spring or EE

Guice 可用于获取 Interface.class 的实例, 但是我们如何在 Java EE 或 Spring 中为 Interface 获得相同的东西?

TestVar testVar = Injector.getInjector().getInstance(Interface.class)

使用 spring IoC 容器是 ApplicationContext。你可以直接这样请求。

ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
Interface myService = ctx.getBean(Interface.class);

或者你可以将它注入到你的组件中。

请记住,这是一种不好的做法。你不应该直接请求容器。你应该让它为你解决依赖关系。