2011-02-08 2 views
0

안녕하세요 여러분,새내기 질문

미리 도움을 주셔서 감사합니다.

여기 내 문제입니다.

봄에서 간단한 빈을 OSGI 번들로 인스턴스화하려고합니다. 여기

내 자바 클래스입니다 : 내 OSGI 용기에 확인

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" 

나는 모든이 : 여기

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; 
    } 

} 

것은 내 매니페스트입니다 묶음이됩니다.

각 시간은 내 번들을 활성화 나는 다음과 같은 예외가 있습니다 : 나는 소스로 META-INF는/봄 디렉토리를 선언하려고해야

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/봄 ")는, 내가 새로운 아이디어의 누락 ... 작동,

모든 것을 ....

을 변경하지 않습니다. 나는 콩의 단순한 인스턴스화를 원한다. 스프링 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]). 제안 된 대답이이 문제를 해결하면 힌트를 제공하는 것이 좋을 것입니다. 응용 프로그램이나 번들 컨텍스트에 액세스하는 것이 좋은 스타일 인 경우 다른 질문입니다. 스프링 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 

을 (경로가 '/'로 시작하는이 경우) 매니페스트 (스프링 DM 용) applicationContext.xml 경로를 지정할 수 있습니다 OSGI에서 dm을 사용하려고 시도하십시오.

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

('/'가 중요 할 수 있음).

관련 문제