【发布时间】: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.resteasy resteasy-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