OSGi Enterprise Application Development (14) integrating spring, MyBatis, spring MVC

Source: Internet
Author: User
Tags httpcontext

As an enterprise-class Web application, the MVC framework is essential. Spring MVC is currently widely used, and this article describes how to implement the integration of the spring, MyBatis, and spring MVC frameworks in OSGi applications, where the integration of spring MVC is difficult because spring is integrated into OSGi, Each bundle has an isolated applicationcontext, which means that dependency injection between the instantiated beans in different bundle is problematic, as mentioned in the previous article, which can be addressed through the bean's registration and referral mechanism.

You also need to specify the bean's configuration file when instantiating the Org.springframework.web.servlet.DispatcherServlet class in the Spring MVC framework, Spring After the MVC framework starts, the bean in the configuration file (for example, the controller instance) is instantiated, and the bean instantiated after the Spring MVC framework is started is a different classloader than the bean instantiated in bundle. So the instantiated bean in bundle cannot be injected into the bean instantiated at startup of the Spring MVC framework, and a more straightforward example is that the sqlsession instance of the MyBatis operation database cannot be injected into the controller of spring MVC. Online more solutions are using Virgo, the author after a study to solve the spring MVC framework and OSGi application integration, if you find any problems, you can explore the next ^_^.

The next step is to introduce the integration of the spring, MyBatis, and spring MVC frameworks in OSGi applications, and we still use the workspace that was already built up in the previous article.

First create a new bundle, the name is Com.csdn.osgi.user.web, the entire work space structure as shown in the following figure:

Since resources and servlet in OSGi applications need to be registered before being accessed by clients, we can write several classes dedicated to registering Web resources.

The first is the image, CSS, JS and other static resources registration, Create a new class in Com.csdn.osgi.user.web this bundle, the name is Com.csdn.osgi.user.web.registry.ResourceRegistry, and the code is as follows:

Package com.csdn.osgi.user.web.registry;
Import Java.util.Iterator;
Import Java.util.Map;

Import Java.util.Set;
Import Org.eclipse.gemini.blueprint.context.BundleContextAware;
Import Org.osgi.framework.BundleContext;
Import org.osgi.framework.ServiceReference;
Import Org.osgi.service.http.HttpContext;
Import Org.osgi.service.http.HttpService;
Import org.osgi.service.http.NamespaceException;
Import org.springframework.beans.BeansException;
Import Org.springframework.context.ApplicationContext;

Import Org.springframework.context.ApplicationContextAware;

    public class Resourceregistry implements Applicationcontextaware, Bundlecontextaware {Map resmapping;

    Bundlecontext Bundlecontext;

    ApplicationContext ApplicationContext;
    @Override public void Setbundlecontext (Bundlecontext bundlecontext) {this.bundlecontext = Bundlecontext; @Override public void Setapplicationcontext (ApplicationContext applicationcontext) throws BeansException {this.applicationcontext = ApplicationContext;
    The public void setresmapping (Map resmapping) {this.resmapping = resmapping; public void init () {ServiceReference servicereference = bundlecontext.getservicereference (httpservice.clas
        S.getname ());
        Httpservice Httpservice = (httpservice) bundlecontext.getservice (servicereference);

        HttpContext commoncontext = Httpservice.createdefaulthttpcontext ();
            try {Set keyset = Resmapping.keyset ();
            Iterator<string> it = Keyset.iterator ();
                while (It.hasnext ()) {String key = It.next ();
                String value = (string) resmapping.get (key);
            Httpservice.registerresources (key, value, Commoncontext);
        } catch (Namespaceexception e) {e.printstacktrace ();
 }
    }
}

We can instantiate this class through spring, and the bean is configured as follows:

    <bean name= "Resourceregistry" class= "Com.csdn.osgi.user.web.registry.ResourceRegistry" init-method= "init" >
        <property name= "resmapping" >
            <map>  
                <entry key= "/js" value= "/webcontent/js  
                "/> <entry key= "/css" value= "/webcontent/css"/> <entry key= "
                /images" value= "/webcontent/images"/>
            </map> 
        </property>
    </bean>

This allows you to complete the registration of static resources, which is flexible, and if you have new resource types, simply add a new entry tag to the map tag.

Next is the JSP registration, we in Com.csdn.osgi.user.web this bundle a new class, the name is Com.csdn.osgi.user.web.registry.JspRegistry, the code is as follows:

Package com.csdn.osgi.user.web.registry;
Import Java.util.Dictionary;

Import java.util.Hashtable;

Import javax.servlet.ServletException;
Import Org.eclipse.equinox.jsp.jasper.JspServlet;
Import Org.eclipse.gemini.blueprint.context.BundleContextAware;
Import Org.osgi.framework.BundleContext;
Import org.osgi.framework.ServiceReference;
Import Org.osgi.service.http.HttpContext;
Import Org.osgi.service.http.HttpService;
Import org.osgi.service.http.NamespaceException;
Import org.springframework.beans.BeansException;
Import Org.springframework.context.ApplicationContext;

Import Org.springframework.context.ApplicationContextAware; public class Jspregistry implements Applicationcontextaware, Bundlecontextaware {bundlecontext Bundlecontex

    T

    ApplicationContext Appcontext;
    @Override public void Setbundlecontext (Bundlecontext bundlecontext) {this.bundlecontext = Bundlecontext; @Override public void Setapplicationcontext (ApplicationContext aPpcontext) throws Beansexception {this.appcontext = Appcontext; public void init () {ServiceReference servicereference = bundlecontext.getservicereference (httpservice.cla
        Ss.getname ());
        Httpservice Httpservice = (httpservice) bundlecontext.getservice (servicereference);


        HttpContext commoncontext = Httpservice.createdefaulthttpcontext ();

        Dictionary initparams = new hashtable<string, string> ();
        Jspservlet Jspservlet = new Jspservlet (Bundlecontext.getbundle (), "/webcontent/web-inf/jsp", "/jsp");
        try {httpservice.registerservlet ("/jsp", Jspservlet, InitParams, Commoncontext);
        catch (Servletexception e) {e.printstacktrace ();
        catch (Namespaceexception e) {e.printstacktrace ();
 }
    }
}

Similarly, the configuration of Jspregistry,bean instantiated in spring is as follows:

<bean name= "Jspregistry" class= "Com.csdn.osgi.user.web.registry.JspRegistry" init-method= "init" >
</ Bean>

Finally, the servlet resource is registered, and with the Spring MVC framework, we need to instantiate a Dispatcherservlet object, and it's important to note that the Dispatcherservlet object cannot be created with the New keyword, Should be instantiated by the spring framework, Create a new class in Com.csdn.osgi.user.web this bundle, the name is Com.csdn.osgi.user.web.registry.DispatcherServletRegistry, and the code is as follows:

Package com.csdn.osgi.user.web.registry;
Import Java.util.Dictionary;

Import java.util.Hashtable;

Import javax.servlet.ServletException;
Import Org.eclipse.gemini.blueprint.context.BundleContextAware;
Import Org.osgi.framework.BundleContext;
Import org.osgi.framework.ServiceReference;
Import Org.osgi.service.http.HttpContext;
Import Org.osgi.service.http.HttpService;
Import org.osgi.service.http.NamespaceException;
Import org.springframework.beans.BeansException;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.ApplicationContextAware;

Import Org.springframework.web.servlet.DispatcherServlet;

    public class Dispatcherservletregistry implements Applicationcontextaware, Bundlecontextaware {String urlpattern;

    String Servletname;

    Bundlecontext Bundlecontext;

    ApplicationContext ApplicationContext;

    Dispatcherservlet Dispatcherservlet; @Override public void Setbundlecontext (Bundlecontext bundlecontext) {thiS.bundlecontext = Bundlecontext; @Override public void Setapplicationcontext (ApplicationContext applicationcontext) throws Beansexce
    ption {this.applicationcontext = ApplicationContext;
    } public void Setservletname (String servletname) {this.servletname = Servletname;
    } public void Seturlpattern (String urlpattern) {this.urlpattern = Urlpattern; } public void Setdispatcherservlet (Dispatcherservlet dispatcherservlet) {This.dispatcherservlet = Dispatcher
    Servlet; public void init () {ServiceReference servicereference = bundlecontext.getservicereference (httpservice.cla
        Ss.getname ());
        Httpservice Httpservice = (httpservice) bundlecontext.getservice (servicereference);


        HttpContext commoncontext = Httpservice.createdefaulthttpcontext ();
        dictionary<string, string> initparams = new hashtable<string, string> (); Initparams.put ("Load-on-startup", "1");
        Initparams.put ("Servlet-name", servletname);
        try {httpservice.registerservlet (Urlpattern, Dispatcherservlet, InitParams, Commoncontext);
        catch (Servletexception e) {e.printstacktrace ();
        catch (Namespaceexception e) {e.printstacktrace ();
 }
    }
}

The instantiation of the Dispatcherservletregistry class is also done through spring, and the bean is configured as follows:

    <bean name= "Dispatcherservlet" class= "Org.springframework.web.servlet.DispatcherServlet" >
        <property Name= "Contextconfiglocation" >
            <value>/META-INF/spring/*.xml</value>
        </property>
    </bean>    

    <bean name= "Servletregistry" class= " Com.csdn.osgi.user.web.registry.DispatcherServletRegistry "init-method=" init ">
        <property name=" Urlpattern "value="/*.do "></property> <property name=" Servletname "value="
        Dispatcherservlet "> </property>
        <property name= "Dispatcherservlet" ref= "Dispatcherservlet" ></property>
    </bean>

Next, in Com.csdn.osgi.user.web this bundle, create a new Meta-inf/spring/registry.xml file to configure the bean to register the Web resource as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:osgix= "http://www.springframework.org/schema/ Osgi-compendium "xmlns:ctx=" Http://www.springframework.org/schema/context "xmlns:osgi=" http://www.eclipse.org/ Gemini/blueprint/schema/blueprint "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://w Ww.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context HTTP://WWW.S Pringframework.org/schema/context/spring-context.xsd Http://www.springframework.org/schema/osgi-compendium http ://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd http://www.eclipse.org/gemini/

    Blueprint/schema/blueprint http://www.eclipse.org/gemini/blueprint/schema/blueprint/gemini-blueprint.xsd "> <bean name= "Dispatcherservlet" class= "Org.springframework.web. servlet. Dispatcherservlet "> <property name=" contextconfiglocation "> <value>/meta-inf/spring/*.x ml</value> </property> </bean> <bean name= "Servletregistry" class= "Com.csdn.osgi" . User.web.registry.DispatcherServletRegistry "init-method=" "init" > <property name= "Urlpattern" value= "/*.do" & gt;</property> <property name= "Servletname" value= "Dispatcherservlet" ></property> &LT;PR Operty name= "Dispatcherservlet" ref= "Dispatcherservlet" ></property> </bean> <bean name= "RESOURC" Eregistry "class=" Com.csdn.osgi.user.web.registry.ResourceRegistry "init-method=" init "> <property name=" resm Apping "> <map> <entry key="/js "value="/webcontent/js "/> &L
            T;entry key= "/css" value= "/webcontent/css"/> <entry key= "/images" value= "/webcontent/images"/> </map> </property> </bean> <bean name= "Jspregistry" class= "Com.csdn.osgi.user.web.regist" Ry.
 Jspregistry "init-method=" init "> </bean> </beans>

The

then creates a new Meta-inf/spring/dmconfig.xml file that references the beans registered in other bundle, as follows:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:osgix= "http://www.springframework.org/schema/ Osgi-compendium "xmlns:ctx=" Http://www.springframework.org/schema/context "xmlns:osgi=" http://www.eclipse.org/ Gemini/blueprint/schema/blueprint "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://w Ww.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context HTTP://WWW.S Pringframework.org/schema/context/spring-context.xsd Http://www.springframework.org/schema/osgi-compendium http ://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd http://www.eclipse.org/gemini/

   Blueprint/schema/blueprint http://www.eclipse.org/gemini/blueprint/schema/blueprint/gemini-blueprint.xsd "> <osgi:reference id= "Sqlmapservice" interface= "Org.apache.ibatIs.session.SqlSession "/> </beans> 

The

Then also needs to create a new Meta-inf/spring/controllers.xml file, instantiate the controller and Viewresolver in spring MVC, and configure the following:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:osgix= "http://www.springframework.org/schema/ Osgi-compendium "xmlns:ctx=" Http://www.springframework.org/schema/context "xmlns:osgi=" http://www.eclipse.org/ Gemini/blueprint/schema/blueprint "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://w Ww.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/context HTTP://WWW.S Pringframework.org/schema/context/spring-context.xsd Http://www.springframework.org/schema/osgi-compendium http ://www.springframework.org/schema/osgi-compendium/spring-osgi-compendium.xsd http://www.eclipse.org/gemini/


    Blueprint/schema/blueprint http://www.eclipse.org/gemini/blueprint/schema/blueprint/gemini-blueprint.xsd "> <bean id= "Viewresolver" class= "ORG.SPRINGFRAMEWORK.WEB.SERVLEt.view.InternalResourceViewResolver "> <property name=" prefix "value=" jsp/"/> <property-name=" Suffix "value=". jsp "/> </bean> <bean name="/test.do "class=" Com.csdn.osgi.web.controllers.TestControl Ler "> </bean> </beans>

In the configuration above, we instantiated a TestController object to test whether the Spring MVC Framework was successfully consolidated, TestController code as follows:

Package

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.