Jersey 2.x中的依赖注入

时间:2018-07-17 14:16:35

标签: java rest dependency-injection jersey hk2

我定义了AbstractBinder并将其注册到我的JAX-RS应用程序中。活页夹指定了依赖项注入应如何创建我的类。

public class AppBinder extends AbstractBinder {
@Override
protected void configure() {
     bind(TranslationImportServiceImpl.class).to(TranslationImportService.class);
    bindAsContract(ImportCandidateBuilder.class);
    bind(DomainsBusinessServices.class)
            .to(IDomainsBusinessServices.class).in(Singleton.class);
    bind(CodeBusinessService.class)
            .to(ICodeBusinessService.class).in(Singleton.class);
    bind(LangBusinessService.class)
            .to(ILangBusinessService.class).in(Singleton.class);
    bind(TranslationBusinessService.class)
            .to(ITranslationBusinessService.class).in(Singleton.class);
    bind(SessionImpl.class)
            .to(Session.class).in(Singleton.class);
    bind(SessionFactoryImpl.class)
            .to(SessionFactory.class).in(Singleton.class);
    bind(CriteriaImpl.class)
            .to(Criteria.class).in(Singleton.class);
    bindAsContract(DefaultBusinessService.class);
}

我实现了Application类(在web.xml的init参数中指定)。

public class Application extends ResourceConfig {
public Application() {
    register(new AppBinder());
    packages(true, "web.rest");
}

web.xml

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>web.rest</param-value>
    </init-param>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>web.rest.Application</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

然后在我的RestService注入正常的地方

@Path("/call")
public class RestService {

@Inject
private TranslationImportService translationImportService;

@POST
@Path("/post")
@Consumes({MediaType.MULTIPART_FORM_DATA})
   public Response upload(@FormDataParam("file") InputStream fileInputStream,
                          @FormDataParam("file") FormDataContentDisposition fileMetaData) throws Exception {

        Map<String, TranslationImportResult> importResultMap = new HashMap<String, TranslationImportResult>();

        try {
            Map<String, InputStream> extractedFiles = extractFiles(fileMetaData.getFileName(), fileInputStream);

            Set<Entry<String, InputStream>> entrySet = extractedFiles.entrySet();
            TranslationImportResult importResult;
            for (Entry<String, InputStream> entry : entrySet) {
                importResult = null;

                if (entry.getKey().endsWith(".xml") || entry.getKey().endsWith(".xlf")) {

                    importResult = translationImportService.importTranslations(entry.getKey(), entry.getValue(), 1320L, "admin", true);
                } else {
                    logger.warn("Cannot process file (wrong extension): ", entry.getKey());
                    importResult = new TranslationImportResult(TranslationImportStatus.FAILURE, -1, -1);
                }
                importResultMap.put(entry.getKey(), importResult);
            }
        } catch (Exception e) {
            logger.error("A problem with the file extention occured! \n", e);
        }
        return Response.ok("File uploaded successfully").build();
    }

我将接口TranslationImportService注释为@Contract

@Contract
public interface TranslationImportService

,其实现为@Service

@Service
public class TranslationImportServiceImpl implements TranslationImportService{

@Inject
private ImportCandidateBuilder importCandidateBuilder;

@Override
public TranslationImportResult importTranslations(String inputName, InputStream inputStream, Long domainId,
                                                  String username, boolean allOrNothing) throws Exception {
    TranslationImportStatus ret;
    InputDeserializer inputDeserializer = InputDeserializerFactory.newInputDeserializer(inputName);
    List<InputTranslationBean> translationBeans = inputDeserializer.deserialize(inputStream);
    List<ImportCandidate> candidates = importCandidateBuilder.buildCandidates(domainId, translationBeans);
    insertStartImportEvent(candidates, inputName);
    translationImportOrchestrator.performTranslationsImport(candidates, username, allOrNothing);
    TranslationImportResult importResult = buildImportResult(translationImportOrchestrator);
    insertStopImportEvent(candidates, inputName);
    return importResult;
}

问题出在注入importCandidateBuilder中。尽管我已将其绑定,但它会引发异常:

org.glassfish.hk2.api.UnsatisfiedDependencyException:在Injectee上没有可用于注入的对象(r​​equiredType = ImportCandidateBuilder,parent = TranslationImportServiceImpl ...)

ImportCandidateBuilderClass:

@Service
public class ImportCandidateBuilder {

