Using Guice 3 with JAX-WS in Java 6 outside web container

后端 未结 2 1476
生来不讨喜
生来不讨喜 2021-02-10 03:12

We have a situation where we use JSR-330 based injections to configure our stand-alone Java 6 applications, which works very well for getting configuration parameters across all

2条回答
  •  离开以前
    2021-02-10 03:40

    I'm not sure that I've understood every bit of the question. It looks to too easy for +500 bounty. Please post some code if that's not what you're searching for.

    Anyway, a simple solution which creates a web service with dependency injection:

    final Module module = new HelloModule();
    final Injector injector = Guice.createInjector(module);
    final HelloService helloService = injector.getInstance(HelloService.class);
    
    Endpoint.publish("http://localhost:8080/helloService", helloService);
    

    Below a more sophisticated solution with classpath scanning (Reflections) based on Marcus Eriksson's code from JAX-WS Guice integration. It publishes all classes which is annotated with @GuiceManaged as a webservice with Endpoint.publish().

    private void initGuicedWebservices(final String packageNamePrefix) 
            throws Exception {
        final Reflections reflections = new Reflections(packageNamePrefix);
        final Set> guiceManaged = 
            reflections.getTypesAnnotatedWith(GuiceManaged.class);
        for (final Class clazz : guiceManaged) {
            doGuice(clazz);
        }
    }
    
    private void doGuice(final Class clazz) throws Exception {
        final GuiceManaged guiceManagedAnnotation = 
            clazz.getAnnotation(GuiceManaged.class);
    
        final Injector injector = createInjector(guiceManagedAnnotation);
    
        final Object serviceObject = clazz.newInstance();
        injector.injectMembers(serviceObject);
    
        final String address = guiceManagedAnnotation.address();
    
        Endpoint.publish(address, serviceObject);
    }
    
    private Injector createInjector(final GuiceManaged guiceManagedAnnotation) 
            throws Exception {
        final Class[] moduleClasses = 
            guiceManagedAnnotation.module();
    
        final List moduleInstances = new ArrayList();
        for (final Class moduleClass : moduleClasses) {
            moduleInstances.add(moduleClass.newInstance());
        }
    
        return Guice.createInjector(moduleInstances);
    }
    

    The GuiceManaged annotation:

    @Retention(RUNTIME)
    @Target(TYPE)
    @Documented
    public @interface GuiceManaged {
        public Class[] module();
        public String address();
    }
    

    And the HelloServiceImpl snippet:

    @GuiceManaged(module = HelloModule.class, 
        address = "http://localhost:8080/helloService")
    @WebService
    public class HelloServiceImpl implements HelloService {
    
        @Inject // bound in HelloModule
        public GreetingsService greetingsService;
    
        @Override
        @WebMethod
        public String sayHello(final String name) {
            return greetingsService.sayHello(name);
        }
    }
    

提交回复
热议问题