2015-04-03 1 views
1

У меня есть ресурс трикотажа, который я пытаюсь ввести просто @Value, но он бросает org.glassfish.hk2.api.UnsatisfiedDependencyException. Это работает в других классах, которые не являются ресурсами и просто управляются весной. Похоже, что у HK2 нет поддержки для слияния инъекции @Value с весны. Я использую пружинный мост, и он отлично работает для других частей кода, кроме инъекции @Value. Чтобы лучше объяснить, мой код отлично работает, когда я удаляю @Inject с установщика setMaxTimeoutMillis и работает на основе конструктора. Поддерживает ли hk2 инъекцию @Value при использовании пружинного моста?Невозможно ввести @Value с пружинным мостом и hk2

@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) 
+0

Я не думаю, что есть поддержка для @Value в весеннем мостике. Если вы хотите эту функцию, добавьте Jira здесь: http://java.net/jira/browse/HK2 – jwells131313

+0

Спасибо @ jwells131313 Я сделаю это всего лишь немного. Есть ли работа, о которой вы можете думать? –

ответ

2

На данный момент я нашел обходное решение проблемы. Я ввожу сам объект Properties и не использую место для хранения весенних свойств.

В моем приложенииContext.xml я создаю компонент для инъекционных свойств.

<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")); 
} 
Смежные вопросы