简体   繁体   English

Autowired 为空且不适用于 Jersey + Spring

[英]Autowired is null and not working with Jersey + Spring

I have problem figuring out why My Jersey RESTful entry point can't find the Spring Bean that I configure when the app server starts.我无法弄清楚为什么 My Jersey RESTful 入口点找不到我在应用服务器启动时配置的 Spring Bean。 It kept getting NullPointerException after trying from尝试后,它不断收到 NullPointerException

Web.xml网页.xml

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener> 

<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.testing.resource</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-serlvet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Spring-context.xml弹簧上下文.xml

<context:annotation-config />
<context:component-scan base-package="com.testing.config, com.testing.repository, com.testing.workflow" />

<bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>classpath:jdbc.properties</value>
    </property>
</bean>

Jersey servlet entry point Jersey servlet 入口点

@Component
@Produces(MediaType.APPLICATION_JSON)
@Path("/{userId}/items")
public class UserResource
{
  @Autowired
  private UserWorkFlow userWorkFlow;

  @GET
  public Response getAllItems(@PathParam("userId") String userId)
  {
    // ** NullPointerException here complains about the UserWorkFlow 
    return Response.status(200).entity(userWorkFlow.getItemsFor(userId)).build();
  }
}

Service layer服务层

I also tried to make an interface for this but it didn't work.我也尝试为此制作一个界面,但没有用。

@Service
public class UserWorkFlow
{
  @Autowired
  private AllItems allItems;

  public List<Item> getItemsFor(String id)
  {
    return allItems.getItemsFor(id);
  }
}

Repository and DAO存储库和 DAO

@Repository
public class AllItems
{
  @Autowired
  private ItemSql itemSql;

  public List<Item> getItemsFor(String id)
  {
    return itemSql.getAllItemsFor(id);
  }
}

MyBatis Mapper (this has a XML associated with it) MyBatis Mapper(它有一个与之关联的 XML)

public interface UserSql
{
  List<Item> getAllItemsFor(@Param("userId") String userId);
} 

I also changed to com.sun.jersey from org.glassfish.jersey but didn't work.我也从org.glassfish.jersey更改为com.sun.jersey但没有用。 I am running out of ideas what could be wrong.我想不出什么可能是错的。 Can anyone spot what did I do wrong ?谁能发现我做错了什么?

The link I provided for your previous question had links to four fully working examples.链接提供给你我刚才的问题有联系四个完全工作的例子。 You could have easily just grabbed one of the examples and built on top of it.您可以轻松地抓住其中一个示例并在其基础上进行构建。

I will just walk you through one of the examples.我将向您介绍其中一个示例。 Maybe it was too hard to follow.也许太难了。 I will use the Jersey 2.x with Spring XML config .我将使用Jersey 2.x 和 Spring XML 配置

First , make sure you have the dependencies (only showing versions to ones not shown in the pom)首先,确保您有依赖项(仅显示未在 pom 中显示的版本)

  • jersey-container-servlet: 2.22.1
  • spring-web: 3.2.3.RELEASE
  • commons-logging
  • jersey-spring3: 2.22.1 . jersey-spring3: 2.22.1 (Notice the snapshot project uses jersey-spring*4* . This is not yet released, and will be released in the next Jersey release) (注意快照项目使用jersey-spring*4* 。这个还没有发布,将在下一个 Jersey 版本中发布)

Second , make sure your web.xml is in order其次,确保您的web.xml井井有条

Third , add your applicationContext.xml to the project class-path.第三,将applicationContext.xml添加到项目类路径。

Fouth , the MyApplication class listed in the previous web.xml.第四MyApplication类在前面的web.xml 中列出。

If you follow the example to the T, you will have a working example.如果您遵循 T 的示例,您将拥有一个工作示例。 It may not be the exact way you want to configure your Spring components, but you will have a working example you can build off of and tweak around to see what works and what doesn't.这可能不是您想要配置 Spring 组件的确切方式,但是您将拥有一个工作示例,您可以在此基础上构建和调整以查看哪些有效,哪些无效。 When you get more comfortable, you can see the other examples (in the first link of this answer) for other configuration styles/options.当您感觉更舒服时,您可以查看其他配置样式/选项的其他示例(在此答案的第一个链接中)。

我使用 SpringBeanAutowiringSupport 扩展了 ServiceImpl 和 DAOImpl 类,它解决了我的自动装配空指针异常。

Here you can create instance of that service and access the methods of the service.您可以在此处创建该服务的实例并访问该服务的方法。 I think this is the simplest way.我认为这是最简单的方法。

UserService user = new UserService();
user.saveUser();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM