当前位置:   article > 正文

OSGI企业应用开发(七)细说Blueprint & Gemini Blueprint(二)_osgi core 4.3.1 与哪个版本的service blueprint兼容

osgi core 4.3.1 与哪个版本的service blueprint兼容

上篇文章介绍了标准的Blueprint 规范与 Gemini Blueprint如何自定义Bean配置文件路径,本文接着上篇文章继续介绍Blueprint的使用。

一、Bean的配置

前面提到过,Gemini Blueprint即实现了Blueprint 规范,又兼容spring DM风格的配置,本节我们再来了解一下Blueprint与Spring DM在Bean的配置上的差异,具体如下表所示:
(注意:这两种风格Gemini Blueprint都支持。)
这里写图片描述
如上图所示,Spring DM风格的Bean配置和Blueprint还是有一些差异的,首先体现在根元素和命名空间声明上,Spring DM根元素为<beans>,命名空间声明如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
  5. default-lazy-init="true">
  6. </beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

而Blueprint根元素为<blueprint>,命名空间声明如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" default-activation="lazy">
  3. </blueprint>
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4
  • 1
  • 2
  • 3
  • 4

相比之下,Blueprint的配置要稍微简单些,除了根元素和命名空间声明外,Bean的配置的主要差异如下:

1、Spring DM使用根元素的default-lazy-init属性指定IoC容器启动时是否立刻实例化所有的Bean,而Blueprint对应的属性为default-activation。

2、Spring DM可以通过default-init-method和default-destroy-method属性指定Bean实例化和销毁时默认的回调方法,还可以通过default-autowire, default-autowire-candidates属性指定Bean的自动装配策略,而这些属性Blueprint是不支持的。

3、Spring DM的Bean支持使用name属性,还支持使用alias属性为Bean指定一个别名,Blueprint是不支持的。

4、Spring DM 配置Bean的scope属性支持singleton, prototype, request, session, bundle等取值,而Blueprint Bean的scope属性仅仅支持singleton, prototype。

5、Spring DM通过Bean的lazy-init属性指定IoC容器启动时是否实例化该Bean,Blueprint对应的属性为activation。

6、Spring DM通过factory-bean属性指定工厂Bean,而Blueprint对应的属性为factory-ref。

7、Spring DM可以使用parent属性继承一个Bean的配置,而Blueprint不支持。

8、Spring DM 通过autowire, autowire-candidate属性指定Bean的自动装配策略,而Blueprint也是不支持的。

9、构造器注入时,Spring DM使用的标签为<constructor-arg>,而Blueprint使用的标签为<argument>

Spring DM 和 Blueprint 风格的Bean的配置差异主要体现在上面几个方面,接下来我们开始学习Bean的注册与引用。

二、Bundle中Bean的注册与引用

前面的文章中有提到过,OSGI应用的每一个Bundle都是使用不同的类加载器加载,OSGI应用并不像普通的Java应用一样,有全局的Classpath的概念,所以如果需要在一个Bundle中使用另外一个Bundle中的类,则需要另外一个Bundle导出这些类,然后在用到这些类的Bundle中导入即可。而OSGI应用对象的共享则是通过服务注册的方式实现。

使用Blueprint將Spring框架整合到OSGI中,ApplicationContext和普通的Java应用也有所差异。一般来说,普通Java应用的ApplicationContext只有一个,而且是全局的,而在OSGI应用中,每个Bundle对应一个单独个ApplicationContext,如下图所示:
这里写图片描述

从上图可以清晰的看出,每个Bundle对应的ApplicationContext是不同的,也就是说每个Bundle中配置的Bean是不共享的,例如当我们在A Bundle中定义了一个Bean,然后以属性注入的方式注入到B Bundle中的一个Bean中,这就有问题了,那么如何解决这个问题呢,Spring DM给出的解决方案是將Bean注册为服务,然后在其他Bundle中引用Bean。接下来我们就来学习一下Bean的注册与引用机制。

