---
title: "Spring整合RESTEasy两种思路"
author: "quainter"
date: "2016-12-28T12:12:57Z"
tags: ["Spring", "resteasy"]
categories: ["开源软件"]
slug: "spring-resteasy"
draft: false
lastmod: "2016-12-28T12:16:07Z"
id: 2347801
url: "https://quainter.iteye.com/blog/2347801"
---

> *RESTEasy 项目是 JAX-RS 的一个实现*   
> [官网地址](http://resteasy.jboss.org/)   
> [官方文档](http://docs.jboss.org/resteasy/docs/3.1.0.Final/userguide/html_single/index.html)

***本文的目的*是：更好的处理Spring Bean和RESTEasy Resource之间的关系** 

思路有两种：

1. RESTEasy自己管理Resource的生命周期，在Resource生成时将需要的Spring bean手动注入进来
2. 依靠Spring容器来管理Resource，将Spring的Bean当做Resource发布出来

下面比较详细的解释下：

### （一）RESTEasy自己管理Resource的生命周期

具体思路是Resource层代码不使用Spring管理，在Resource的构造方法中手动注入需要的Spring Bean（比如Service方法等），上代码

```
@Path("book")
public class BookResource{

    private BookService bookService;

    public BookResource(){
        BookService bookSerivce = (BookService)YOUR_SPRING_CONTEXT.getBean("bookService");
        this.bookService = bookService;
    }

    @Path("{bookId:[0-9]*}")
    @Get
    @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
    @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
    public Book getBookById(@PathParam("bookId")final Integer bookId){
        final Book book = this.bookService.getBookById(bookId);
        return book;
    }

}
```

 

RESTEasy是在接收到请求的时候通过反射利用构造方法生成Resource实例的，所以这里我们重写了Resource的构造方法。在初始化的时候手动去SpringContext中取到需要的bean，将其注入进来，这样就实现了在Resource中调用Service方法。

### （二）依靠Spring容器来管理Resource

先看看下面是*chapter45.spring integration*中web.xml中需要的配置：

```
<web-app>
    <listener>
        <listener-class>
            org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
        </listener-class>
    </listener>

    <!-- ** INSERT YOUR LISTENERS HERE!!!! -->

    <!-- resteasy自定义的spring容器 -->
    <listener>
        <listener-class>
            org.jboss.resteasy.plugins.spring.SpringContextLoaderListener
        </listener-class>
    </listener>

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

    <servlet-mapping>
        <servlet-name>Resteasy</servlet-name>
        <url-pattern>/Resteasy/*</url-pattern>  
    </servlet-mapping>

</web-app>
```

 

可以看出上面配置了resteasy自定义的SpringContextLoaderListener。

这样做的局限是：由于SpringContext只能存在一个，所以下面的spring容器不可以和上面的RESTEasy自定义spring容器共存。

***如果你的项目没有自定义Spring容器，而使用的是原始的Spring容器的话，考虑直接用上面代码中的`org.jboss.resteasy.plugins.spring.SpringContextLoaderListener`替换掉你原来的`org.springframework.web.context.ContextLoaderListener`来解决，就不用往下看了。***

```
<!-- 当前项目自定义的spring容器 -->
<listener>
    <listener-class>
        custom.SpringContextLoaderListener
    </listener-class>
</listener>
```

 

那么解决思路就很明确了。

> **将RESTEasy的spring实现整合到项目自定义的spring实现中**

通过分析`resteasy-spring`源码包发现，RESTEasy的自定义SpringContextLoader代码入口在`org.jboss.resteasy.plugins.spring.SpringContextLoader.java`中，此类代码如下：

```
public class SpringContextLoader extends ContextLoader
{
   private SpringContextLoaderSupport springContextLoaderSupport = new SpringContextLoaderSupport();

   protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext configurableWebApplicationContext)
   {
      super.customizeContext(servletContext, configurableWebApplicationContext);
      this.springContextLoaderSupport.customizeContext(servletContext,configurableWebApplicationContext);
   }
}
```

 

到这里就简单了，我们在自定义的CustomSpringContextLoader中加入上面的代码，OK

```
public class CustomSpringContextLoader extends ContextLoader
{

   <!--  YOUR CODE!!! -->

   private SpringContextLoaderSupport springContextLoaderSupport = new SpringContextLoaderSupport();

   protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext configurableWebApplicationContext)
   {
      super.customizeContext(servletContext, configurableWebApplicationContext);
      this.springContextLoaderSupport.customizeContext(servletContext,configurableWebApplicationContext);
   }
}
```

 

接下来要做的就是通过注解或者配置的方式将BookResource加载到Spring容器。

```
@Component
@Scope("request")
@Path("book")
public class BookResource{

    private BookService bookService;

    public BookResource(){
        BookService bookSerivce = (BookService)YOUR_SPRING_CONTEXT.getBean("bookService");
        this.bookService = bookService;
    }

    @Path("{bookId:[0-9]*}")
    @Get
    @Produces({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
    @Consumes({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
    public Book getBookById(@PathParam("bookId")final Integer bookId){
        final Book book = this.bookService.getBookById(bookId);
        return book;
    }

}
```

 

我这里通过`@Component`来加载的。   
是不是看到了`@Scope`注解,为什么要这样呢？