问题
This is the way how I get OSGi service:
ServiceReference reference = bundleContext.getServiceReference(Foo.class.getName());
Foo foo=(Foo) bundleContext.getService(reference);
What happens if I don't do bunldeContext.ungetSerivice(reference)
when I finished with service:
- If I registered service manually, for example in activator
- If I use declarative service and it is Singleton
回答1:
The OSGi framework maintains a use count of each service for your bundle. If your bundle does not unget the service, it will be released automatically by the framework when either your bundle stops or the service is unregistered.
It you are using Declarative Services, SCR will handle calling unget at the appropriate time in the life cycle of the component referencing the service.
来源:https://stackoverflow.com/questions/43154532/osgi-what-happens-if-not-to-unget-service