HK2使用@Named注入实现不起作用

时间:2015-05-09 19:18:44

标签: java guice service-locator hk2

我正在尝试学习HK2以使用服务定位器模式。以下是我写的一些代码:

package org.swx.nursing.ccquerytool.file;
import org.jvnet.hk2.annotations.Contract;

@Contract
public interface FileReader {
    public void test();
}

以上是界面。以下2个测试实现如下:     包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!!!!"+ ", ");
    }
}

以下是第二个实施:     包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!!!!, 

请告知我在这里做错了什么。谢谢!

2 个答案:

答案 0 :(得分:0)

也许你把这个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>

答案 1 :(得分:0)

使用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();
    }
}