我正在尝试学习HK2来使用服务定位器模式。下面是我写的一些代码:
package org.swx.nursing.ccquerytool.file;
import org.jvnet.hk2.annotations.Contract;
@Contract
public interface FileReader {
public void test();
}上面是一个接口。下面是两个测试实现: package org.swx.nursing.ccquerytool.file;
import javax.inject.Singleton;
import org.jvnet.hk2.annotations.Service;
@Service (name="org.swx.nursing.ccquerytool.file.OcxReaderImpl")
@Singleton
class OcxReaderImpl implements FileReader{
public void test() {
// TODO Auto-generated method stub
System.out.println("OCX HelloWorld!!!!"+ ", ");
}
}下面是第二个实现: package org.swx.nursing.ccquerytool.file;
import javax.inject.Singleton;
import org.jvnet.hk2.annotations.Service;
@Service (name="org.swx.nursing.ccquerytool.file.RarReaderImpl")
@Singleton
class RarReaderImpl implements FileReader{
public void test() {
// TODO Auto-generated method stub
System.out.println("RAR HelloWorld!!!!"+ ", ");
}
}下面的类使用main()方法对此进行测试:
package org.swx.nursing.ccquerytool.file;
import javax.inject.Inject;
import javax.inject.Named;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
public class Hk2Test {
private static ServiceLocator SERVICELOCATOR = ServiceLocatorUtilities.createAndPopulateServiceLocator();
@Inject @Named ("org.swx.nursing.ccquerytool.file.OcxReaderImpl")
public static FileReader fr =SERVICELOCATOR.getService(FileReader.class);
@Inject @Named ("org.swx.nursing.ccquerytool.file.RarReaderImpl")
public static FileReader fr2 =SERVICELOCATOR.getService(FileReader.class);
public static void main(String argv[]) {
//FileReader ocxReaderService = SERVICELOCATOR.getService(FileReader.class);
//ocxReaderService.test();
fr.test();
fr2.test();
}
} 当我运行这个命令时,输出如下:
OCX HelloWorld!!!!,
OCX HelloWorld!!!!, 我期待的地方
OCX HelloWorld!!!!,
RAR HelloWorld!!!!, 请告诉我这里哪里做错了。谢谢!
发布于 2015-05-12 05:38:28
也许你可以把这段XML放入你的pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.glassfish.hk2</groupId>
<artifactId>hk2-inhabitant-generator</artifactId>
<version>2.4.0-b20</version>
<executions>
<execution>
<id>generate-inhabitants</id>
<goals>
<goal>generate-inhabitants</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>发布于 2017-01-12 02:56:30
使用Hk2Utilities进行示例。一个实时的节省!!
package org.swx.nursing.ccquerytool.file;
import javax.inject.Inject;
import javax.inject.Named;
import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.hk2.api.ServiceLocatorFactory;
import org.jvnet.hk2.annotations.Service;
import gov.va.oia.HK2Utilities.HK2RuntimeInitializer;
@Service
public class Hk2Test {
@Inject
@Named("org.swx.nursing.ccquerytool.file.OcxReaderImpl")
FileReader fr;
@Inject
@Named("org.swx.nursing.ccquerytool.file.RarReaderImpl")
FileReader fr2;
public void test() {
fr.test();
fr2.test();
}
public static void main(String argv[]) throws Exception {
HK2RuntimeInitializer.init("Test", false, "org.swx.nursing.ccquerytool.file" );
ServiceLocator locator = ServiceLocatorFactory.getInstance().create("Test");
Hk2Test app = locator.getService(Hk2Test.class);
app.test();
}
}https://stackoverflow.com/questions/30144058
复制相似问题