Resteasy and Google Guice: how to use multiple @ApplicationPath and resource with @Injection?

前端 未结 1 1914
夕颜
夕颜 2021-01-17 21:50

I created a project to test the dependency injection offered by Google Guice in my Jax-rs resources, using Resteasy.

My intentions are:

  • Use multiple
相关标签:
1条回答
  • 2021-01-17 22:37

    When you have getClasses method in your Application then it tries to create instance for all the registered resources using the default constructor which is missing in our Resources class. One way is to create a default constructor and Inject the dependencies through setter Injection. And then instead of overriding getClasses in ApplicationV1RS and ApplicationV2RS you override getSingletons. Since Resources can be Singleton.

    Below are the changes that I made to make it work the way you want.

    ApplicationV1RS.java

    @ApplicationPath("v1")
    public class ApplicationV1RS extends Application {
    
        private Set<Object> singletons = new HashSet<Object>();
    
        public ApplicationV1RS(@Context ServletContext servletContext) {
        }
    
        @Override
        public Set<Object> getSingletons() {
            Injector injector = Guice.createInjector(new HelloModule());
    
            HelloResource helloResource = injector.getInstance(HelloResource.class);
            UserResource userResource = injector.getInstance(UserResource.class);
            singletons.add(helloResource);
            singletons.add(userResource);
            return singletons;
        }
    }
    

    ApplicationV2RS.java

    @ApplicationPath("v2")
    public class ApplicationV2RS extends Application {
    
        private Set<Object> singletons = new HashSet<Object>();
    
        public ApplicationV2RS(@Context ServletContext servletContext) {
        }
    
        @Override
        public Set<Object> getSingletons() {
            Injector injector = Guice.createInjector(new HelloModule());
    
            HelloResource helloResource = injector.getInstance(HelloResource.class);
            UserResource userResource = injector.getInstance(UserResource.class);
            singletons.add(helloResource);
            singletons.add(userResource);
            return singletons;
        }
    }
    

    HelloResource.java

    @Path("hello")
    public class HelloResource {
        @Inject
        private IGreeterService greeter;
    
        public HelloResource() {
        }
    
        @GET
        @Path("{name}")
        public String hello(@PathParam("name") final String name) {
            return greeter.greet(name);
        }
    }
    

    UserResource.java

    @Path("user")
    public class UserResource {
    
        @Inject
        private IUserService userService;
    
        public UserResource() {
        }
    
        @GET
        @Path("{name}")
        public String hello(@PathParam("name") final String name) {
            return userService.getUser(name);
        }
    }
    

    Add @Singleton to your Service Classes.

    Hope it helps.

    I have also pushed the code to forked repo. check it out

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