spring - 无法使用 spring-bridge 和 hk2 注入(inject)@Value

标签 spring jersey hk2

我有一个 Jersey 资源,我试图将一个简单的 @Value 注入(inject)其中,但它抛出了 org.glassfish.hk2.api.UnsatisfiedDependencyException。这适用于其他非资源类,并且仅由 spring 框架管理。 HK2 似乎不支持桥接来自 spring 的 @Value 注入(inject)。我正在使用 spring-bridge,除了 @Value 注入(inject)之外,它对于代码的其他部分工作得很好。为了更好地解释,当我从 setMaxTimeoutMillis 的 setter 中删除 @Inject 时,我的代码工作正常,并且基于构造函数的注入(inject)工作。使用spring-bridge时hk2支持@Value注入(inject)吗?

@Lazy
@Component
@Path(ICustomerResource.PATH)
public class CustomerResource extends TransactedRestResource implements ICustomerResource {


    @Inject
    protected CustomerResource(@Named("idGenerator") IIDGenerator<String> idGenerator, IMessageRouter messageRouter,
            ICommandResponseCallbackRegistry responseRegistry, IAccountProvider accountProvider) {
        super(responseRegistry, messageRouter);
        this.accountProvider = accountProvider;
        this.idGenerator = idGenerator;
        destination = MessageRouterUtils.formatDestination(accountProvider.getCurrentAccount().getEnvironment(),
                accountProvider.getCurrentAccount().getPartner().getName(), ApiAction.ACTOR);
    }



    /**
     * Injected setter for the max timeout.
     * 
     * @see #setMaxTimeoutMillis(int)
     * @param maxTimeoutMillis
     */
    @Inject
    public void setMaxTimeoutMillis(@Value("${max_rest_request_timeout}") final String maxTimeoutMillis) {
        setMaxTimeoutMillis(Integer.parseInt(maxTimeoutMillis));
    }
}




MultiException stack 1 of 1
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=String,parent=CustomerResource,qualifiers={},position=0,optional=false,self=false,unqualified=null,2284872)
    at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)
    at org.jvnet.hk2.internal.Utilities.justInject(Utilities.java:965)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:923)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:913)
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:575)
    at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:166)
    at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:327)
    at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
    at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:324)
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:289)
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.<init>(GrizzlyHttpContainer.java:334)
    at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory.createHttpServer(GrizzlyHttpServerFactory.java:144)
    at org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory$GrizzlyTestContainer.<init>(GrizzlyTestContainerFactory.java:82)
    at org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory$GrizzlyTestContainer.<init>(GrizzlyTestContainerFactory.java:66)
    at org.glassfish.jersey.test.grizzly.GrizzlyTestContainerFactory.create(GrizzlyTestContainerFactory.java:130)
    at org.glassfish.jersey.test.JerseyTest.createTestContainer(JerseyTest.java:277)
    at org.glassfish.jersey.test.JerseyTest.setUp(JerseyTest.java:609)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

最佳答案

我现在找到了解决该问题的方法。我正在注入(inject) Properties 对象本身,而不是使用 spring 属性占位符。

在我的 applicationContext.xml 中,我创建了一个可注入(inject)属性 bean。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:sec="http://www.springframework.org/schema/security"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">



    <util:properties id="applicationProperties" location="classpath:test.properties"></util:properties>

然后在 CustomerResource 中,我现在只需注入(inject)属性对象。

@Inject
public void setPropertiesPlaceHolder(@Named("applicationProperties") Properties properties) {
    setMaxTimeoutMillis(properties.getProperty("max_rest_request_timeout"));
}

关于spring - 无法使用 spring-bridge 和 hk2 注入(inject)@Value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29435352/

相关文章:

java - 在Spring Boot App中使用Redis Stream通过HTTP长轮询来阻止HTTP响应

java - 定制 Jersey 注入(inject)

tomcat - Jersey servlet 异常 tomcat

java - Jersey 2.x 的 InjectionResolver——资源被调用两次

java - 如何在 Spring Social 中获取早于特定 id 的推文

java - Spring上的-Drun.profiles和-Dspring.profiles.active有什么区别?

java - 如何将 url 作为路径参数传递?在 JAX-RS @Path 中

spring-boot - Swagger 的HK2服务修改失败

java - 具有接口(interface)类型和 InjectionResolver 的 HK2 Factory

java - 在组件构造函数上使用 `@Lazy` 等于注释每个参数吗?