Hier zeigen wir Ihnen zwei allgemeine Möglichkeiten, Spring Bean in JBoss RESTEasy zu injizieren. Die folgenden Lösungen sollten auf den meisten Web-Frameworks und JAX-RS-Implementierungen funktionieren.
-
Verwenden Sie WebApplicationContextUtils + ServletContext.
-
Erstellen Sie eine Klasse, um ApplicationContextAware-Schnittstellen mit zu implementieren
Singleton-Pattern wird empfohlen.
In diesem Tutorial integrieren wir RESTEasy 2.2.1.GA mit Spring 3.0.5.RELEASE .
1. Projektabhängigkeiten
Nicht viele Abhängigkeiten, deklarieren Sie einfach den RESTEasy- und den Spring-Kern in Ihrer Maven-Datei
pom.xml
.
<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
Eine einfache Frühlingsbohne mit dem Namen " customerBo ", die Sie später über Spring in den RESTEasy-Dienst injizieren.
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"; } }
Datei: 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
Die erste Lösung ist JAX-RS
@ Context
, um den
ServletContext
und` WebApplicationContextUtils` den Spring-Anwendungskontext abzurufen. Mit diesem Spring-Anwendungskontext können Sie Beans aus dem Spring-Container abrufen.
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 Implementieren Sie ApplicationContextAware
Die zweite Lösung besteht darin, eine Klasse zu implementieren, die Spring
ApplicationContextAware
implementiert, und sie als Singleton definieren, um die Instantiierung von anderen Klassen zu verhindern.
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); } }
Denken Sie daran, diese Bean zu registrieren, andernfalls wird der Spring-Container diese Klasse nicht kennen.
Datei: 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>
Im REST-Dienst können Sie die neue Singleton-Klasse "SpringApplicationContext" verwenden, um die Bean aus dem Spring-Container abzurufen.
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. Integrieren Sie RESTEasy mit Spring
Um beide in eine Webanwendung zu integrieren, fügen Sie Spring "ContextLoaderListener" in Ihre web.xml hinzu.
Datei: 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. Demo
Lösung 1 und 2 erzeugen die gleiche Ausgabe:
-
Hinweis ** + Wenn Sie diesen http://docs.jboss.org/resteasy/docs/2.2.1.GA/userguide/html/RESTEasy Spring Integration.html%0A[RESTEasy Spring-Leitfaden]verstanden haben oder eine bessere Lösung haben Bitte teile es mir mit.
Quellcode herunterladen
Download it - RESTEasyt-Spring-Integration-Example.zip (9 KB)
Referenzen
Frühlingsbohnen aus altem Code]. link://spring/spring-how-to-do-abhängigkeit-einspritzung in ihren sitzungslistener/[Allgemein
Weg, Frühling mit anderen Rahmen zu integrieren]. http://docs.jboss.org/resteasy/docs/2.2.1.GA/userguide/html/RESTEasy Spring Integration.html[Besser
als nichts RESTEasy Spring Beispiel]. http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/context/ApplicationContextAware.htmlglich__AnwendungContextAware
JavaDoc]. http://en.wikipedia.org/wiki/Singleton__pattern#Java [Wiki Singleton
Spring Application Kontext]