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

时间:2015-04-03 15:43:52

标签: spring jersey hk2

我有一个球衣资源,我试图注入一个简单的@Value,但它正在抛出一个org.glassfish.hk2.api.UnsatisfiedDependencyException。这适用于非资源的其他类,只需由spring框架管理。似乎HK2没有支持从春天开始桥接@Value注射。我正在使用spring-bridge,除了@Value注入之外,它一直在代码的其他部分工作正常。为了更好地解释,当我从setMaxTimeoutMillis的setter中移除@Inject并且基于构造函数的注入工作时,我的代码工作正常。使用弹簧桥时,hk2是否支持@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)

1 个答案:

答案 0 :(得分: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"));
}