3

我正在尝试整合 Resteasy 和 Spring;我已经关注了 Resteasy 的文档和这篇文章:Inject Spring beans into RestEasy。我让它在其余类上使用@Autowire 或其他 Spring 注释工作,但我想这样做使我的其余类不受 Spring(或 DI)依赖项的影响。我也想只通过java配置来配置spring。在 spring 配置中我添加了这个:

<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>

当然我在 web.xml 中有,所以 SpringContextLoaderListener 会获取 spring 配置:

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

删除 @Autowire 注释,如果我还删除了第一个包(我通过 Spring java config 配置了注入),则不会发生注入并且字段保持为空;如果我删除第二个包,resteasy 无法识别其余类的 url。

我想只在 Spring 配置中配置注入,有没有办法让 resteasy 识别来自外部配置的 spring bean 的路径?

编辑:我注意到我正在尝试使用 @Provider 带注释的类,前提是您正确配置了 Spring:

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

但谜团比我最初想象的还要深……我更有信心,我走在了正确的轨道上,只是错过了一步!

4

2 回答 2

1

更好的方法是使用 JSR-330 注释。

而不是@Autowired,更喜欢使用@Inject. Spring 支持 JSR-330 注释,并将在后台使用 Autowire 实现。对于使用 , 注释的 Spring bean @Component@Service只需将注释替换为 JSR-330 特定的@Named.

如果您使用的是 maven,只需在pom.xml文件中包含以下内容。

<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>
于 2013-10-15T03:03:15.930 回答
0

使用 JSR-250 的 @Resource 代替 @Autowired 是向前迈出的一大步;它可以满足@Autowired 的需求,它是 JSR 而不是 Spring 特定的。

对于许多用途来说可能已经足够好了。

于 2013-01-29T10:09:21.070 回答