注意
关于集成Spring的官方RESTEasy指南,没有太多评论,文档需要改进,因为对于其他人来说,它太“ 抽象 ”了(至少对我而言:))。
在这里,我们向您展示了将Spring bean注入JBoss RESTEasy的两种一般方法,下面的解决方案也应适用于大多数Web框架和JAX-RS实现。
- 使用WebApplicationContextUtils + ServletContext。
- 创建一个类来实现ApplicationContextAware intrefaces ,建议使用单例模式。
在本教程中,我们将RESTEasy 2.2.1.GA与Spring 3.0.5.RELEASE集成在一起 。
1.项目依赖
依赖关系不多,只需在您的Maven pom.xml
文件中声明RESTEasy和Spring核心。
<repositories>
<repository>
<id>JBoss repository</id>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>
</repositories>
<dependencies>
<!-- JBoss RESTEasy -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.2.1.GA</version>
</dependency>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<!-- need for solution 1, if your container don't have this -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
</dependency>
</dependencies>
2.Spring Bean
一个简单的Spring bean,名为“ customerBo ”,稍后您将通过Spring将其注入RESTEasy服务。
package com.mkyong.customer;
public interface CustomerBo{
String getMsg();
}
package com.mkyong.customer.impl;
import com.mkyong.customer.CustomerBo;
public class CustomerBoImpl implements CustomerBo {
public String getMsg() {
return "RESTEasy + Spring example";
}
}
文件:applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
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 ">
<bean id="customerBo" class="com.mkyong.customer.impl.CustomerBoImpl">
</bean>
</beans>
3.1 WebApplicationContextUtils + ServletContext
第一个解决方案是使用JAX-RS @Context
获取ServletContext ,并使用WebApplicationContextUtils
获取Spring应用程序上下文,通过此Spring应用程序上下文,您可以访问Spring容器并从中获取bean。
package com.mkyong.rest;
import javax.servlet.ServletContext;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.mkyong.customer.CustomerBo;
@Path("/customer")
public class PrintService {
CustomerBo customerBo;
@GET
@Path("/print")
public Response printMessage(@Context ServletContext servletContext) {
//get Spring application context
ApplicationContext ctx =
WebApplicationContextUtils.getWebApplicationContext(servletContext);
customerBo= ctx.getBean("customerBo",CustomerBo.class);
String result = customerBo.getMsg();
return Response.status(200).entity(result).build();
}
}
3.2实施ApplicationContextAware
第二种解决方案是创建一个实现Spring ApplicationContextAware
的类,并使其成为单例,以防止从其他类实例化。
package com.mkyong.context;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringApplicationContext implements ApplicationContextAware {
private static ApplicationContext appContext;
// Private constructor prevents instantiation from other classes
private SpringApplicationContext() {}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
appContext = applicationContext;
}
public static Object getBean(String beanName) {
return appContext.getBean(beanName);
}
}
请记住注册此bean,否则,Spring容器将不会意识到此类。
文件:applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
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 ">
<bean class="com.mkyong.context.SpringApplicationContext"></bean>
<bean id="customerBo" class="com.mkyong.customer.impl.CustomerBoImpl">
</bean>
</beans>
在REST服务中,可以使用新的单例类“ SpringApplicationContext
”从Spring容器中获取bean。
package com.mkyong.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import com.mkyong.context.SpringApplicationContext;
import com.mkyong.customer.CustomerBo;
@Path("/customer")
public class PrintService {
CustomerBo customerBo;
@GET
@Path("/print")
public Response printMessage() {
customerBo = (CustomerBo) SpringApplicationContext.getBean("customerBo");
String result = customerBo.getMsg();
return Response.status(200).entity(result).build();
}
}
4.将RESTEasy与Spring集成
要将两者集成到Web应用程序中,请在web.xml中添加Spring“ ContextLoaderListener
”。
文件:web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>
<context-param>
<param-name>resteasy.resources</param-name>
<param-value>com.mkyong.rest.PrintService</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</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>/*</url-pattern>
</servlet-mapping>
</web-app>
5.演示
解决方案1和2都将产生以下相同的输出:
注意
同样,如果您了解此RESTEasy Spring指南或有更好的解决方案,请与我分享。
下载源代码
下载它– RESTEasyt-Spring-Integration-Example.zip (9 KB)
参考文献
- 从遗留代码访问Spring Bean
- 将Spring与其他框架集成的一般方法
- 总比没有好RESTEasy + Spring示例
- ApplicationContextAware JavaDoc
- Java中的Wiki Singleton模式
- 获取Spring Application上下文
翻译自: https://mkyong.com/webservices/jax-rs/resteasy-spring-integration-example/