`
sunxboy
  • 浏览: 2857517 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Spring Web Application应用到OSGI环境 三(转)

阅读更多
一、Spring Framework
通常Spring的web应用结构层次:
1,ContextLoaderServlet,创建一个ContextLoader做为整个web应用的Loader,它会为自己创建一个web应用级别的WebApplicationContext,接着默认加载applicationContext.xml里面的bean
2,DispatcherServlet,同样创建一个WebApplicationContext,加载dispatch-bean,它会将ContextLoader的WebApplicationContext做为自己的父ApplicationContext

二、Spring-OSGI
通过Spring-osgi规范可以了解到spring-osgi会为每一个有spring-bean配置文件的bundle创建一个ApplicationContext,spring-bean配置文件里面的bean则被它加载,所以Dispatch-bean-loader无法找到spring-osgi-bean

三、RESOLVER
首先要先得到bundle的ApplicationContext,然后创建一个自定义的ContextLoader-BundleContextLoader,不加载配置文件,重写loadParentContext方法,将父ApplicationContext设置成bundle的ApplicationContext,然后生成一个自定义的WebApplicationContext-BundleWebApplicationContext,里面的bean信息直接从parentApplicationContext里面获取。本来spring规范里面写着org.springframework.osgi.context.support.WebApplicationContext ,但是找个底朝天,也没找到,可能还没出呢吧,于是只能自己写了。
bundle的ApplicationContext可通过org.springframework.context.ApplicationContextAware.setApplicationContext(ApplicationContext applicationContext)得到

四、BundleContextLoaderServlet
在org.phrancol.osgi.jpetstore.springmvc里新建类BundleContextLoaderServlet
public class BundleContextLoaderServlet extends ContextLoaderServlet {
    
private final ApplicationContext applicationContext;
    
private BundleContext ctx;

    
public BundleContextLoaderServlet(BundleContext ctx,
            ApplicationContext appContext) 
{
        
this.ctx = ctx;
        
this.applicationContext = appContext;
    }

    
    
/** *//**
     * 重写该方法,用于构造一个自定义的BundleContextLoader
     
*/

    
protected ContextLoader createContextLoader() {
        
return new BundleContextLoader(applicationContext, ctx);
    }

}
五、BundleContextLoader
在org.phrancol.osgi.jpetstore.springmvc里新建类BundleContextLoader
public class BundleContextLoader extends ContextLoader {
    
private final ApplicationContext applicationContext;
    
private BundleContext bundleContext;

    
public BundleContextLoader(ApplicationContext appContext, BundleContext ctx) {
        
this.applicationContext = appContext;
        
this.bundleContext = ctx;
    }

    
    
/** *//**
     * 重写该方法,让其返回的ParentContext是Bundle里的那个ApplicationContext
     
*/

    
protected ApplicationContext loadParentContext(ServletContext servletContext)
            
throws BeansException {
        
return applicationContext;
    }


    
/** *//**
     * 生成一个自定义的BundleWebApplicationContext,返回的bean信息直接从Parent获取
     * 这里的代码请参考org.springframework.osgi.extender.support.ApplicationContextCreator.run()方法里的代码
     
*/

    
protected WebApplicationContext createWebApplicationContext(
            ServletContext servletContext, ApplicationContext parent)
            
throws BeansException {
        ClassLoader contextClassLoader 
= Thread.currentThread()
                .getContextClassLoader();
        
try {
            ClassLoader cl 
= BundleDelegatingClassLoader
                    .createBundleClassLoaderFor(bundleContext.getBundle(), getClass()
                            .getClassLoader());
            Thread.currentThread().setContextClassLoader(cl);
            LocalBundleContext.setContext(bundleContext);
            
return new BundleWebApplicationContext(parent, servletContext);
        }
 finally {
            Thread.currentThread().setContextClassLoader(contextClassLoader);
        }

    }

}

六、BundleWebApplicationContext
在org.phrancol.osgi.jpetstore.springmvc里面新建类BundleWebApplicationContext,所有的方法都返回parent的方法返回值
public class BundleWebApplicationContext implements WebApplicationContext {
    
    
private final ApplicationContext parentApplicationContext;
    
private ServletContext servletContext;
    
    
public BundleWebApplicationContext(ApplicationContext parentApplicationContext,
            ServletContext servletContext)
{
        
this.parentApplicationContext = parentApplicationContext;
        
this.servletContext = servletContext;
    }


    
public ServletContext getServletContext() {
        
// TODO Auto-generated method stub
        return servletContext;
    }


    
public AutowireCapableBeanFactory getAutowireCapableBeanFactory()
            
throws IllegalStateException {
        
// TODO Auto-generated method stub
        return parentApplicationContext.getAutowireCapableBeanFactory();
    }


    
public String getDisplayName() {
        
// TODO Auto-generated method stub
        return parentApplicationContext.getDisplayName();
    }

        .
}

七、修改
1,修改接口HttpServiceRegister.serviceRegister(BundleContext context),改成
serviceRegister(BundleContext context, ApplicationContext bundleApplicationContext);
2,修改SpringmvcHttpServiceRegister.serviceRegister(BundleContext context),改成
serviceRegister(BundleContext context, ApplicationContext bundleApplicationContext)
3,SpringmvcHttpServiceRegister.serviceRegister方法修改一下
public void serviceRegister(BundleContext context, ApplicationContext bundleApplicationContext) {
        
try {
            ServiceReference sr 
= context.getServiceReference(HttpService.class
                    .getName());
            HttpService httpService 
= (HttpService) context.getService(sr);
            httpService.registerResources(
"/""/web"null);
            httpService.registerServlet(
"/*.jsp"new JspServlet(context
                    .getBundle(), 
"/web"), nullnull);
            Dictionary
<String, String> initparams = new Hashtable<String, String>();
            initparams.put(
"load-on-startup""1");
           
/*  编辑器的问题,这4行代码不是注释。。。 */
            ContextLoaderServlet contextloaderListener = new BundleContextLoaderServlet(
                    context, bundleApplicationContext);
            httpService.registerServlet("/ContextLoader",
                    contextloaderListener, initparams, null);
            /*   
*/

            DispatcherServlet dispatcherServlet 
= new DispatcherServlet();
            dispatcherServlet
                    .setContextConfigLocation(
"META-INF/dispatcher/petstore-servlet.xml");
            initparams 
= new Hashtable<String, String>();
            initparams.put(
"servlet-name""petstore");
            initparams.put(
"load-on-startup""2");
            httpService.registerServlet(
"/*.do", dispatcherServlet, initparams,
                    
null);
        }
 catch (Exception e) {
            e.printStackTrace(System.out);
        }

    }

OK,这下应该没问题了,启动看看效果
倒,还是报错。。。。。。

八、HttpContext
来看看org.osgi.service.http.HttpService.registerServlet的代码
  * Servlets registered with the same <code>HttpContext</code> object will
  * share the same <code>ServletContext</code>.
原来是这样,注册了2个servlet,于是生成了2个不同的HttpContext,那就只生成一个试试
HttpContext defaultContext = new DefaultHttpContext(context.getBundle());

httpService.registerServlet(
"/init-context-loader-petsore-web",
                    contextloaderListener, initparams, defaultContext);

httpService.registerServlet(
"/*.do", dispatcherServlet, initparams,
                    defaultContext);

再启动,一切顺利,访问页面也正常。

九、打完收工
将org.phrancol.osgi.jpetstore.springmvc/METS-INF/dispatcher/petstore-servlet.xml里面的bean补全(注意还有2个bean需要注册和引用),再运行,就OK了。

 

 

 

 

分享到:
评论

相关推荐

    RAP 整合 Spring (基于 Spring-osgi )

    标题 "RAP 整合 Spring(基于 Spring-osgi)" 暗示了本文将探讨如何在 RAP(Remote Application Platform)框架中集成 Spring 框架,并利用 Spring-osgi 进行服务管理和依赖注入。RAP 是一个用于构建富客户端应用...

    OSGi与Web容器的整合

    Web Application Bundle (WAB) 是一种特殊的OSGi Bundle,它可以作为一个Web应用运行。WAB包含传统的Web项目元素,如Web-INF目录和web.xml,同时还包含OSGi元数据,如MANIFEST.MF文件,使得它们能够作为OSGi模块运行...

    OSGI原理最佳实践(包含源代码)

    是一本适合新接触OSGI开发学习的一本很好的书,本书介绍了Equinox, Spring-DM和Felix这三个常用的OSGi容器的使用、开发、以及WebApplication的开发部署;介绍了OSGi的规范和Core Framework和Layer。包含书中的用例源...

    springDM开发指南(英文)

    5. **Web Support**:Spring DM增加了对Web应用的支持,使得开发者可以更方便地在OSGi环境中部署和管理Web应用。 6. **Classpath Resource Abstraction**:引入了资源抽象层,使得开发者可以通过统一的方式访问类...

    Spring Recipes: A Problem-Solution Approach, Second Edition

    * Building modular services using OSGi with Spring DM and Spring Dynamic Modules and SpringSource dm Server. * Delivering web applications with Spring Web Flow, Spring MVC, Spring Portals, Struts, ...

    Eclipse RCP+Spring建胖客户端Web程序

    ### Eclipse RCP+Spring构建富客户端Web程序 #### 摘要 随着技术的发展和用户需求的变化,构建高效、响应迅速且用户体验良好的客户端应用变得尤为重要。传统的Web 2.0和丰富的互联网应用(RIA)虽然流行,但在某些...

    example-spring-boot-embedded-felix:测试如何将Apache Felix嵌入到Spring Boot应用程序中

    运行嵌入式Apache Felix OSGI容器的示例Spring Boot Web应用程序 这个想法是能够: 加载运行嵌入式Felix Framework的基本Spring Boot App。 使用标准的注解(例如Spring Web控制器和ServletFilter等),使用普通...

    04 Java基础-springboot1

    在Spring Boot中,`@SpringBootApplication`注解是核心注解,它结合了`@Configuration`、`@EnableAutoConfiguration`和`@ComponentScan`三个注解的功能,使得Spring Boot能够自动配置并启动应用。 在【部分内容】中...

    virgo编程手册.pdf

    手册涵盖了从环境搭建到应用程序部署的全过程,是一份不可多得的学习资料。 ### 核心知识点 #### 1. **Virgo Web Server(VWS)概述** - **定义与特性**:Virgo Web Server(VWS)是基于OSGi规范构建的,旨在...

    Spring Recipes A Problem-Solution Approach [英文原版]

    One of the first (if not the first) books on the latest Spring 3.x, and the first Spring code Recipes book focused on Spring Web-tier development The Spring framework is growing. It has always been ...

    IDEA教程中文版。

    - **Vaadin**: 支持 Vaadin 框架,这是一个基于 GWT 的 Web RIA(Rich Internet Application)框架。 - **Velocity**: 支持 Velocity 模板引擎。 #### 二、配置建议 - **选择适合自己的插件**: 根据项目需求和个人...

    Jetty中文手册

    部署展开形式的web应用 使用Jetty进行开发 如何使用Jetty进行开发 如何编写Jetty中的Handlers 使用构建工具 如何在Maven中使用Jetty 如何在Ant中使用Jetty Maven和Ant的更多支持 Jetty Maven插件(Plugin) Jetty ...

    Onboard插件编程指南1

    - **WEB-INF**:存放Spring的配置文件,如applicationContext.xml、applicationOSGI.xml、applicationSecurity.xml、dispatcher-servlet.xml和web.xml,分别处理Bean声明、OSGI服务引用、安全认证和Web配置。...

    H2数据库官方文档(English)

    - **嵌入H2到应用中**(Embedding H2 in an Application):文档将提供如何将H2数据库嵌入到Java应用程序中,并启动和使用H2控制台应用程序的详细步骤。 - **H2控制台应用程序**(The H2 Console Application):...

    各种在线api

    2.5版本的Spring提供了强大的依赖注入(DI)和面向切面编程(AOP)功能,以及对Web应用的全面支持,包括MVC控制器、视图解析器等,大大提升了应用的可测试性和可维护性。 ### Struts Struts框架分为两个主要版本:...

    JAVA开源软件分类

    应用服务器为Java Web应用提供运行环境,包括但不限于以下几类: - **P1:主流应用服务器** - **Tomcat/JBOSS/Jetty/GlassFish**:广泛使用的应用服务器,适合不同规模的应用。 - **P2:高级应用服务器** - **SOA...

    Java EE 学习方向

    - WebService、SOA(Service-Oriented Architecture)、ESB(Enterprise Service Bus)、OSGI(Open Service Gateway Initiative)、EAI(Enterprise Application Integration)等架构概念。 - **面向资源架构** ...

    eclipse Java开发

    12. **部署与发布**:对于Web应用,Eclipse可以连接到应用服务器(如Tomcat、Jetty),进行应用的部署和调试。 13. **Problems视图和Error Log视图**:显示项目中存在的问题和错误,便于定位和修复。 14. **...

    基于Java通用多模块后台管理系统源代码

    常见的Java模块化框架有Spring Boot和OSGi,它们通过定义清晰的接口和依赖关系,使得模块间的交互更加有序。本项目可能采用了Spring Boot,它简化了Spring框架的配置,并集成了多种微服务组件,如数据访问、安全控制...

Global site tag (gtag.js) - Google Analytics