`
mizhao1984
  • 浏览: 91688 次
  • 性别: Icon_minigender_1
  • 来自: 西安
社区版块
存档分类
最新评论

osgi配置数据源bundle供其他bundle使用

阅读更多

1.创建plug-in project:

此bundle依赖jar包:

commons-dbcp-1.2.2.jar
commons-pool-1.3.jar
oracle-jdbc-10g.jar
spring-beans-2.5.6.jar
spring-dao-2.5.6.jar
spring-jdbc-2.5.6.jar

在META-INF下新建spring文件夹,并在文件夹里创建jdbc.xml,services-publish-cxf.xml

jdbc.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd"
 default-autowire="byName">
 <!-- datasource -->
 <bean id="_dataSource"  class="com.thewebpagestudio.util.jdbc.BasicDataSource">
  <constructor-arg index="0"
   value="oracle.jdbc.driver.OracleDriver"></constructor-arg>
  <constructor-arg index="1"
   value="jdbc:oracle:thin:@192.168.0.57:1521:xe"></constructor-arg>
  <constructor-arg index="2" value="surfing_campus"></constructor-arg>
  <constructor-arg index="3" value="20100504"></constructor-arg>
 </bean>
</beans>

 

services-publish-cxf.xml:

<?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:osgi="http://www.springframework.org/schema/osgi"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                      http://www.springframework.org/schema/osgi http://www.springframework.org/schema/osgi/spring-osgi.xsd">
  
   <osgi:service id="dataSource" ref="_dataSource" interface="javax.sql.DataSource"></osgi:service>
</beans>

 

2。创建测试数据源的bundle:

bundle依赖包:

spring-beans-2.5.6.jar
spring-dao-2.5.6.jar
spring-jdbc-2.5.6.jar

新建测试类:

package com.oohla.test;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

public class TestDataSource {
 
 public static final Log log = LogFactory.getLog(TestDataSource.class);

 private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
 
 public void init() throws Exception{
  int count = jdbcTemplate.queryForInt("select count(*) from sms_log");
  log.info("count2222222222222222222222222" + count);
 }

}

 

在META-INF下新建spring文件夹,并在文件夹里创建beans.xml,services-reference-cxf.xml

beans.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd" default-autowire="byName">
        
      <!-- 启动通话监听任务 -->
      <bean class="com.oohla.test.TestDataSource" init-method="init"></bean>
   
      <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"/>
</beans>

 

<?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:osgi="http://www.springframework.org/schema/osgi"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd
                      http://www.springframework.org/schema/osgi
                      http://www.springframework.org/schema/osgi/spring-osgi.xsd" default-autowire="byName">
   
    <osgi:reference id="dataSource" interface="javax.sql.DataSource" />                  
   
</beans>

 

注:如果运行过程中报:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.oohla.test.TestDataSource#0' defined in URL [bundleentry://155.fwk7031149/META-INF/spring/beans.xml]: Invocation of init method failed; nested exception is org.springframework.aop.AopInvocationException: AOP configuration seems to be invalid: tried calling method [public abstract java.sql.Connection javax.sql.DataSource.getConnection() throws java.sql.SQLException] on target [com.thewebpagestudio.util.jdbc.BasicDataSource@c932df]; nested exception is java.lang.IllegalArgumentException: object is not an instance of declaring class 错误

这是由于 osgi加载机制的原因

 

解决方法:在jvm设置中添加-Dorg.osgi.framework.bootdelegation=*

分享到:
评论

相关推荐

    OSGi进阶源代码

    4. **Hibernate配置**:包括Hibernate的实体定义、数据源配置和SessionFactory的创建。可能通过OSGi服务暴露SessionFactory,以便其他bundle按需使用。 5. **服务接口和实现**:源代码可能包含多个bundle提供的服务...

    osgi数据库连接demo

    在OSGi中,我们需要配置iBATIS的SqlMapConfig.xml文件,声明数据源(这里使用的是C3P0连接池)并定义SQL映射文件。 Spring Dynamic Modules (Spring-DS) 是Spring框架在OSGi环境下的扩展,它帮助我们管理Spring应用...

    OSGI 经典实例,入门

    在"exampleSource.jar"中,我们可以预见到包含了OSGI bundle的源代码。这通常会包含bundle的初始化类,它定义了bundle的生命周期方法,如start()和stop(),以及bundle的元数据,例如Bundle-Activator和Export-...

    OSGI Core PDF+Code

    导出包意味着bundle提供了某些包的实现,而导入包则表示bundle需要使用其他bundle提供的功能。这种设计使得bundle之间可以有选择地依赖,降低了模块间的耦合度。 Equinox是Eclipse基金会开发的一个OSGI实现,它是...

    blueprint-osgi-bundle:OSGi 示例

    3. **服务组件(Service Component)**:在OSGi中,服务组件是提供服务的模块,可以通过注册到服务注册表来供其他组件使用。蓝图允许声明式地定义这些服务组件及其依赖。 4. **模块化**:OSGi将应用程序拆分为独立...

    jmx osgi 实例

    "demo"文件可能包含了实现上述步骤的源代码,包括OSGi bundle、MBean实现、MBeanServer的配置和使用,以及可能的测试脚本。通过分析和运行这个示例,开发者可以更深入地理解JMX与OSGi的集成,并将其应用于自己的项目...

    OSGi Web示例工程

    2. **Bundle**:OSGi中的基本单位,类似Java的JAR文件,但包含元数据以描述其依赖和其他元信息。每个bundle都是独立的,有自己的类加载器,可以在运行时安装、启动、停止和卸载。 3. **Web Bundle**:在OSGi环境中...

    很久之前的osgi整理

    标签提到“源码”和“工具”,这可能意味着内容包含了如何使用特定的IDE或构建工具(如Eclipse或Maven)进行OSGi开发,以及如何阅读和理解OSGi Bundle的源代码。 9. **EOFFICE.doc**: 压缩包中的文件名为EOFFICE...

    OSGI 实例eclipse插件开发

    它包含了配置元数据的解析、事件发布、国际化等功能,是其他所有模块的基础。 4. `org.springframework.osgi.extender`:这是Spring OSGI扩展器,它允许在OSGI环境中启动和管理Spring应用上下文。它监控bundle的...

    osgi spring实例

    4. **使用Spring DM或Spring OSGi**:配置元数据,如XML配置文件,描述服务的生命周期和依赖关系。 5. **部署和管理**:将bundle部署到OSGi容器,容器会自动管理服务的生命周期和依赖解析。 通过这个实例,开发者...

    osgi-bundle-hello-world:使用Maven的演示OSGI捆绑包

    标题中的“osgi-bundle-hello-world”是一个示例项目,展示了如何使用Maven构建一个OSGi(Open Services Gateway Initiative)捆绑包。OSGi是一种Java模块化系统,它允许开发者将应用程序分解为独立的、可重用的服务...

    osgi hibernate

    - 数据源管理:在OSGi环境中配置数据源,确保其能在多个模块间共享。 5. **挑战与解决方案**:集成过程中可能遇到的问题,比如类加载冲突、服务生命周期管理等,并提供相应的解决策略。 6. **最佳实践**:提供在...

    SpringDM笔记7-开发SpringDM Bundle

    Spring配置文件可以使用标准的Spring XML格式,但需添加一些特定于OSGi的元素,如`&lt;osgi:reference&gt;`用于声明对其他服务的依赖,以及`&lt;osgi:service&gt;`用于发布自己的服务。SpringDM会自动处理服务的注册和解注册,...

    OSGI常用通信实例

    "osgi-demo-2016-06-27"可能包含示例项目的源代码,展示了如何创建和注册服务,如何在bundle之间共享数据,以及如何处理事件。分析这些代码可以帮助理解OSGI的实际工作原理。 7. 实践应用: OSGI常用于嵌入式系统...

    OSGi原理与最佳实践的源码

    1. **Bundle**:在OSGi中,bundle是基本的部署和运行单位,类似于Java中的JAR文件,但包含额外的元数据来描述其依赖关系和其他元信息。 2. **生命周期**:每个bundle都有自己的生命周期,包括安装、启动、暂停、...

    OSGi与Equinox 创建高度模块化的Java系统 第4章完整源码

    3. **Bundle开发**:学习如何编写符合OSGi规范的Java代码,创建bundle的manifest.mf文件,声明导出和导入的包,配置元数据以及定义bundle的启动级别。 4. **服务注册与发现**:理解OSGi服务的概念,如何注册服务到...

    一个关于osgi的例子

    在OSGi中,每个模块称为一个Bundle,它包含类、资源和元数据,可以理解为Java的增强版JAR文件。这些Bundle可以通过声明依赖来相互连接,使得系统可以根据需要动态地加载、启动、停止或更新模块,而不会影响到其他...

    OSGi-lib.rar

    这个项目通常会包含必要的源代码、配置文件以及构建脚本,帮助初学者理解OSGi的工作原理。而另一个文件"OSGi"可能是一个更复杂的库,包含了OSGi框架的实现或者额外的模块和服务。 在OSGi的实践中,开发者需要了解...

    OSGi_with_Eclipse_Equinox_-_Tutorial

    定义声明式服务涉及到使用元数据来描述服务的属性和配置。 **7.3 审查结果** 使用声明式服务后,可以审查生成的服务配置,并确保它们符合预期。 **7.4 运行声明式服务** 配置完成后,可以启动OSGi框架并验证声明...

    maven+osgi

    5. 源码分析:在“源码”标签的提示下,可能涉及到对Maven插件配置或OSGi bundle的源代码分析,以解释它们的工作原理或展示如何解决特定问题。 6. 实战应用:可能通过实例讲解如何在实际项目中配置Maven和OSGi,...

Global site tag (gtag.js) - Google Analytics