Auto scan

I have never used a pattern before, and I wanted to try it on an example of a project with a JAX-RS API based on knitwear supported by the bean service. I followed this guide: http://randomizedsort.blogspot.de/2011/05/using-guice-ified-jersey-in-embedded.html and was able to put it into action. My setup is very simple: the JAX-RS resource is called through Guice and has a field annotated by @Inject and injected by Guice:

@Path("configuration") @Produces(MediaType.APPLICATION_JSON) @Singleton public class ConfigurationResource { @Inject private ConfigurationService configurationService; 

So far so good, everything is working as it should, except for the following: I use GuiceServletContextListener to configure things and must explicitly specify each component:

 @WebListener public class GuiceInitializer extends GuiceServletContextListener{ @Override protected Injector getInjector() { return Guice.createInjector(new JerseyServletModule() { @Override protected void configureServlets() { //resources bind(ConfigurationResource.class); //services bind(ConfigurationService.class).to(ConfigurationServiceImpl.class); // Route all requests through GuiceContainer serve("/management/*").with(GuiceContainer.class); } }); } } 

It seems to me that it is inconvenient to explicitly indicate all the dependencies. Previously, I worked with an autonomous knitting machine and was perfectly able to automatically scan resources in certain packages. In addition, Spring and CDI are able to map implementations to interfaces without the need to explicitly specify them.

Now the question is:

  • Is there any extension / autostart setting for guice? I found some on the Internet, but it’s hard to say which ones are still usable and redistributable.

  • Is there any other way to make the configuration of implementations and resources more convenient?

Thank you in advance. Leon

+5
source share
1 answer

I don't think Guice had built-in support for companion, as a component-scan of the Spring structure. However, it is not difficult to simulate this feature in Guice.

You just need to write a helper module, like the following

 import com.google.inject.AbstractModule; import org.reflections.Reflections; import java.lang.annotation.Annotation; import java.util.Arrays; import java.util.HashSet; import java.util.Set; /** * To use this helper module, call install(new ComponentScanModule("com.foo", Named.class); in the configure method of * another module class. */ public final class ComponentScanModule extends AbstractModule { private final String packageName; private final Set<Class<? extends Annotation>> bindingAnnotations; @SafeVarargs public ComponentScanModule(String packageName, final Class<? extends Annotation>... bindingAnnotations) { this.packageName = packageName; this.bindingAnnotations = new HashSet<>(Arrays.asList(bindingAnnotations)); } @Override public void configure() { Reflections packageReflections = new Reflections(packageName); bindingAnnotations.stream() .map(packageReflections::getTypesAnnotatedWith) .flatMap(Set::stream) .forEach(this::bind); } } 

To have the component scan a package of type com.foo and com.foo for classes that carry @Singleton , use it as follows:

 public class AppModule extends AbstractModule { public void configure() { install(new ComponentScanModule("com.foo", Singleton.class)); } } 
+3
source

All Articles