The spring-osgi default load resolves an XML file in the Meta-inf/spring directory as a spring bean configuration file, or you can specify a spring bean configuration file by adding Spring-context to MANIFEST.MF.
To publish a spring bean as an OSGi Service, simply configure it in the XML of the Spring bean:
<osgi:service id= "OSGi service ID" ref= "interface for the spring Bean" interface= "service that needs to be published as an OSGi service/>
To reference an OSGi Service in a spring bean, simply configure it in the XML of the Spring bean:
<osgi:reference ref= "A spring bean that needs to be published as an OSGi service" interface= "Service Interface"/>
It can be seen that the configuration in the DS is basically the same, but the configuration of attributes such as policy is still missing, and if you want to implement bind and unbind settings, you need to add Osgi:listener nodes under osgi:reference elements, for example:
<osgi:reference>
<osgi:listener ref= "Spring beans referencing this service"
Bind-method= "method to invoke when service is available"
Unbind-method= "Method of call for service Unavailable"/>
</osgi:reference>
Here Bind-method and Unbind-method methods are signed and slightly different when using DS, where the Bind-method and Unbind-method method signatures are as follows:
public void Onbind (service interface, Dictionary)
In the above DS demo, the com.ferry.client bundle is changed slightly, the modified part is as follows
Adding a class Tiny
public class Tiny implements commandprovider{
private iability ability;
public void setability (iability ability) {
this.ability = ability;
}
public void Onbind (iability ability,map properties) {
this.ability = ability;
}
public void Onunbind (iability ability,map properties) {
this.ability = null;
}
public void _counttinydamage (Commandinterpreter ci) {
if (ability!=null) {
ci.println (ability.getname () + "): "+ability.ability (4));
} else {
ci.println ("Ability is null!");
}
}
@Override public
String gethelp () {
return "\tcountdamage-count the damage\n";
}
}
The corresponding spring configuration file is as follows
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:osgi= "Http://www.springframework.org/schema/osgi" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework . org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/osgi http://www.springframework.org/ Schema/osgi/spring-osgi.xsd "> <bean id=" Tiny "class=" Com.ferry.hero.Tiny "> <property name=" Abi Lity "ref=" ability "/> </bean> <osgi:reference id=" Ability "interface=" com.ferry.ability.IAbility "
; <osgi:listener ref= "Tiny" bind-method= "Onbind" unbind-method= "Onunbind" ></osgi:listener> </osgi: reference> <osgi:service ref= "Tiny" interface= "Org.eclipse.osgi.framework.console.CommandProvider" > & Lt;/osgi:service> </bEans>
SPRING-DM also supports multi-service provisioning, with list and set two, configured as follows
<osgi:list id= "Spring Bean" referencing this service interface= "service Interface"/> <osgi:setid= "Spring bean referencing this service" interface= "Service Interface"/>
Hero to SPRING-DM Multi-service provided list mode, the code is as follows
public class Hero implements commandprovider{
private list<iability> abilitylist;
public void Setabilitylist (list<iability> abilitylist) {
this.abilitylist = abilitylist;
}
public void addability (iability ability) {
Abilitylist.add (ability);
System.out.println (Ability.getname ());
}
public void removeability (iability ability) {
abilitylist.remove (ability);
System.out.println (Ability.getname ());
}
public void _countdamage (Commandinterpreter ci) {
if (abilitylist!=null&&abilitylist.size ()!=0) {
for (iability ability:abilitylist) {
ci.println (ability.getname () + ":" +ability.ability (4));}}}
@Override public
String gethelp () {
return "\tcountdamage-count the damage\n";
}
}
The spring configuration file for hero is as follows
<bean id= "Hero" class= "Com.ferry.hero.Hero" >
<property name= "abilitylist" ref= "Abilitys" ></ property>
</bean>
<osgi:list id= "Abilitys" interface= "com.ferry.ability.IAbility" ></ osgi:list>
<osgi:service ref= "hero" interface= "Org.eclipse.osgi.framework.console.CommandProvider" >
</osgi:service>
Code run results can be run by the code I provided myself, here is not the screenshot
Jar Package Used
Com.springsource.org.aopalliance-1.0.0.jar
Jcl-over-slf4j-1.6.4.jar
Logback-classic-1.0.0.jar
Logback-core-1.0.0.jar
Org.eclipse.equinox.ds_1.3.1.r37x_v20110701.jar
Org.eclipse.equinox.util_1.0.300.v20110502.jar
Org.eclipse.osgi.services_3.3.0.v20110513.jar
Org.eclipse.osgi_3.7.2.v20120110-1415.jar
Org.springframework.aop-2.5.6.sec03.jar
Org.springframework.beans-2.5.6.sec03.jar
Org.springframework.context-2.5.6.sec03.jar
Org.springframework.core-2.5.6.sec03.jar
Slf4j-api-1.6.4.jar
Spring-osgi-core-1.2.1.jar
Spring-osgi-extender-1.2.1.jar
Spring-osgi-io-1.2.1.jar Spring-osgi-mock-1.2.1.jar
Click to download demo code