    private static Logger logger = LoggerFactory.getLogger(ImportCandidateBuilder.class);

    private IDomainsBusinessServices domainService;
    private ICodeBusinessService codeService;
    private ILangBusinessService vlService;
    private ITranslationBusinessService translationService;

    public ImportCandidateBuilder(IDomainsBusinessServices domainService, ICodeBusinessService codeService,
                                  ILangBusinessService vlService, ITranslationBusinessService translationService) {
        this.domainService = domainService;
        this.codeService = codeService;
        this.vlService = vlService;
        this.translationService = translationService;
    }

    public List<ImportCandidate> buildCandidates(Long domainId, List<InputTranslationBean> inputBeans) {
        Validate.notNull(domainId, "arg [domainId] is null");
        Validate.notEmpty(inputBeans, "arg [translations] is null or empty");

        List<ImportCandidate> ret = new ArrayList<>();

        Domains domain = fetchDomain(domainId);

        for (InputTranslationBean inputBean : inputBeans) {
            Tables table = fetchTable(domain, inputBean.getTableName());
            Codes code = (table == null) ? null : fetchCode(table, inputBean.getCodeName());
            Vl lang = fetchVl(inputBean.getLang());
            Trad translation = (code == null) ? null : fetchTranslation(code, lang);
            String transText = inputBean.getTranslation();
            String instructions = inputBean.getInstructions();
            ImportCandidate candidate = new ImportCandidate(domain, table, code, lang, translation, transText, instructions, inputBean);

            logger.debug("New import candidate built: [{}]", candidate);
            ret.add(candidate);
        }
        return ret;
    }
}

当我调用方法getCurrentSession()时,抛出一个无法注入SessionFactory的异常;当我尝试为DefaultBusinessService创建和注入构造函数时,sessionfactory返回为null。我也绑定了类。

@Service
public class DomainsBusinessServices extends DefaultBusinessService 
                                     implements IDomainsBusinessServices
{
public Domains getDomainById(Long domainId, boolean fetchLangs,
            boolean fetchTables, boolean fetchCodes) {
        Criteria domainCriteria = getCurrentSession().createCriteria(Domains.class);
        domainCriteria.add(Restrictions.idEq(domainId));
        if(fetchLangs){
            domainCriteria.setFetchMode("Vls", FetchMode.JOIN);
        }
        if(fetchTables){
            domainCriteria.setFetchMode("Tableses", FetchMode.JOIN);            
        }
        if(fetchCodes){
            domainCriteria.setFetchMode("Codeses", FetchMode.JOIN);         
        }
        Domains domainVL = (Domains)domainCriteria.uniqueResult();
        if (domainVL != null) {
            Hibernate.initialize(domainVL.getVls());
        }
        return domainVL;
    }
}


@Service
public class DefaultBusinessService {

    @Inject
    private SessionFactory sessionFactory;

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    protected Session getCurrentSession(){
        return sessionFactory.getCurrentSession();
    }
}

1 个答案:

答案 0 :(得分:0)

由于没有默认构造函数,因此无法创建ImportCandidateBuilder类。您有一个构造函数,但是所有这些参数都应该来自哪里

public ImportCandidateBuilder(IDomainsBusinessServices domainService,
                              ICodeBusinessService codeService,
                              ILangBusinessService vlService, 
                              ITranslationBusinessService translationService) {
}

当您要使用注入框架创建服务时,要么需要将要调用的默认构造函数,要么是带有参数的构造函数需要用@Inject进行注释,并且所有参数都必须为也绑定到DI容器。

@Inject
public ImportCandidateBuilder(IDomainsBusinessServices domainService,
                              ICodeBusinessService codeService,
                              ILangBusinessService vlService, 
                              ITranslationBusinessService translationService) {
}

register(new AbstractBinder() {
    @Override
    public void configure() {
        bind(DomainsBusinessServices.class)
                .to(IDomainsBusinessServices.class).in(Singleton.class);
        //.. bind others
    }
});