JAX-RS之resteasy跟spring整合

来源:互联网 发布:mac 触摸板 鼠标 消失 编辑:程序博客网 时间:2024/06/07 05:22
其实,在JAX-RS标准下,jboss的resteasy跟spring结合的话,无非是如何去取得
spring中的bean而已.两个方法,例子如下

1 比如有个接口和实现类
Java代码 复制代码
  1. public interface CustomerBo{   
  2.     
  3.     String getMsg();   
  4.     
  5. }  


  实现类
  
Java代码 复制代码
  1. public class CustomerBoImpl implements CustomerBo {   
  2.     
  3.     public String getMsg() {   
  4.     
  5.         return "RESTEasy + Spring example";   
  6.     
  7.     }  


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>


之后用WebApplicationContextUtils + ServletContext
引用

@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();

}


 
第2种方法,自己写类,实现ApplicationContextAware ,当然这个类要单例
 
Java代码 复制代码
  1. import org.springframework.beans.BeansException;   
  2. import org.springframework.context.ApplicationContext;   
  3. import org.springframework.context.ApplicationContextAware;   
  4.     
  5. public class SpringApplicationContext implements ApplicationContextAware {   
  6.     
  7.     private static ApplicationContext appContext;   
  8.     
  9.     // Private constructor prevents instantiation from other classes  
  10.         private SpringApplicationContext() {}   
  11.     
  12.     @Override  
  13.     public void setApplicationContext(ApplicationContext applicationContext)   
  14.             throws BeansException {   
  15.         appContext = applicationContext;   
  16.     
  17.     }   
  18.     
  19.     public static Object getBean(String beanName) {   
  20.         return appContext.getBean(beanName);   
  21.     }   
  22.     
  23. }  

  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>

使用:
 
Java代码 复制代码
  1. import javax.ws.rs.GET;   
  2. import javax.ws.rs.Path;   
  3. import javax.ws.rs.core.Response;   
  4. import com.mkyong.context.SpringApplicationContext;   
  5. import com.mkyong.customer.CustomerBo;   
  6.     
  7. @Path("/customer")   
  8. public class PrintService {   
  9.     
  10.     CustomerBo customerBo;   
  11.     
  12.     @GET  
  13.     @Path("/print")   
  14.     public Response printMessage() {   
  15.     
  16.         customerBo = (CustomerBo) SpringApplicationContext.getBean("customerBo");   
  17.     
  18.         String result = customerBo.getMsg();   
  19.     
  20.         return Response.status(200).entity(result).build();   
  21.     
  22.     }   
  23.     
  24. }  


    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>
原创粉丝点击