2011-02-08 8 views
0

皆さん、新生児質問

ご協力いただきありがとうございます。

これは私の問題です。

私はスプリングからOSGI Bundleに単純なBeanをインスタンス化したいと考えています。ここで

は私のJavaクラスです:

package testspring; 

public class ClassIOC { 

    public void helloWorld(){ 
     System.out.println("Hello World Spring with OSGI"); 
    } 

} 

ここに私のアクチベーターここ

package testspring; 

import org.osgi.framework.BundleActivator; 
import org.osgi.framework.BundleContext; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 

public class Activator implements BundleActivator { 

    private static BundleContext context; 

    static BundleContext getContext() { 
     return context; 
    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see 
    * org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext 
    *) 
    */ 
    public void start(BundleContext bundleContext) throws Exception { 
    Activator.context = bundleContext; 

     ApplicationContext appContext = new ClassPathXmlApplicationContext(
       "META-INF/spring/applicationContext.xml"); 
     ClassIOC instance = (ClassIOC) appContext.getBean("ClassIOC"); 
     instance.helloWorld(); 

    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see 
    * org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext) 
    */ 
    public void stop(BundleContext bundleContext) throws Exception { 
     Activator.context = null; 
    } 

} 

が私のマニフェストです:

Manifest-Version: 1.0 
Bundle-ManifestVersion: 2 
Bundle-Name: TestSpring 
Bundle-SymbolicName: TestSpring 
Bundle-Version: 1.0.0.qualifier 
Bundle-Activator: testspring.Activator 
Bundle-ActivationPolicy: lazy 
Bundle-RequiredExecutionEnvironment: JavaSE-1.6 
Import-Package: org.osgi.framework;version="1.3.0" 
Bundle-ClassPath: . 
Require-Bundle: org.springframework.aop;bundle-version="2.5.6", 
org.springframework.beans;bundle-version="2.5.6", 
org.springframework.context;bundle-version="2.5.6", 
org.springframework.core;bundle-version="2.5.6", 
com.springsource.org.apache.commons.logging;bundle-version="1.1.1", 
com.springsource.org.apache.log4j;bundle-version="1.2.15" 

私はOSGI容器にチェックして、私はすべて持っています束ねる。

私は私のバンドルをアクティブに各時間は、私は次の例外があります

org.osgi.framework.BundleException: Activator start error in bundle TestSpring [36]. 
     at org.apache.felix.framework.Felix.activateBundle(Felix.java:1869) 
     at org.apache.felix.framework.Felix.startBundle(Felix.java:1739) 
     at org.apache.felix.framework.BundleImpl.start(BundleImpl.java:922) 
     at org.apache.felix.gogo.command.Basic.start(Basic.java:758) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
     at java.lang.reflect.Method.invoke(Unknown Source) 
     at org.apache.felix.gogo.runtime.Reflective.method(Reflective.java:136) 
     at org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:82) 
     at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:458) 
     at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:384) 
     at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108) 
     at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:183) 
     at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:120) 
     at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:79) 
     at org.apache.felix.gogo.shell.Console.run(Console.java:62) 
     at org.apache.felix.gogo.shell.Shell.console(Shell.java:198) 
     at org.apache.felix.gogo.shell.Shell.gosh(Shell.java:124) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
     at java.lang.reflect.Method.invoke(Unknown Source) 
     at org.apache.felix.gogo.runtime.Reflective.method(Reflective.java:136) 
     at org.apache.felix.gogo.runtime.CommandProxy.execute(CommandProxy.java:82) 
     at org.apache.felix.gogo.runtime.Closure.executeCmd(Closure.java:458) 
     at org.apache.felix.gogo.runtime.Closure.executeStatement(Closure.java:384) 
     at org.apache.felix.gogo.runtime.Pipe.run(Pipe.java:108) 
     at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:183) 
     at org.apache.felix.gogo.runtime.Closure.execute(Closure.java:120) 
     at org.apache.felix.gogo.runtime.CommandSessionImpl.execute(CommandSessionImpl.java:79) 
     at org.apache.felix.gogo.shell.Activator.run(Activator.java:75) 
     at java.lang.Thread.run(Unknown Source) 
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-I 
NF/spring/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/spring/applicationContex 
t.xml] cannot be opened because it does not exist 
     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:349) 
     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:310) 
     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143) 

     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178) 

     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149) 

     at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:212) 

     at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:113) 
     at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:80) 
     at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContex 
