【问题标题】:Apache Karaf does not load the spring application contextApache Karaf 不加载 spring 应用程序上下文
【发布时间】:2015-07-19 09:11:13
【问题描述】:

我正在尝试编写一个将部署在 Apache Karaf 中的 Spring 应用程序 osgi 包。我的包已成功生成,当我在 Karaf 中安装包时,它显示包已成功启动,相应的包状态为“已解决”。但我不相信捆绑已成功启动。

因为只是为了测试目的,我的一个 Spring bean 实现了 InitializingBean 并且在 afterPropertiesSet 方法中我只是放了一个 sysout。这意味着当加载应用程序上下文时,应该在 karaf 控制台中打印 sysout。

但事实并非如此。 应用程序上下文文件位于src/main/resources/META-INF/spring 目录下,并且在清单文件中我添加了 Spring-Context 标记。但它仍然无法正常工作。

我是 OSGI 的新手,并且坚持了 3 天以上。我遵循了文档和谷歌结果,但仍然没有运气。

请在这方面帮助我。我还将我的小代码库附加在这里,以便您查看。

我的应用程序上下文

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:p = "http://www.springframework.org/schema/p"
    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">
    <bean id="newService" class="com.mycompany.bundle.SpringService"/>
    <bean id="springContext" class="com.mycompany.bundle.SpringContext"/>
    <osgi:service id="simpleServiceOsgi" ref="newService"
        interface="com.mycompany.bundle.ISpringService" />
</beans>

我的 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.shamik</groupId>
    <artifactId>com.shamik.bundle.new</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>bundle</packaging>
    <name>com.shamik.bundle.new Bundle</name>
    <description>com.mycompany.bundle OSGi bundle project.</description>
    <dependencies>
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>4.2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
            <version>2.2.0.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.osgi</groupId>
            <artifactId>spring-osgi-core</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.osgi</groupId>
            <artifactId>spring-osgi-extender</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.osgi</groupId>
            <artifactId>spring-osgi-io</artifactId>
            <version>1.2.1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <version>2.3.7</version>
                <extensions>true</extensions>
                <configuration>
                <excludeDependencies>*;scope=provided|runtime</excludeDependencies>
                    <unpackBundle>true</unpackBundle>
                    <instructions>
                        <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                        <Bundle-Version>${project.version}</Bundle-Version>
    <!--                <Bundle-Activator>com.mycompany.bundle.Activator</Bundle-Activator> -->
                        <Export-Package>
                            javax.xml.parsers*,org.xml.sax*,com.mycompany.bundle*;version=${project.version},org.springframework.context*,org.springframework.stereotype*
                        </Export-Package>
                        <Import-Package>
                            com.mycompany.bundle*,javax.xml.parsers*,org.xml.sax*,org.springframework.context*,org.osgi*,org.springframework.stereotype*,!*
                        </Import-Package>
                        <Include-Resource>src/main/resources</Include-Resource>
                        <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
                        <Embed-Transitive>true</Embed-Transitive>
                        <Spring-Context>*;publish-context:=false;create-asynchronously:=true</Spring-Context>
                        <DynamicImport-Package>*</DynamicImport-Package>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>       
    </build>
</project>

【问题讨论】:

    标签: spring osgi apache-karaf


    【解决方案1】:

    “解决”并不意味着一切都很好开始。它仅仅意味着 karaf '找到了你的东西'。如果一切都已成功加载并启动,则状态将为“活动”。实际上,您的捆绑包是惰性的。

    您可能需要检查应用程序上下文 xml file 的名称是否为默认名称。我曾认为该目录中的任何 xml 文件都会被读入,但您可能希望重命名为“spring.xml”作为第一次尝试。 (我使用蓝图配置和 jboss karaf 构建,所以我不能说这是否会有所帮助——尽管我怀疑没有)

    假设不是问题,请打开 2 个终端,然后:
    - 首先,启动 karaf 客户端并执行log:tail
    - 第二步,启动karaf客户端并执行restart &lt;number of your bundle&gt;
    - 使用您的第一个终端的输出更新您的帖子

    辅助注释 - 如果您选择采用较新的“蓝图”配置标准(推荐,基本上是 spring config 的超集),那么默认文件名/位置是src/main/resources/OSGI-INF/blueprint/blueprint.xml

    【讨论】:

      【解决方案2】:

      您需要安装 karaf 的 spring-dm 功能。它包含扫描捆绑包以查找弹簧上下文并启动它们的扩展程序。如果没有这个,bundle 可能会启动但不会做任何事情。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-16
        • 2011-05-13
        • 1970-01-01
        • 2023-04-09
        • 1970-01-01
        • 1970-01-01
        • 2017-02-12
        • 1970-01-01
        相关资源
        最近更新 更多