gpt4 book ai didi

java - @Autowired 不适用于 resteasy

转载 作者:行者123 更新时间:2023-11-29 07:46:03 25 4
gpt4 key购买 nike

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

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

网络.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 注释类本身,但没有帮助。

提前致谢!

最佳答案

您不能直接对 resteasy 资源使用 Autowiring 。你应该告诉 spring,特定的类将使用 Autowiring 。可以找到文档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>

关于java - @Autowired 不适用于 resteasy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25677003/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com