本文的目的是:更好的处理Spring Bean和RESTEasy Resource之间的关系
思路有两种:
- RESTEasy自己管理Resource的生命周期,在Resource生成时将需要的Spring bean手动注入进来
- 依靠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
注解,为什么要这样呢?
相关推荐
Spring整合RestEasy示例工程源码,Spring+Jetty+RestEasy
Resteasy + Spring + Netty sample Inject resteasy provider / controllers as spring bean Authentication Run at Main.java Test http://localhost:8082/resteasy/hello/world 教程 jax-rs规范用法: ...
另外,此RESTEasy Spring Boot启动程序将按预期方式与Spring集成,这意味着每个也是Spring Bean的JAX-RS REST资源都将被自动自动扫描,集成和可用。 产品特点 为Spring Boot应用程序启用RESTEasy 作为Spring bean...
resteasy-spring-netty Resteasy + Spring + Netty sample Inject resteasy provider / controllers as spring bean Authentication ===================== Run at Main.java Test 教程 jax-rs规范用法: resteasy ...
resteasy
POC 网址 Resteasy Jaxrs 1.0 Spring
利用RestEasy和Jetty服务器搭建轻量级的RESTful web service的例程
该项目为 Spring Boot 应用程序提供 RESTEasy 自动配置。入门构建此项目并将其安装到您的 Maven 存储库中: $ mvn install然后,您应该在应用程序的build.gradle或pom.xml添加对org.springframework.boot:spring-...
春假Spring + Resteasy + Maven + JUnit + Mockito + AngularJS + Karma + Jasmine + Wildfly POC 这是一个使用上述技术的 POC。 该项目已使用 Maven 构建,并设置为在 Wildfly 8.x 中运行。 要对其进行测试,请配置...
另外,此RESTEasy Spring Boot启动程序将按预期方式与Spring集成,这意味着每个也是Spring Bean的JAX-RS REST资源都将被自动自动扫描,集成和可用。 特征 为Spring Boot应用程序启用RESTEasy 作为Spring bean支持...
Jboss EAP 6(Jboss 7)RestEasy 3.09 Spring 3.2.5 Swagger 2.0与Web.xml和Bean配置的Swagger UI集成 为此,您必须将RestEasy安装升级到3.09 说明来自: : Resteasy与JBoss AS 7捆绑在一起。您可能需要在AS7中...
resteasy讲解
宁静的春天 Resteasy + Spring + Netty示例 作为spring bean注入resteasy提供者/控制器 验证 ==================== 在Main.java中运行 测试
resteasy简单demo使用的jar包
resteasy入门级文档、如何使用Restful进行WebService的开发简介及相关RestEasy相关资源的链接
RESTEasy入门经典.docx
详细描述了restEasy在servlet3.0前后的不同使用方法;restEasy 结合httpServer 无需容器开发;restEasy的通用差错拦截;附详细实现源码!
resteasy不使用tomcat使用netty实现,实现了高性能,也可以不需要tomcat的容器,一个主函数就能执行
该资源是java restful的应用框架resteasy---实现了jax-rs规范的一个demo,如有疑问请联系caizhh2009@gmail.com
resteasy-jaxrs-2.3.2框架源码。希望您了解Resteasy框架有所帮助