3
votes

I am trying to integrate Resteasy and Spring; I have followed both the docs for Resteasy and this post: Inject Spring beans into RestEasy . I have it working using @Autowire or other Spring annotations on the rest class, but I would like to do this keeping my rest classes free from spring (or DI) dependencies. I also would like to configure spring only through java configuration. In spring configuration I added this:

<context:component-scan base-package="package.where.spring.configuration.beans.are , package.where.rest.classes.are">
<context:include-filter type="annotation" expression="javax.ws.rs.Path"/>
</context:component-scan>

and of course I have in web.xml, so that spring config is picked up by SpringContextLoaderListener:

<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:/spring-config.xml</param-value>
</context-param>

Removing the @Autowire annotations, if I also remove the first package (where I configured the injections through Spring java config) no injection takes place and the fields remain null; if I remove the second package the urls for the rest classes are not recognized by resteasy.

I would like to configure the injections just in Spring configuration, is there a way to have resteasy recognize the paths from spring beans which are configured externally?

EDIT: I noticed that what I am trying to do works with @Provider annotated classes, given that you configure Spring correctly:

 <context:component-scan base-package="my.package1 , my.package2">
    <context:include-filter type="annotation" expression="javax.ws.rs.ext.Provider"/>
 </context:component-scan>

But the mistery is deeper than I first thought... I am more confident that I am on the right track and just missed a step!

2

2 Answers

1
votes

A much better approach is to use JSR-330 annotations.

Instead of @Autowired, prefer using @Inject. Spring supports JSR-330 annotations and will use Autowire implementation under the covers. For your Spring beans annotated with @Component, @Service, simply replace the annotation with JSR-330 specific @Named.

If you're using maven, simply include the following in your pom.xml file.

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>
0
votes

Using JSR-250's @Resource instead of @Autowired is a big step forward; it does what is needed from @Autowired and it is JSR and not Spring specific.

Could be good enough for many uses.