【问题标题】:@Autowired not working with resteasy@Autowired 不适用于 resteasy
【发布时间】:2014-10-29 21:12:03
【问题描述】:

我知道以前有人问过这个问题,但是我已经根据提供的所有答案检查了这段代码,但仍然看不出它有什么问题......我的 @Autowired 字段根本没有被注入(即它们为空),这是我的设置:

  • 雄猫 7
  • RestEasy 3.0.8
  • Spring 框架 4.0.6
  • Java 8

pom.xml(相关依赖)

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxrs</artifactId>
        <version>3.0.8.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jaxb-provider</artifactId>
        <version>3.0.8.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-jettison-provider</artifactId>
        <version>3.0.8.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>jaxrs-api</artifactId>
        <version>3.0.8.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-spring</artifactId>
        <version>3.0.8.Final</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>

web.xml

<!--  <context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>true</param-value>
</context-param> -->

<context-param>
    <param-name>resteasy.resources</param-name>
    <param-value>com.reporting.rest.MyRestResource</param-value>
</context-param>

<!-- this need same with resteasy servlet url-pattern -->
<context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/reporting</param-value>
</context-param>

<!--  Must come before any other listener -->
<listener>
    <listener-class>
        org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>

<servlet>
    <servlet-name>resteasy-servlet</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
    </servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>resteasy-servlet</servlet-name>
    <url-pattern>/reporting/*</url-pattern>
</servlet-mapping>  

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/application-context.xml</param-value>
</context-param>

<listener>
    <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener
    </listener-class>
</listener>    

应用程序上下文.xml

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="
  http://www.springframework.org/schema/beans     
  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.reporting" />

</beans>

最后是我的 Java 类:

package com.reporting.rest;

@Path("/events")
@Produces("application/json")
public class MyRestResource {
....
@Autowired IProductionEventService productionEventService;
....
}

我也尝试用 @Component 注释类本身,但没有帮助。

提前致谢!

【问题讨论】:

  • 你们是如何整合这两个框架的?他们不会被神奇地捆绑起来。
  • @Luiggi - 从我看到的例子来看,这个依赖应该做: org.jboss.resteasyresteasy-spring 3.0.8.Final
  • 这只是将 jar 复制到您的项目库中。这就像把生番茄和一整颗生菜放在盘子里,然后称之为沙拉......请按照本教程进行操作:mkyong.com/webservices/jax-rs/…
  • @chrylis 这不是您提到的问题的重复。我没有在代码中的任何地方使用 new 。谢谢。
  • @Luiggi 很有趣,但我认为您没有完全阅读我的问题。我正在使用带有 context:component-scan 的注释配置来查找我的注释类,而您告诉我遵循的示例在 xml 中声明了 bean。该示例还仅显示了如何使用 ApplicationContext 查看容器上下文并获取 bean,这很好(实际上我正在这样做),但对于我的问题来说不是必需的。最后,pom 依赖和 web.xml 是一样的(除了版本)。

标签: java spring resteasy autowired component-scan


【解决方案1】:

您不能直接将 autowire 与 resteasy 资源一起使用。你应该告诉 spring,特定的类将使用自动装配。文档可以在here找到。

您可以使用以下代码来做到这一点。

@Component
@Path("/trn")
public class TrnResource extends SpringBeanAutowiringSupport {

}

pom.xml,你应该有以下依赖关系,

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring-framework.version}</version>
    </dependency>     

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>${spring-framework.version}</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>  

【讨论】:

  • 很抱歉,但事实并非如此。 Resteasy 知道以多种方式查找带注释的资源 - 通过 resteasy.scan 属性或直接在 web.xml 中列出您的资源。
  • @dhalia - 是的,resteasy 知道,如何找到注释,而不是弹簧注释。顺便说一句,如果您想在 resteasy 资源中自动装配,这是最简单的方法。
【解决方案2】:

终于找到问题了,貌似resteasy到spring 4集成有问题-https://issues.jboss.org/browse/RESTEASY-1012

我降级了 spring 版本并且它工作正常。希望这可以帮助其他人节省时间!

【讨论】:

  • 您确实为我节省了一些时间,谢谢。一些额外的信息:我已经降级到最新的 3.x 版本。就我而言,它仅在我使用 @Component 和 @Path 注释其余服务类时才有效。
猜你喜欢
  • 1970-01-01
  • 2012-12-20
  • 2014-07-16
  • 1970-01-01
  • 2015-03-29
  • 2013-11-07
  • 1970-01-01
  • 2017-06-25
  • 1970-01-01
相关资源
最近更新 更多