簡體   English   中英

Apache Karaf 不加載 spring 應用程序上下文

[英]Apache Karaf does not load the spring application context

我正在嘗試編寫一個將部署在 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>

您需要安裝 karaf 的 spring-dm 功能。 它包含掃描包以獲取 spring 上下文並啟動它們的擴展器。 如果沒有這個,bundle 可能會啟動,但不會做任何事情。

“解決了”並不意味着一切都好,開始了 這僅僅意味着 karaf '找到了你的東西'。 如果一切都成功加載並啟動,則狀態將為“活動”。 事實上,你的包是惰性的。

您可能需要檢查應用程序上下文 xml文件的名稱是否為默認名稱。 我原以為該目錄中的任何 xml 文件都會被讀入,但您可能希望重命名為“spring.xml”作為第一次嘗試。 (我使用藍圖配置和 jboss karaf 構建,所以我不能說這是否會有所幫助——盡管我懷疑不會)

假設這不是問題,請打開 2 個終端,然后:
- 首先,啟動 karaf 客戶端並執行log:tail
- 在第二個中,啟動 karaf 客戶端並執行restart <number of your bundle>
- 使用您的第一個終端的輸出更新您的帖子

輔助注釋 - 如果您選擇采用較新的“藍圖”配置標准(推薦,基本上是 spring 配置的超集),則默認文件名/位置為src/main/resources/OSGI-INF/blueprint/blueprint.xml

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM