球衣自定义上下文注入

Ben*_*Ben 5 java dependency-injection jax-rs jersey java-ee

我尝试实现自定义上下文注入,就像在这个答案中一样

@Provider
public class DaoContextProvider extends SingletonTypeInjectableProvider<Context,Bar> {

    public DaoContextProvider() {
        super(Bar.class, new Bar("haha"));
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的控制器类,我想向女巫注入我的上下文:

@Path("foo")
public class Foo {

    @Context
    private Bar message;

    @GET
    public String index() {
        return String.format("%s", message );
    }

}
Run Code Online (Sandbox Code Playgroud)

但是响应消息为空。

我尝试将上下文提供者添加到单例中,如推荐的那样:

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

    public ApplicationConfig() {
        getSingletons().add(new DaoContextProvider());
    }
//...
Run Code Online (Sandbox Code Playgroud)

但是然后我的工件甚至没有部署,并向我提供了此错误:

Artifact server:war exploded: java.io.IOException: com.sun.enterprise.admin.remote.RemoteFailureException: Error occurred during deployment: Exception while loading the app : java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException: java.lang.UnsupportedOperationException. Please see server.log for more details.
Run Code Online (Sandbox Code Playgroud)

我将提供server.log,如异常描述,但我不知道在哪里可以找到此日志。

Pau*_*tha 3

返回的集合getSingletons()是不可修改的。相反,我们需要重写该方法

@Override
public Set<Object> getSingletons() {
    Set<Object> singletons = new HashSet<>();
    singletons.add(new new DaoContextProvider());
    return singletons;
}
Run Code Online (Sandbox Code Playgroud)

Application请注意,与 Jersey 特定方式相比,直接子类在功能上受到限制。在 Jersey 中,首选方法是使用ResourceConfig子类(实际上是 的子类Application)。例如(您可以用来PackagesResourceConfig扫描包)。

@ApplicationPath("/webresources")
public class AppConfig extends PackagesResourceConfig {
    
    public AppConfig() {
        // scans the package and sub-packages.
        super("package.where.all.your.resource.and.providers.are");
        getProperties().put("some properites", "to set");
        getContainerRequestFilters().add(new SomeFiltersToRegister());
        getProviderSingletons().add(new SomeProvidersToAdd());
        // see the ResourceConfig API for more methods.
    }
}
Run Code Online (Sandbox Code Playgroud)

这将扫描@Path@Provider注释的类,因此我们不需要显式注册所有内容。尽管某些提供商需要明确注册。不过,您的特定提供商不需要注册。它是我在包裹扫描中捡到的。


更新

好吧,既然您说您使用的是 Glassfish 4.1,那么您首先应该了解的是 Glassfish 4 使用 Jersey 2.x。因此,您拥有的任何 Jersey 1.x 依赖项/jar 都应该摆脱它们。仅使用 Jersey 2.x 依赖项,并确保它们只是编译时依赖项,因为您不希望冲突的版本进入 Glassfish,因为 Glassfish 已经拥有它们的版本。

这意味着您当前的实施DaoContextProvider将不起作用。这SingletonTypeInjectableProvider是 Jersey 1.x 类,Jersey 2.x 运行时将忽略它。

在 Jersey 2.x 中,有几种配置可注入对象的方法。Factory一种方法是为该对象创建一个。例如。

public class DaoContextProvider implements Factory<Bar> {

    @Override
    public Bar provide() {
        return new Bar("boo hoo!");
    }

    @Override
    public void dispose(Bar bar) {}
}
Run Code Online (Sandbox Code Playgroud)

在Jersey 2.x中,APIResourceConfig发生了变化,我们可以直接扩展它。例如

@ApplicationPath("/webresources")
public class AppConfig extends ResourceConfig {

    public AppConfig() {
        
        packages("com.stackoverflow.jersey");
        
        register(new AbstractBinder(){
            @Override
            protected void configure() {
                bindFactory(DaoContextProvider.class)
                        .to(Bar.class)
                        .in(RequestScoped.class);
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

你可以看到AbstractBinder. 这就是我们向班级注册的DaoContextProvider方式Bar。所以现在Bar可以注入到你的资源类中。

您需要拉取其他所有内容的唯一 Maven 依赖项是

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.19</version>
    <scope>provided</scope>
</dependency> 
Run Code Online (Sandbox Code Playgroud)

注意提供的范围,这样它就不会被构建到战争中。如果您不使用 Maven,请获取Jersey JAX-RS 2.0 RI 包中的所有 jar 。请记住,您应该使它们仅在编译时依赖。他们不应该被卷入战争。

也可以看看: