2013-02-26 69 views
2

在开发Spring和OSGi的应用程序时,我遇到了一个问题。在我的应用程序中,我想使用Spring beans配置文件,但是我不知道如何强制使用特定的配置文件。我使用过一次,弹簧配置文件,但在Web应用程序中,我遵循本教程:http://java.dzone.com/articles/spring-31-environment-profilesSpring Beans Profiles和OSGI

但我不知道如何做到这一点在OSGi环境,因为我无法找到ApplicationContextInitializer

回答

2

一些相当于我不知道,如果弹簧DM支持,作为其基于旧的春天。 Spring 3.1被添加到spring 3.1中,spring-dm是一个死了的项目。 http://www.springsource.org/osgi

0

Spring-DM可能不支持较新版本的Spring,但Eclipse Gemini Blueprint会。如果您可以使用Spring 3.1.x或更高版本和Blueprint,则可能可以使Spring配置文件正常工作。一种方法是使用extend the Blueprint Extender bundle自己实现OsgiApplicationContextCreator,根据需要配置ApplicationContextEnvironment活动配置文件。例如,请考虑以下自定义BlueprintContainerCreator实现:

public class MyOsgiApplicationContextCreator extends BlueprintContainerCreator { 
    @Override 
    public DelegatedExecutionOsgiBundleApplicationContext createApplicationContext(
     BundleContext bundleContext) throws Exception { 
    DelegatedExecutionOsgiBundleApplicationContext applicationContext = super 
     .createApplicationContext(bundleContext); 

    if (null == applicationContext) { 
     // non-spring/blueprint bundles will not build an ApplicationContext 
     return null; 
    } 

    // determine environment profile here... 
    applicationContext.getEnvironment().setActiveProfiles("myProfile"); 

    return applicationContext; 
    } 
} 

您需要把这个到附在蓝图扩展包片段束。执行以下操作:

您需要创建一个包含三个文件的包:META-INF/MANIFEST.MF,META-INF/spring/extender/extender.xml(可以将xml文件命名为一个xml扩展名,但必须位于META-INF/spring/extender文件夹中)以及您的实现。您的MANIFEST.MF文件将需要包含org.eclipse.gemini.blueprint.extender的OSGi清单头文件碎片主机。如果您正在使用Maven的捆绑,插件,插件配置会是这个样子:

... 
<plugin> 
    <groupId>org.apache.felix</groupId> 
    <artifactId>maven-bundle-plugin</artifactId> 
    <version>2.3.5</version> 
    <extensions>true</extensions> 
    <configuration> 
    <instructions> 
     <Fragment-Host>org.eclipse.gemini.blueprint.extender</Fragment-Host> 
     <Export-Package>your.package,!*</Export-Package> 
     <Import-Package>org.osgi.framework,org.springframework.core.env,!*</Import-Package> 
    </instructions> 
    </configuration> 
</plugin> 
... 

你extender.xml文件将需要使用的applicationContextCreator一个名称来定义您的自定义OsgiApplicationContextCreator豆。该文件可能如下所示:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 
    <util:properties id="extenderProperties"> 
    <prop key="shutdown.wait.time">30000</prop> 
    </util:properties> 
    <bean id="applicationContextCreator" class="your.package.MyOsgiApplicationContextCreator"/> 
</beans> 

然后将该软件包部署到您的环境中。您可能需要重新启动Blueprint OSGi软件包(或您的服务器),具体取决于与Blueprint软件包相关的此片段软件包的安装顺序。