新蜂问题
大家好,
提前感谢您的帮助。
这是我的问题。
我想将 spring 中的一个简单 bean 实例化到 OSGI Bundle 中。
这是我的 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/spring”),但它没有改变一切......
如果我将 spring 包添加为 jar,它就有效......
我缺少新的想法。我只想做一个简单的bean实例化,我不想使用spring dm,或者我不明白一切,比如如何用scope =“prototype”实例化bean
我是否被迫使用spring dm?
此致
Hy everybody,
Thanks in advance for your help.
Here is my problem.
I want to instanciate a simple bean from spring into an OSGI Bundle.
Here is my java class :
package testspring;
public class ClassIOC {
public void helloWorld(){
System.out.println("Hello World Spring with OSGI");
}
}
Here is my activator
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;
}
}
Here is my manifest :
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"
I check into my OSGI container and I have all thoses bundles.
Each times I activate my bundle i have the following exception :
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
I have try to declare META-INF/spring directory as source, I try to access to my application Context directly (without "META-INF/spring"), it doesn't change everything....
If I add the spring bundles as jar, it works...
I'm missing of new ideas. I just want to do a simple instanciation of bean, I don't want to use spring dm, or I have don't understand everything, like how to instanciate bean with scope="prototype"
Am i forced to use spring dm ?
best regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 Spring DM,它将使生活变得更加容易,因为例如,如果您将文件放在 META-INF/spring 目录。
您还可以在 MANIFEST 中指定 applicationContext.xml 路径(对于 spring dm)(在本例中,路径以“/”开头),
因此,如果您不想在 OSGI 中使用 spring dm,请尝试使用
(也许是“/”) '很重要)。
If you use Spring DM it will make live much easier as it e.g. creates the application context for you if you place the files in the META-INF/spring directory.
You can also specify the applicationContext.xml path (for spring dm) in the MANIFEST (in this case the path starts with a '/')
So if you do not want to use spring dm in OSGI try to use
(maybe the '/' is important).
最后我找到了答案。
OSGI 并不是为了访问它的应用程序上下文而设计的。要进行实例化,最好将 bean 作为服务公开。但可以通过以下方式访问它们:
其中 com.binomed.osgi.spring.test 是我的包的 id。
不管怎样,谢谢你的回复
Finally I found an answer.
OSGI is not designed to have access to it application context. To do instantiation, it is better to expose beans as services. But it is possible to access them in the following way :
where com.binomed.osgi.spring.test is the id of my bundle.
Thanks anyway for your response