Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jerey 2.30 breaks HK2 AbstractBinder injection in Features #4388

Closed
cykl opened this issue Feb 7, 2020 · 1 comment
Closed

Jerey 2.30 breaks HK2 AbstractBinder injection in Features #4388

cykl opened this issue Feb 7, 2020 · 1 comment
Assignees
Labels
bug Something isn't working
Milestone

Comments

@cykl
Copy link

cykl commented Feb 7, 2020

Since I upgraded to Jersey 2.30, it is no longer possible to inject an instance defined by an HK2 AbstractBinder into a Feature.

Here is a minimal test case:

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;

import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;

public class MTC {

    public static void main( String[ ] args ) throws Exception {
        var test = new JerseyTest( ) {

            @Override
            protected Application configure( ) {
                final ResourceConfig config = new ResourceConfig( );

                var binder = new AbstractBinder( ) {
                    @Override
                    protected void configure( ) {
                        System.out.println( "Configuring binder" );
                        bindAsContract( Dependency.class ).in( Singleton.class );
                    }
                };

                config.register( binder );
                config.register( MyFeature.class );
                return config;
            }
        };

        test.setUp( );
    }

    static final class MyFeature implements Feature {

        private final Dependency service;

        @Inject
        public MyFeature( Dependency service ) {
            this.service = service;
        }

        @Override
        public boolean configure( FeatureContext context ) {
            return true;
        }
    }

    static final class Dependency {

        @Inject Dependency( ) {
            System.out.println( "Constructing Service" );
        }
    }
}

It used to work on Jersey 2.29. Switching to a Jersey's AbstractBinder also works. This is likely related to #4349.

@cykl
Copy link
Author

cykl commented Feb 7, 2020

Root cause is likely 29913b9#diff-546b2ffbf565a0242fdfb1dd4329380bR626

CommonConfig:

    public void configureMetaProviders(InjectionManager injectionManager, ManagedObjectsFinalizer finalizer) {
        // First, configure existing binders
        Set<Binder> configuredBinders = configureBinders(injectionManager, Collections.emptySet());

        // Check whether meta providers have been initialized for a config this config has been loaded from.
        if (!disableMetaProviderConfiguration) {
            // Configure all features
            configureFeatures(injectionManager, new HashSet<>(), resetRegistrations(), finalizer);
            // Next, register external meta objects
            configureExternalObjects(injectionManager);
            // At last, configure any new binders added by features
            configureBinders(injectionManager, configuredBinders);
        }

HK2 binders are external objects and are now configured after features.

@jansupol jansupol self-assigned this Feb 12, 2020
@jansupol jansupol added the bug Something isn't working label Feb 12, 2020
@jansupol jansupol added this to the 2.30.1 milestone Feb 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants