简体   繁体   English

Tomcat上的hk2依赖注入

[英]hk2 Dependency Injection on Tomcat

I am trying hard to get some kind of (c)di work for my little web application. 我想很难得到某种(三)二工作为我的小Web应用程序。 I gave up using Weld and now i am trying hk2 (it is already used by jersey 2). 我放弃了使用焊接 ,现在我试图HK2(它已经由球衣2)。 Here is my service that tries to inject something: 这里是我的服务,它会尝试注入的东西:

@ManagedBean
@Path("excelExport")
public class ExcelExportService {

    @Inject
    ExcelReport excelReport;

    /**
     * 
     * @param idString
     *            crucial to fetch the report
     * @return excel document (xlsx) to be downloaded
     */
    @GET
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public final Response getExcel(@QueryParam("id") final String idString) {
        Long id;
        try {
            id = Long.valueOf(idString);
        } catch (NumberFormatException e) {
            return Response.status(Status.BAD_REQUEST).build();
        }
        return Response.ok(excelReport.getCompleteExcel(id), MediaType.APPLICATION_OCTET_STREAM)
                       .header("Content-Disposition", "attachment; filename=\"" + "report-" + id + ".xlsx" + "\"")
                       .build();
    }
}

I am not sure if @ManagedBean is necessary. 我不知道如果@ManagedBean是必要的。 The Problem is : 问题是

1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=ExcelReport,parent=ExcelExportService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1464117566)

So the inject is simply not working. 所以注入是根本无法工作。 I set up DI as it is described here (see accepted answer). 我设置了DI,因为它是描述在这里 (见接受的答案)。 In my case the binder look like that: 在我的情况下,粘合剂的样子说:

@Override
protected void configure() {
    bind(ExcelReport.class).to(ExcelReport.class);
    bind(Data.class).to(DataDbImpl.class).in(Singleton.class);

}

I think i wouldn't need the bind of ExcelReport because it has a Constructor that injects Data - right? 我想我不需要ExcelReport的绑定,因为它有注入数据构造 - 对吗? But it isn't working either way. 但这两种方法都不起作用。

My "main" is this 我的“主要”是这样的

public class WebApp extends ResourceConfig{
    public WebApp(){
        register(Binder.class);
        packages("true", "com.prodyna.reportExport.service");
    }
}

and web.xml: 和web.xml:

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>reportExport</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.prodyna.WebApp</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>reportExport</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

So how to setup DI properly (my third try will be Guice). 因此,如何建立正确的DI(我的第三次尝试将吉斯)。

UPDATE I am also unsure about the dependencies needed. UPDATE我也不能确定所需的依赖关系。 currently i included this (updated based on hint): 目前我包括了这个(根据提示更新):

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>2.16</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <dependency>
        <groupId>javax.ws.rs</groupId>
        <artifactId>javax.ws.rs-api</artifactId>
        <version>2.0.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
    </dependency>
    .....

Sample project trying to solve this problem: https://github.com/miguelangelprogramacion/jersey-dependency-injection 尝试解决此问题的示例项目: https : //github.com/miguelangelprogramacion/jersey-dependency-injection

I used the recommendations of peeskillet, with a modification in the final register process, as you can see in ResourceConfigApp.java 我使用了peeskillet的建议,并在最终注册过程中进行了修改,如您在ResourceConfigApp.java中所见

It should not be register(Binder.class); 它不应该是register(Binder.class); , but instead register(new Binder()); ,而是register(new Binder());

UPDATE 更新

Ok, so on top the the above part of the answer, it should also be 好的,所以在答案的上面部分之上,它也应该是

bind(DataDbImpl.class).to(Data.class).in(Singleton.class);

The binding basically reads "bind service to contract" 绑定基本上是“将服务绑定到合同”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM