在此姿势中,我们将采用
使用OSGi进行的
第一个实现,并使用
Spring Dynamic Modules改进应用程序。
Spring动态模块(Spring Dm)使基于OSGi的应用程序的开发更加容易。 这样,服务的部署就容易得多。 您可以像其他任何Spring bean一样注入服务。
因此,让我们从Spring dm开始。
首先,您需要下载Spring Dm Distribution 。 在本文中,我使用了具有依赖关系的发行版,而我将仅使用以下库:
com.springsource.net.sf.cglib-2.1.3.jar
com.springsource.org.aopalliance-1.0.0.jar
log4j.osgi-1.2.15-SNAPSHOT.jar
com.springsource.slf4j.api-1.5.0.jar
com.springsource.slf4j.log4j-1.5.0.jar
com.springsource.slf4j.org.apache.commons.logging-1.5.0.jar
org.springframework.aop-2.5.6.SEC01.jar
org.springframework.beans-2.5.6.SEC01.jar
org.springframework.context-2.5.6.SEC01.jar
org.springframework.core-2.5.6.SEC01.jar
spring-osgi-core-1.2.1.jar
spring-osgi-extender-1.2.1.jar
spring-osgi-io-1.2.1.jar
当然,您可以将Spring 2.5.6库替换为Spring 3.0库。 但是对于本文而言,Spring 2.5.6就足够了。
因此,从服务捆绑开始。 回想一下,该捆绑软件导出了一项服务:
package com.bw.osgi.provider.able;
public interface HelloWorldService {
void hello();
}
package com.bw.osgi.provider.impl;
import com.bw.osgi.provider.able.HelloWorldService;
public class HelloWorldServiceImpl implements HelloWorldService {
@Override
public void hello(){
System.out.println("Hello World !");
}
}
这里没有要做的任何更改。 现在,我们可以看到激活器:
package com.bw.osgi.provider;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import com.bw.osgi.provider.able.HelloWorldService;
import com.bw.osgi.provider.impl.HelloWorldServiceImpl;
public class ProviderActivator implements BundleActivator {
private ServiceRegistration registration;
@Override
public void start(BundleContext bundleContext) throws Exception {
registration = bundleContext.registerService(
HelloWorldService.class.getName(),
new HelloWorldServiceImpl(),
null);
}
@Override
public void stop(BundleContext bundleContext) throws Exception {
registration.unregister();
}
}
因此,在这里,我们将简单化。 让我们删除这个类,它对于Spring Dm不再有用。
我们将让Spring Dm为我们导出捆绑包。 我们将为此捆绑包创建一个Spring上下文。 我们只需要在文件夹META-INF / spring中创建一个文件provider-context.xml即可。 这是XML文件中的简单上下文,但是我们使用新的名称空间注册服务“ http://www.springframework.org/schema/o