import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
@Component(name = "ServerComponent", immediate = false)
public class Server {
@Reference(cardinality = ReferenceCardinality.MANDATORY)
System system;
@Reference(cardinality = ReferenceCardinality.MANDATORY)
protected void bindFunction(Function func) {
}
@Activate
public void activate() {
}
}
您可以在激活方法中调用 doSomething()。Osgi 使用@Reference 注解保证方法调用顺序。
如果获取系统和函数引用,则激活方法将被 OSGi 环境调用。您可以在 activate() 方法中调用 system.doSomething() 方法。@Reference(cardinality = ReferenceCardinality.MANDATORY) 注解表示在获取到引用后调用activate方法。