RESTEasy Springの統合の例
ここではSpring Beanを
JBoss RESTEasy
に挿入する2つの一般的な方法を示します。以下のソリューションは、ほとんどのWebフレームワークとJAX-RS実装でも機能するはずです。
-
WebApplicationContextUtils
ServletContextを使用します. -
ApplicationContextAwareのintrefaces
を実装するクラスを作成します.
シングルトンパターンを推奨します。
このチュートリアルでは、
RESTEasy 2.2.1.GA
と
Spring 3.0.5.RELEASE
を統合しています。
プロジェクトの依存関係
多くの依存関係はなく、Mavenの `pom.xml`ファイルにRESTEasyとSpring coreを宣言するだけです。
<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.春の豆
”
customerBo
“という名前の単純なSpring Beanは、後でSpringを介してRESTEasyサービスにこのBeanを注入します。
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"; } }
File: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
最初の解決策は、Springアプリケーションコンテキストを取得するために
ServletContext
とWebApplicationContextUtilsを取得するためにJAX-RSの `@Context`を使用し、この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を実装する
2番目の解決策は、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コンテナはこのクラスを認識しません。
File: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サービスでは、新しいSingletonクラス “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”を追加します。
File: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の両方が同じ出力を生成します:
-
注意** また、このhttp://docs.jboss.org/resteasy/docs/2.2.1.GA/userguide/html/RESTEasy
Spring
Integration.html%0A[RESTEasy Spring guide]を理解している方、またはより良い解決策がある場合は、それを私に教えてください。
ソースコードをダウンロードする
ダウンロードする –
RESTEasyt-Spring-Integration-Example.zip
(9 KB)
参考文献
レガシーコードからのスプリングビーン]。
General
Springと他のフレームワークを統合する方法]。
http://docs.jboss.org/resteasy/docs/2.2.1.GA/userguide/html/RESTEasy
Spring
Integration.html[Better
何よりもRESTEasy Springの例]。
http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/context/ApplicationContextAware.html
[ApplicationContextAware
JavaDoc]。
http://ja.wikipedia.org/wiki/Singleton__pattern#Java
[Wikiシングルトン
Springアプリケーションコンテキスト]