【问题标题】:Using custom factory HK2 DI with Jersey使用带有 Jersey 的定制工厂 HK2 DI
【发布时间】:2023-03-11 14:30:01
【问题描述】:

我在我的 Jersey 应用程序中使用 HK2 容器。我需要使用我的自定义工厂方法从 HK2 容器中获取注入的实例。 例如,

// Here I declare the IOC binding.
public class ApplicationBinder extends AbstractBinder {
    @Override
    protected void configure() {
        bind(Logger.class).to(ILogger.class).in(Singleton.class);;       
        bind(MySqlRepository.class).to(IRepository.class).in(Singleton.class);
    }
}



 public class MyApplication extends ResourceConfig {
    public static ApplicationBinder binder ;
    public MyApplication () {
        binder = new ApplicationBinder();
        register(binder);
        packages(true, "com.myapplication.server");
    }
    }

这是我的代码:

public class BusinessLogic

    {
        //@Inject
        //ILogger logger ; 

        //Instead 
        ILogger logger = DependencyResolver .resolve(ILogger.class) // resolve will get ILogger from HK2   container
    }

我需要这样做的原因是有时,我手动分配具有依赖关系的类,因此每次使用 @Inject 都会返回 null。 例如,如果我使用 new BusinessLogic() ,则带有 @Inject 的记录器为空。我还必须绑定业务逻辑并使用 IOC 才能获得 ILogge。

我需要这样的东西:

public class DependencyResolver {    

    public static <T> T resolve(Class<T> beanClass){           
        return instance;
    }    
}

我需要使用 DependencyResolver 来获取我在 MyApplication 中注册的实例。

任何建议。 在此先感谢...

【问题讨论】:

  • 您是否尝试构建一个 org.glassfish.hk2.api.Factory 以满足您的需求?
  • 我做了,但我需要使用我在 ResourceConfig 中使用 ApplicationBinder 进行的绑定配置。
  • 请提供更多信息,即代码。您到底尝试将 ILogger 注入到您的存储库中是什么? Repository 是如何参与的——它是否绑定在某个地方?
  • 请看我编辑的问题。

标签: java jersey-2.0 hk2


【解决方案1】:

我不能 100% 确定您到底想做什么,但是...

我认为您误解了AbstractBinder.bind(...) 或绑定本身。此外,您不能将某些东西注入不是托管组件的实例(例如您的BusinessLogic)。

有关您的BusinessLogic 的示例,请参见jersey.java.net - ioc。你可以看看ComponentProvider 和/或InjectableProvider

对于您的 ILogger,我建议像这样创建和绑定工厂:

public class LoggerFactory implements Factory<ILogger> {

    // inject stuff here if you need (just an useless example)
    @Inject
    public LoggerFactory(final UriInfo uriInfo) {
        this.uriInfo = uriInfo;
    }

    @Override
    public ILogger provide() {
        // here you resolve you ilogger
        return MyLocator.resolve(ILogger.class);
    }

    @Override
    public void dispose(ILogger instance) {
        // ignore
    }

}

绑定工厂

public class ApplicationBinder extends AbstractBinder {
    @Override
    protected void configure() {
        bindFactory(LoggerFactory.class).to(ILogger.class).in(PerLookup.class /* or other scopeAnnotation if needed */);

        // what's you Logger.class ? 
        // bind(Logger.class).to(ILogger.class).in(Singleton.class);      
        // bind(MySqlRepository.class).to(IRepository.class).in(Singleton.class);
    }
}

希望这对您有所帮助。也许有人愿意为您的案例写一些关于 Providers 的内容。

【讨论】:

  • 感谢 allot,“实例中的某些东西不是托管组件(例如您的 BusinessLogic)。” ,我认为让我将 IOC 管理的服务注入到我的 BusinessLogic 中非常有帮助,就像使用 ILogger 一样,所以在这里我不能使用注解 Inject 并且我需要其他选项来获取 ILogger 那里。希望我很清楚。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-17
  • 2021-06-12
相关资源
最近更新 更多