4.web
Next is another bundle of this demo. And it is a web-bundle (WAB with spring-mvc capability). Let's take a look at the structure first.
First look at web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>Search Web Module</display-name> <servlet> <servlet-name>search</servlet-name> <servlet-class>org.phantom.web.virgo.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextClass</param-name> <param-value>org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>search</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
In this demo, we are using Spring-MVC, so Spring-MVC support is added here. A custom extension class org.phantom.web.virgo.servlet.DispatcherServlet is used here . Explain the role of this class. In OSGI, each bundle is independent, it has an independent ClassLoad, and an independent Spring ApplicationContext. But we want to get the services of another bundle from one bundle through spring, that is, we need these applicationContexts to know each other. How to do it What? virgo supports this. It provides a class org.eclipse.virgo.web.dm.ServerOsgiBundleXmlWebApplicationContext . This class is equivalent to an OSGI global applicationContext. We are here to inject this class into Spring-MVC In the DispatcherServlet. This is achieved by extending the default DispatcherServlet
public class DispatcherServlet extends org.springframework.web.servlet.DispatcherServlet{ @Override public void init(ServletConfig config) throws ServletException { String contextClass = config.getInitParameter("contextClass"); if (contextClass != null) { try { setContextClass(Class.forName(contextClass)); } catch (ClassNotFoundException e) { throw new ServletException(String.format("Context class %s not found", contextClass), e); } } super.init(config); } }Then take a look at the OSGI description.
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: search web module Bundle-SymbolicName: org.phantom.demo.web Bundle-Version: 1.0.0.SNAPSHOT Import-Template: org.springframework.*;version="[3.0.5,4)" Import-Package: org.springframework.context.config;version="[3.0.5,4)", org.springframework.web.servlet.config;version="[3.0.5,4)" Excluded-Imports: org.phantom.demo.web Snap-Host: org.phantom.demo.host;version="1.0.0.SNAPSHOT" Snap-ContextPath: /searchHere we will introduce Snap-ContextPath:/search. Host-Snap has been introduced before. This configuration is to configure the request path of the current bundle, that is, the second-level path. There is also a Snap-Host: org.org.phantom.demo. host. This configuration mounts the current snap to a host. Therefore, according to the previous introduction, when entering /demo, the SnapHostFilter starts to work, gets the second level /search of the request, and distributes it to the current bundle. The configuration below is the configuration of Spring-MVC. Create search-servlet.xml with the same name as DispatcherServlet in WEB-INF/
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/utils" xmlns:osgi="http://www.springframework.org/schema/osgi" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi-1.2.xsd"> <context:component-scan base-package="org.phantom.demo.web" /> <mvc:annotation-driven/> <mvc:resources location="/" mapping="*.html"/> <mvc:resources location="/resources/" mapping="/resources/**" /> <osgi:reference id="pictureSearch" interface="org.phantom.demo.api.SearchHandler" bean-name="pictureSearch"/> </beans>Explain sentence by sentence. The first sentence, open the package scan, and add the Controller to the Spring management
<context:component-scan base-package="org.phantom.demo.web" />