HK2工厂实现单例

raw*_*wat 1 jersey-2.0 hk2

我试图了解 HK2 Factory 在 Jersey 应用程序中的实现。

目标:如何实现单例工厂?

    // Below is the simple factory implementation

    public class MyFactory implements Factory<SomeObject> {

        private static final Logger logger = LoggerFactory.getLogger(MyFactory.class);
        private final CloseableService closeService;

        @Inject
        public MyFactory(CloseableService closeService) {
            this.closeService = closeService;
        }

        @Override
        public MyFactory provide() {
            logger.debug("provide object from MyFactory");
            SomeObject objectFromFactory = new SomeObject();
            this.closeService.add(() -> dispose(objectFromFactory));
            return objectFromFactory;
        }

        @Override
        public void dispose(SomeObject instance) {
            // destroy instance
             logger.debug("dispose object from MyFactory");
        }
    }

    // and binding
     bindFactory(MyFactory.class).to(SomeObject.class).in(Singelton.class);

     // to check object creation and destruction
     bind(HK2InstanceListener.class).to(InstanceLifecycleListener.class).in(Singleton.class);

    // and injecting in some resource class

    @Inject
    SomeObject useThisObject;

    //updated information 

    AbstractBinder abstractBinder = configureBinder();

    ServiceLocator appServiceLocator = configureHK2(abstractBinder);

    public static AbstractBinder configureBinder() {
            return new AbstractBinder() {
                @Override
                protected void configure() {
         bindFactory(MyFactory.class).to(SomeObject.class).in(Singelton.class);

           // to check object creation and destruction
         bind(HK2InstanceListener.class).to(InstanceLifecycleListener.class).in(Singleton.class);

         }
         }

    public ServiceLocator configureHK2(AbstractBinder binder) {
            ServiceLocatorFactory factory = ServiceLocatorFactory.getInstance();
            ServiceLocator locator = factory.create("my-test-server");

            DynamicConfigurationService dcs = locator.getService(DynamicConfigurationService.class);
            DynamicConfiguration dc = dcs.createDynamicConfiguration();

            locator.inject(binder);
            binder.bind(dc);
            dc.commit();
            return locator;
        }
Run Code Online (Sandbox Code Playgroud)

启动应用程序时,我在日志中看到下面的内容

    10:38:34.122 [grizzly-http-server-0] DEBUG com.test.HK2InstanceListener - HK2 before object create : com.test.MyFactory
    10:38:34.125 [grizzly-http-server-0] DEBUG com.test.HK2InstanceListener - HK2 before object create : com.test.MyFactory
    10:38:34.125 [grizzly-http-server-0] DEBUG com.test.HK2InstanceListener - HK2 after object create : com.test.MyFactory
    10:38:34.125 [grizzly-http-server-0] DEBUG com.test.MyFactory provide - object from MyFactory
    10:38:35.700 [grizzly-http-server-0] DEBUG com.test.HK2InstanceListener - HK2 after object create : com.test.MyFactory

    10:38:37.743 [grizzly-http-server-0] DEBUG com.test.MyFactory - dispose object from MyFactory
Run Code Online (Sandbox Code Playgroud)
  1. 当范围=单例时

    • 创建 MyFactory 的两个对象
    • 下一个请求失败,并出现空指针异常@Inject。
  2. 当范围 = RequestScoped、PerLookup 时

    • 每个请求都会创建两个 MyFactory 对象

通过单例工厂,我理解是工厂(MyFactory)的单个对象,它在注入时提供某种对象。

那么(1)应该有效还是我错过了什么?

为什么有两个工厂对象?

有什么建议么?提前致谢。

HK2 Version : 2.5.0-b60
Jersey Version: 2.26
Run Code Online (Sandbox Code Playgroud)

有关 NPE 的其他信息

它不是来自 HK2,但.in(Singleton.class).in(PerLookup.class)之间的行为不同

// SomeObject looks like

Class SomeObject
{

    private Stack<String> someStack; 

     public SomeObject() {

        // this may be the issue for Singleton
        this.someStack = new Stack();
    }

    public someOperation(String stackIt)
    {
        // NPE location
        this.someStack.push(stackIt);
    }
}
Run Code Online (Sandbox Code Playgroud)

位于上方位置时的 NPE 位于下方

bindFactory(MyFactory.class,Singleton.class).to(SomeObject.class).in(Singleton.class);
Run Code Online (Sandbox Code Playgroud)

以下位置时以上位置无 NPE

bindFactory(MyFactory.class,Singleton.class).to(SomeObject.class).in(PerLookup.class);
Run Code Online (Sandbox Code Playgroud)

jwe*_*313 5

当您执行bindFactory时,使用第二个参数将Factory绑定为单例。您这样做的方式只是将提供方法绑定为单例。因此,要使工厂本身也成为单例,请这样做:

bindFactory(MyFactory.class, Singleton.class).to(SomeObject.class).in(Singelton.class);
Run Code Online (Sandbox Code Playgroud)

这应该将提供的东西和工厂本身绑定为单例。