t.java:123) 
     at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:422) 
     at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:352) 
     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139) 
     at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83) 
     at testspring.Activator.start(Activator.java:31) 
     at org.apache.felix.framework.util.SecureAction.startActivator(SecureAction.java:633) 
     at org.apache.felix.framework.Felix.activateBundle(Felix.java:1822) 
     ... 32 more 
Caused by: java.io.FileNotFoundException: class path resource [META-INF/spring/applicationContext.xml] cannot be opened because it does not 
exist 
     at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:143) 
     at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336) 
     ... 47 more 
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [META-INF/spring/a 
pplicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [META-INF/spring/applicationContext.xml] cann 
ot be opened because it does not exist 

を私はソースとしてMETA-INF/springディレクトリを宣言しようとしている、私はMETA」なし(直接自分のアプリケーションコンテキストにアクセスしよう-INF /春」)、それがすべてを変えていません....

私はjarファイルとして春の束を追加した場合、それが動作する...

私は新しいアイデアの欠落しています。私は豆の単純なインスタンス化をしたい、私は春のdmを使用したくない、またはスコープ= "プロトタイプ"で豆をインスタンス化する方法のようなすべてを理解していない

私は強制的に使用しています春のdm?あなたは春のDMを使用している場合

よろしく

+4

は、「[OSGI-春] Newbeeの質問」などの科目を使用しないでください、それは絶対に何も教えてくれるん。'[spring] [osgi]'のタグを使い、その内容を説明的なものにしてください。 – skaffman

答えて

-1

最後に私は答えを見つけました。

OSGIは、アプリケーションコンテキストにアクセスするようには設計されていません。インスタンス化を行うには、豆をサービスとして公開する方がよいでしょう。しかし、次の方法でそれらにアクセスすることが可能である:

com.binomed.osgi.spring.testは私のバンドルのIDです
ServiceReference webContext = context.getAllServiceReferences("org.springframework.context.ApplicationContext", "(org.springframework.context.service.name=com.binomed.osgi.spring.test)")[0]; 

。あなたの応答のためにとにかく

おかげ

+2

これは本当にあなたの質問に答えません(java.io.FileNotFoundException:クラスパスリソース[META-INF/spring/applicationContext.xml])。いいですね。私が示唆した答えがこの問題を解決するなら、あなたはヒントを与えるでしょう。アプリケーションやバンドルコンテキストにアクセスするためのスタイルが良い場合は、別の質問があります。 Spring DMは、これに簡単にアクセスするためのサーバインタフェース(BundleContextAware/ApplicationContextAware)を提供します。また、OSGiサービスの登録やアクセスも簡単にできます。 http://static.springsource.org/osgi/docs/1.2.1/reference/html/を参照してください。 – FrVaBe

2

はそれが生きてはるかに簡単それとして、例えばようになりますファイルをMETA-INF/springディレクトリに置くと、アプリケーションコンテキストが作成されます。

また、あなたが春を使用したくないので、場合

Spring-Context: /META-INF/spring/applicationContext.xml 

を(パスは「/」で始まるこの場合)MANIFEST中(スプリングDM用)applicationContext.xmlをパスを指定することができますOSGIのdmは使用しようとします

ApplicationContext appContext = new ClassPathXmlApplicationContext(
       "/META-INF/spring/applicationContext.xml"); 

(多分 '/'が重要です)。