Put my interfaces and implementations in a bundle
Interface code:
Public interface myservice {
Public String gethello ();
}
Implementation Code:
Import org. osgi. Service. component. componentcontext;
Import osgi. Service. myservice;
Public class myserviceimpl implements myservice {
Protected void activate (componentcontext context ){
System. Out
. Println ("myservice component active, within the bundle lifecircle .");
}
Public void deactivate (componentcontext context) throws exception {
System. Out
. Println ("myservice component deactive, within the bundle lifecircle .");
}
@ Override
Public String gethello (){
// Todo auto-generated method stub
Return "no spring osgi ";
}
}
Call implementation is another bundle
Import org. Eclipse. osgi. Framework. Console. commandprovider;
Import osgi. Service. myservice;
Public class testosgi implements commandprovider {
Myservice MS;
Public synchronized void setrunnable (myservice R ){
MS = R;
System. Out. println (Ms. gethello ());
System. Out. println ("this is testosgi declarative service .");
}
Public synchronized void unsetrunnable (myservice R ){
MS = NULL;
}
@ Override
Public String gethelp (){
// Todo auto-generated method stub
Return NULL;
}
}
The problem lies in the implementation of the call. I must implement an interface (such as commandprovider), which is embodied in the XML file as the provider interface of the service. At this time, you can see the output. If I do not implement the interface, after running it, you can see that the output is active through the command SS, but there is no output, that is to say, testosgi does not execute the setrunnable method and myserviceimpl does not execute the activate method.
Finally, we know that the interface must be implemented as a Service. Therefore, testosgi is not used as a Service. Therefore, you can delete a service segment in the bundle XML, but use it as a common component.