私は非常に単純なサービスプロバイダとコンシューマを持っています。何らかの理由で私の消費者がプロバイダのサービスを利用するという問題を解決できません。ここ プロバイダのバンドルソースコード:カラフでのサービスに不満足な(参考) - 間違いはどこですか? (OSGi、宣言型サービス、注釈)
package test;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
@Component (name = "MyProvider", immediate = true)
public class TestClass implements SimpleMathI {
public TestClass() {
System.out.println("contructing TestClass");
}
@Activate
protected void activate(ComponentContext c, BundleContext b) {
System.out.println("activate testClass ");
}
@Deactivate
protected void deactivate() {
System.out.println("de-activate testClass");
}
@Override
public void doSimpleAdd(int x, int y) {
System.out.println("Result(TestClass): " + (x + y));
}
@Override
public void doSimpleSubstract(int x, int y) {
System.out.println("Result(TestClass): " + (x - y));
}
}
それは成分MyProviderを登録し、ここでサービスtest.SimpleMathI(listed in karaf)
は、消費者である:
Iがサービスを参照しない場合SimpleMathIが、それは正常に動作するだけConfigurationAdmin!
package test;
import org.osgi.framework.BundleContext;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
@Component (name = "MyConsumer", immediate = true, configurationPolicy = ConfigurationPolicy.OPTIONAL)
public class TestClass2 {
public TestClass2() {
System.out.println("contructing TestClass2");
}
@Reference (bind = "bind", unbind = "unbind")
ConfigurationAdmin cm; // works
@Reference (bind = "bindSimpleMathI", unbind = "unbindSimpleMathI")
SimpleMathI simpleMath; // does not work, see screenshot
@Activate
protected void activate(ComponentContext c, BundleContext b) {
System.out.println("activate testClass2");
// simpleMath.doSimpleAdd(20, 25);
}
@Deactivate
protected void deactivate() {
System.out.println("de-activate testClass2");
}
protected void bind(ConfigurationAdmin a) {
System.out.println("binding");
}
protected void unbind(ConfigurationAdmin a) {
System.out.println("un-binding");
}
protected void bindSimpleMathI(SimpleMathI a) {
System.out.println("binding!!");
}
protected void unbindSimpleMathI(SimpleMathI a) {
System.out.println("un-binding!!");
}
}
、ここKaraf webconsoleで出力されます。
私は十分に探知しましたが、まだ私が逃したものを理解することはできません。コードが非常に単純で透明なので、不思議です。だから、プロバイダや消費者が間違って実装したものは何ですか?
私はあなたが簡単なエラーをしたと思うKaraf 4.0.7、使用なしApacheのフェリックス、純粋なOSGiのR6の宣言型サービス
今、私はそれを理解しました。また、ほとんどの人が3つのパッケージアプローチを使用していることに気づいた。それは確かに意味があります。 – neodix