@Immediate annotation use in Jersey2

后端 未结 1 1056
南方客
南方客 2021-01-13 01:03

I have a similar issue to the one raised here: How do I get my Jersey 2 Endpoints to eagerly initialize on startup?

But slightly further down the line. I can get my

相关标签:
1条回答
  • 2021-01-13 01:38

    One way to get a handle on the ServiceLocator is to implement a Feature.

    import javax.inject.Inject;
    import javax.ws.rs.core.Feature;
    import javax.ws.rs.core.FeatureContext;
    import org.glassfish.hk2.api.ServiceLocator;
    import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
    
    public class ImmediateFeature implements Feature {
    
        @Inject
        public ImmediateFeature(ServiceLocator locator) {
            ServiceLocatorUtilities.enableImmediateScope(locator);            
        }
    
        @Override
        public boolean configure(FeatureContext context) {
            return true;
        }  
    }
    

    Then just register the Feature

    ResourceConfig rc = new ResourceConfig().packages("jersey.hk2.test");
    rc.register(ImmediateFeature.class);
    

    I've tested this and it works fine

    0 讨论(0)
提交回复
热议问题