从业务层使用泽西的基本依赖注入

Lib*_*tal 7 java rest dependency-injection jersey maven

我正在开发一个RESTful服务项目.我有模块作为Web层,业务层等.我添加了基本的api层(使用泽西),我获得了get请求的基本响应.现在我必须将它连接到业务层.我在谷歌搜索,但我不知道如何实现我的项目的每个解决方案.

这是我的旅行资源类:

@Path("trip")
public class TripResource {

    @Context
    private UriInfo context;
    @Inject
    private AdminService adminService;

    public TripResource() {
    }

    @GET
    @Produces("text/plain")
    public List<TripDTO> getText() {
        return adminService.listAllTrips();
    }

}
Run Code Online (Sandbox Code Playgroud)

这我用于添加资源类:

@javax.ws.rs.ApplicationPath("api")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<Class<?>>();
        addRestResourceClasses(resources);
        return resources;
    }
    private void addRestResourceClasses(Set<Class<?>> resources) {
        resources.add(cz.infi.javatravelagency.ServiceResource.class);
        resources.add(cz.infi.javatravelagency.TripResource.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的pom.xml:

<name>JavaTravelAgency - Api module</name>
    <dependencies>
         <dependency>
            <groupId>cz.infi</groupId>
            <artifactId>javatravelagency-business</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.media</groupId>
            <artifactId>jersey-media-moxy</artifactId>
            <version>2.4.1</version>
        </dependency>
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>7.0</version>
            <type>jar</type>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- Java language version -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>6</source>
                    <target>6</target>
                </configuration>
            </plugin>
            <!-- Servlet 3.0 without web.xml -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.1.1</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
Run Code Online (Sandbox Code Playgroud)

我试着按照这个链接回答.我刚补充说:

public class MyApplicationBinder extends AbstractBinder {
    @Override
    protected void configure() {
        bind(AdminServiceImpl.class).to(AdminService.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我被卡住了.

如何将此绑定添加到我的配置类?没有使用任何其他技术,最简单的实施是什么?

Ben*_*Ben 10

这也花了我很多时间.

请尝试以下方法:

  1. 按照此处的说明将HK2 Binder添加到您的项目中:https://jersey.java.net/documentation/latest/migration.html

在这里,您必须将绑定添加到业务逻辑中.你已经有了这个(只是添加了完整性).

例如

public class MyBinder extends AbstractBinder {

    @Override
    protected void configure() {
        // request scope binding
        bind(MyInjectablePerRequest.class)
                .to(MyInjectablePerRequest.class)
                .in(RequestScope.class);
        // singleton binding
        bind(MyInjectableSingleton.class).in(Singleton.class);
        // singleton instance binding
        bind(new MyInjectableSingleton()).to(MyInjectableSingleton.class);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在项目中添加"ResourceConfig"类并在此处注册您的绑定器:http://sleeplessinslc.blogspot.de/2012/10/jax-rs-20-jersey-20-preview-example.html

在您的情况下,您可以简单地从ResourceConfig而不是ApplicationConfig扩展您的ApplicationConfig(这就是我所做的).在"getClasses()"中注册的所有类应该如下所述.

例如

/**
 * Application config
 */
public class ApplicationConfig extends ResourceConfig {

    public ApplicationConfig() {
        register(SomeResources.class, SomeProviders.class);

        // Register different Binders
        addBinders(new MyBinder());
    }
}
Run Code Online (Sandbox Code Playgroud)

至少确保您的web.xml使用配置.这取决于您的设置(glassfish,servlet v1/v2等)

由于您已经在使用ApplicationConfig类,因此您可能已经使用了正确的设置.

这里再举一个例子:

<servlet>
    <servlet-name>om.example.package.to.your.config.ApplicationConfig</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.example.package.to.your.config.ApplicationConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助;)

问候本


现在找到一个类似的帖子: Jersey 2.0的依赖注入