开发者

CXF and Google Guice using JAX-RS + JAX-WS

开发者 https://www.devze.com 2023-03-11 03:45 出处:网络
I would like to integrate CXF with Google Guice. I am already using Guice in my project and I want to avoid adding extra dependencies.

I would like to integrate CXF with Google Guice. I am already using Guice in my project and I want to avoid adding extra dependencies.

CXF was my choice because one of the requirements is to be able to provide XML, JSON, JSONP and SOAP interface to the users of the services without having duplic开发者_JAVA技巧ate code (right now we have SOAP-specific classes, for XML we use Struts and for JSON we wrote our own parsers, I know, I feel dirty too).

Anyway, afaik, CXF can fulfill this requirement, so it seems I'm stuck with CXF.

Any ideas or pointers or advice on how to integrate Guice with CXF? I thought of extending the CXFNonSpringJaxrsServlet class and hack Guice into it, but it somehow seems like I would be doing something that someone else has already done.


With CXF 2.4.x, CXF no longer uses Spring to internally configure itself. Thus, you can do pretty much anything without Spring.

The configuration parts are definitely an issue. However, all the spring configuration stuff is just thin wrappers over the CXF API's. Thus, you can configure pretty much everything via the API's. You may just need to dig a little bit more.

As an example, with CXF 2.4, we now have started to support using Blueprint instead of Spring in OSGi. The blueprint support doesn't require any of the Spring stuff, but is modelled from it.


Here is what I have been using (cxf-2.5.0).

@Singleton
public class WebServiceServlet extends CXFNonSpringServlet {
    private static final long serialVersionUID = 1L;
    private final SamaService samaService;

    @Inject
    public WebServiceServlet(SamaService samaService) {
        this.samaService = samaService;
    }

    @Override
    protected void loadBus(ServletConfig sc) {
        super.loadBus(sc);
        BusFactory.setDefaultBus(getBus());
        Endpoint.publish("/Data.asmx", samaService);
    }
}


You've got a problem. CXF is thoroughly dependent on Spring, due to it being internally configured by Spring and using Spring's facilities quite extensively. As such, I don't think it's practical to try to prise CXF out of Spring just so you can use Guice instead; at a minimum, you'd have to replicate all the configuration with custom Guice providers and it still will not work (parts of CXF use some of Spring's Web layer).

You could try using Spring's JavaConfig style for your code instead. That's mostly similar to the sorts of things that Guice does (as far as I can see). Or I suppose you could try using two separate DI frameworks in the same app… though that feels to me like an opportunity for “hilarious” failure.

0

精彩评论

暂无评论...
验证码 换一张
取 消