2

我有一个球衣资源,我试图将一个简单的@Value 注入其中,但它正在抛出一个 org.glassfish.hk2.api.UnsatisfiedDependencyException。这适用于其他不是资源的类,只是由 spring 框架管理。似乎 HK2 不支持从 spring 中桥接 @Value 注入。我正在使用spring-bridge,它对于@Value注入以外的其他代码部分一直运行良好。为了更好地解释,当我从 setMaxTimeoutMillis 的设置器中删除 @Inject 并且基于构造函数的注入工作时,我的代码工作正常。hk2在使用spring-bridge时是否支持@Value注入?

@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)
4

1 回答 1

2

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

在我的 applicationContext.xml 中,我创建了一个可注入的属性 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
public void setPropertiesPlaceHolder(@Named("applicationProperties") Properties properties) {
    setMaxTimeoutMillis(properties.getProperty("max_rest_request_timeout"));
}
于 2015-04-03T18:55:29.313 回答