Spring DM Bean的注册与引用机制实际上有个缺点,它要求被注册的Bean必须实现了某个接口,为了方便说明问题,笔者在前面文章中搭建好的环境中进行演示,首先我们可以在com.csdn.osgi.common工程中新建一个com.csdn.osgi.domain.Employee接口,表示员工的信息,内容如下:

  1. package com.csdn.osgi.domain;
  2. public interface Employee {
  3. public String getName();
  4. public int getAge();
  5. public String getAddress();
  6. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

然后定义一个具体的类来实现Employee 接口,比如我们有实现类com.csdn.osgi.domain.Programmer,如下:

  1. package com.csdn.osgi.domain;
  2. public class Programmer implements Employee{
  3. private String name; // 员工姓名
  4. private int age; // 员工年龄
  5. private String address; // 居住地址
  6. private double salary; // 员工薪资
  7. public String getName() {
  8. return name;
  9. }
  10. public void setName(String name) {
  11. this.name = name;
  12. }
  13. public int getAge() {
  14. return age;
  15. }
  16. public void setAge(int age) {
  17. this.age = age;
  18. }
  19. public String getAddress() {
  20. return address;
  21. }
  22. public void setAddress(String address) {
  23. this.address = address;
  24. }
  25. public double getSalary() {
  26. return salary;
  27. }
  28. public void setSalary(double salary) {
  29. this.salary = salary;
  30. }
  31. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

接着在com.csdn.osgi.common工程中,新建 META-INF/spring/employee.xml文件,在新建的文件中,配置一个Programmer类的实例,如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
  5. default-lazy-init="false">
  6. <bean id="programmer" class="com.csdn.osgi.domain.Programmer">
  7. <property name="name" value="Smith"/>
  8. <property name="age" value="25"/>
  9. <property name="address" value="Nanjin"/>
  10. <property name="salary" value="2000"/>
  11. </bean>
  12. </beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

如上面代码所示,我们在com.csdn.osgi.common这个Bundle中通过Spring实例化了一个Programmer对象,其他Bundle中如果需要注入这个实例,该怎样做呢?

接下来我们在com.csdn.osgi.helloworld工程中,新建一个Company类,表示公司信息,例如:

  1. package com.csdn.osgi.domain;
  2. public class Company {
  3. private String name; // 公司名称
  4. private Employee emp; // 假设只有一个员工
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public Employee getEmp() {
  12. return emp;
  13. }
  14. public void setEmp(Employee emp) {
  15. this.emp = emp;
  16. }
  17. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

项目结构如下图所示:
这里写图片描述
需要注意的是,Company类在com.csdn.osgi.helloworld这个Bundle中,而Programmer类在com.csdn.osgi.common中,我们需要在com.csdn.osgi.common这个Bundle中导出com.csdn.osgi.domain这个包,具体做法很简单,可以將com.csdn.osgi.common工程中的MANIFEST.MF文件,修改成如下内容:

  1. Manifest-Version: 1.0
  2. Bundle-ManifestVersion: 2
  3. Bundle-Name: Common
  4. Bundle-SymbolicName: com.csdn.osgi.common
  5. Bundle-Version: 1.0.0.qualifier
  6. Bundle-Activator: com.csdn.osgi.common.Activator
  7. Bundle-Vendor: CSDN
  8. Bundle-RequiredExecutionEnvironment: JavaSE-1.8
  9. Import-Package: org.osgi.framework;version="1.3.0"
  10. Export-Package: com.csdn.osgi.domain
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

其中Export-Package: com.csdn.osgi.domain为新增内容,解决依赖问题后,我们可以在com.csdn.osgi.helloworld工程中,新建一个config/company.xml文件(注意:前面我们通过Spring-Context头自定义了Bean配置文件路径为config/*.xml),然后配置一个Company实例,内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
  5. default-lazy-init="false">
  6. <bean id="company" class="com.csdn.osgi.domain.Company">
  7. <property name="name" value="CSDN"/>
  8. <property name="emp" ref="programmer"/>
  9. </bean>
  10. </beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

这里我们直接將com.csdn.osgi.common Bundle中配置的employee对象注入,然后启动OSGI容器,看看会有什么问题,控制台输出日志如下:

  1. 十二月 25, 2016 9:08:05 下午 org.eclipse.gemini.blueprint.extender.internal.boot.ChainActivator <init>
  2. 信息: Blueprint API detected; enabling Blueprint Container functionality
  3. 十二月 25, 2016 9:08:05 下午 org.eclipse.gemini.blueprint.extender.internal.activator.LoggingActivator start
  4. 信息: Starting [org.eclipse.gemini.blueprint.extender] bundle v.[2.0.0.M02]
  5. osgi> 十二月 25, 2016 9:08:05 下午 org.eclipse.gemini.blueprint.extender.internal.support.ExtenderConfiguration start
  6. 信息: No custom extender configuration detected; using defaults...
  7. 十二月 25, 2016 9:08:05 下午 org.springframework.scheduling.timer.TimerTaskExecutor afterPropertiesSet
  8. 信息: Initializing Timer
  9. hello world!
  10. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.extender.support.DefaultOsgiApplicationContextCreator createApplicationContext
  11. 信息: Discovered configurations {osgibundle:/META-INF/spring/*.xml} in bundle [Common (com.csdn.osgi.common)]
  12. Hello World!!
  13. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.extender.support.DefaultOsgiApplicationContextCreator createApplicationContext
  14. 信息: Discovered configurations {config/*.xml} in bundle [Helloworld (com.csdn.osgi.helloworld)]
  15. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.extender.internal.blueprint.activator.support.BlueprintContainerCreator createApplicationContext
  16. 信息: Discovered configurations {bundleentry://43.fwk1878169648/OSGI-INF/blueprint/beans.xml} in bundle [Helloworld (com.csdn.osgi.helloworld)]
  17. 十二月 25, 2016 9:08:06 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
  18. 信息: Refreshing OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.common, config=osgibundle:/META-INF/spring/*.xml): startup date [Sun Dec 25 21:08:06 CST 2016]; root of context hierarchy
  19. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext unpublishContextAsOsgiService
  20. 信息: Application Context service already unpublished
  21. 十二月 25, 2016 9:08:06 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
  22. 信息: Refreshing OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.helloworld, config=config/*.xml): startup date [Sun Dec 25 21:08:06 CST 2016]; root of context hierarchy
  23. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext unpublishContextAsOsgiService
  24. 信息: Application Context service already unpublished
  25. 十二月 25, 2016 9:08:06 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
  26. 信息: Refreshing OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.helloworld, config=bundleentry://43.fwk1878169648/OSGI-INF/blueprint/beans.xml): startup date [Sun Dec 25 21:08:06 CST 2016]; root of context hierarchy
  27. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext unpublishContextAsOsgiService
  28. 信息: Application Context service already unpublished
  29. 十二月 25, 2016 9:08:06 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  30. 信息: Loading XML bean definitions from OSGi resource[bundleentry://43.fwk1878169648/OSGI-INF/blueprint/beans.xml|bnd.id=43|bnd.sym=com.csdn.osgi.helloworld]
  31. 十二月 25, 2016 9:08:06 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  32. 信息: Loading XML bean definitions from URL [bundleentry://43.fwk1878169648/config/beans.xml]
  33. 十二月 25, 2016 9:08:06 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  34. 信息: Loading XML bean definitions from URL [bundleentry://37.fwk1878169648/META-INF/spring/beans.xml]
  35. 十二月 25, 2016 9:08:06 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  36. 信息: Loading XML bean definitions from URL [bundleentry://43.fwk1878169648/config/company.xml]
  37. 十二月 25, 2016 9:08:06 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  38. 信息: Loading XML bean definitions from URL [bundleentry://37.fwk1878169648/META-INF/spring/dmconfig.xml]
  39. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor stageOne
  40. 信息: No outstanding OSGi service dependencies, completing initialization for OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.helloworld, config=bundleentry://43.fwk1878169648/OSGI-INF/blueprint/beans.xml)
  41. 十二月 25, 2016 9:08:06 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  42. 信息: Loading XML bean definitions from URL [bundleentry://43.fwk1878169648/config/dmconfig.xml]
  43. 十二月 25, 2016 9:08:06 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  44. 信息: Loading XML bean definitions from URL [bundleentry://37.fwk1878169648/META-INF/spring/employee.xml]
  45. 十二月 25, 2016 9:08:06 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
  46. 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@48766948: defining beans [helloWorld,blueprintBundle,blueprintBundleContext,blueprintContainer,blueprintConverter]; root of factory hierarchy
  47. ================Hello World================
  48. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor stageOne
  49. 信息: No outstanding OSGi service dependencies, completing initialization for OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.helloworld, config=config/*.xml)
  50. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor stageOne
  51. 信息: No outstanding OSGi service dependencies, completing initialization for OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.common, config=osgibundle:/META-INF/spring/*.xml)
  52. 十二月 25, 2016 9:08:06 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
  53. 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@6ec2b45d: defining beans [object,length,buffer,current-time,list,programmer]; root of factory hierarchy
  54. 十二月 25, 2016 9:08:06 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
  55. 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@30a0eb63: defining beans [helloWorld1,helloWorld2,company]; root of factory hierarchy
  56. ================Hello World================
  57. ================Hello World================
  58. =========Company=========
  59. 十二月 25, 2016 9:08:06 下午 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
  60. 信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@30a0eb63: defining beans [helloWorld1,helloWorld2,company]; root of factory hierarchy
  61. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.extender.internal.support.DefaultOsgiBundleApplicationContextListener onOsgiApplicationEvent
  62. 严重: Application context refresh failed (OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.helloworld, config=config/*.xml))
  63. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'company' defined in URL [bundleentry://43.fwk1878169648/config/company.xml]: Cannot resolve reference to bean 'programmer' while setting bean property 'emp'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'programmer' is defined
  64. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
  65. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
  66. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1305)
  67. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1067)
  68. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
  69. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
  70. at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
  71. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
  72. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
  73. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
  74. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557)
  75. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)
  76. at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.access$1600(AbstractDelegatedExecutionApplicationContext.java:60)
  77. at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:325)
  78. at org.eclipse.gemini.blueprint.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)
  79. at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:290)
  80. at org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:137)
  81. at java.lang.Thread.run(Thread.java:745)
  82. Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'programmer' is defined
  83. at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:504)
  84. at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1041)
  85. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:273)
  86. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
  87. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
  88. ... 17 more
  89. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext unpublishContextAsOsgiService
  90. 信息: Application Context service already unpublished
  91. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.blueprint.container.support.BlueprintContainerServicePublisher registerService
  92. 信息: Publishing BlueprintContainer as OSGi service with properties {Bundle-SymbolicName=com.csdn.osgi.helloworld, Bundle-Version=1.0.0.qualifier, osgi.blueprint.container.version=1.0.0.qualifier, osgi.blueprint.container.symbolicname=com.csdn.osgi.helloworld}
  93. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.extender.internal.support.DefaultOsgiBundleApplicationContextListener onOsgiApplicationEvent
  94. 信息: Application context succesfully closed (OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.helloworld, config=config/*.xml))
  95. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor fail
  96. 严重: Unable to create application context for [com.csdn.osgi.helloworld], unsatisfied dependencies: none
  97. org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'company' defined in URL [bundleentry://43.fwk1878169648/config/company.xml]: Cannot resolve reference to bean 'programmer' while setting bean property 'emp'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'programmer' is defined
  98. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
  99. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
  100. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1305)
  101. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1067)
  102. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511)
  103. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450)
  104. at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:290)
  105. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
  106. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:287)
  107. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
  108. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:557)
  109. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:842)
  110. at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.access$1600(AbstractDelegatedExecutionApplicationContext.java:60)
  111. at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext$4.run(AbstractDelegatedExecutionApplicationContext.java:325)
  112. at org.eclipse.gemini.blueprint.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)
  113. at org.eclipse.gemini.blueprint.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:290)
  114. at org.eclipse.gemini.blueprint.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor$CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:137)
  115. at java.lang.Thread.run(Thread.java:745)
  116. Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'programmer' is defined
  117. at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:504)
  118. at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1041)
  119. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:273)
  120. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:189)
  121. at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
  122. ... 17 more
  123. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext publishContextAsOsgiServiceIfNecessary
  124. 信息: Publishing application context as OSGi service with properties {org.eclipse.gemini.blueprint.context.service.name=com.csdn.osgi.helloworld, org.springframework.context.service.name=com.csdn.osgi.helloworld, Bundle-SymbolicName=com.csdn.osgi.helloworld, Bundle-Version=1.0.0.qualifier}
  125. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.extender.internal.support.DefaultOsgiBundleApplicationContextListener onOsgiApplicationEvent
  126. 信息: Application context successfully refreshed (OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.helloworld, config=bundleentry://43.fwk1878169648/OSGI-INF/blueprint/beans.xml))
  127. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.context.support.AbstractOsgiBundleApplicationContext publishContextAsOsgiServiceIfNecessary
  128. 信息: Publishing application context as OSGi service with properties {org.eclipse.gemini.blueprint.context.service.name=com.csdn.osgi.common, org.springframework.context.service.name=com.csdn.osgi.common, Bundle-SymbolicName=com.csdn.osgi.common, Bundle-Version=1.0.0.qualifier}
  129. 十二月 25, 2016 9:08:06 下午 org.eclipse.gemini.blueprint.extender.internal.support.DefaultOsgiBundleApplicationContextListener onOsgiApplicationEvent
  130. 信息: Application context successfully refreshed (OsgiBundleXmlApplicationContext(bundle=com.csdn.osgi.common, config=osgibundle:/META-INF/spring/*.xml))
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133

错误信息意思很明显,名称为programmer这个Bean未定义,原因是该Bean定义在com.csdn.osgi.common这个Bundle中,在com.csdn.osgi.helloworld Bundle中无法直接访问,接下来我们通过Bean的注册和引用来解决该问题。

在com.csdn.osgi.common工程中,新建一个META-INF/spring/dmconfig.xml文件,用来注册Bean,内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:osgi="http://www.eclipse.org/gemini/blueprint/schema/blueprint"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.eclipse.org/gemini/blueprint/schema/blueprint
  8. http://www.eclipse.org/gemini/blueprint/schema/blueprint/gemini-blueprint.xsd">
  9. <osgi:service id="employee" ref="programmer" interface="com.csdn.osgi.domain.Employee" />
  10. </beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

然后在com.csdn.osgi.helloworld Bundle中,新建config/dmconfig.xml文件,我们在该文件中引入其他Bundle中注册的Bean,内容如下:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:osgi="http://www.eclipse.org/gemini/blueprint/schema/blueprint"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.eclipse.org/gemini/blueprint/schema/blueprint
  8. http://www.eclipse.org/gemini/blueprint/schema/blueprint/gemini-blueprint.xsd">
  9. <osgi:reference id="employee" interface="com.csdn.osgi.domain.Employee" />
  10. <alias name="employee" alias="programmer"/>
  11. </beans>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

需要注意的是,<osgi:service/>标签的id属性值不能和Bean的id相同。上面的配置中我们使用<alias/>标签为employee指定别名为programmer,然后重启OSGI容器,会发现com.csdn.osgi.common Bundle中定义的名称为programmer的Bean成功注入到Company类中。

Bean的注册与引用机制对项目开发非常重要,在后面整合Mybatis时,我们可以將用于操作数据库的SqlSessionTemplate注册为服务,然后在其他Bundle就可以引用该服务操作数据库了。

本文源码地址:http://download.csdn.net/detail/rongbo_j/9720886

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/587191
推荐阅读
相关标签
  

闽ICP备14008679号