我在使用@Value注释将Tomcat中的JNDI值注入spring java配置时遇到了麻烦,而我通过Environment
类检索值没有任何问题.我们使用的是Java 1.7.0_17,Spring 4.0.3和Tomcat 7.0.52.
我在context.xml中定义了以下变量:
<Environment name="USER_NAME" value="initech" type="java.lang.String" />
<Environment name="USER_PASSWORD" value="1n3+3ch!" type="java.lang.String" />
在我的Java配置文件中,我有以下代码:
@Bean
public Foo foo( Environment env ){
return new Foo( env.getProperty("USER_NAME"), env.getProperty("USER_PASSWORD") );
}
当我查看服务器日志时,我看到:
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.c.e.PropertySourcesPropertyResolver -> Searching for key 'USER_NAME' in [servletConfigInitParams]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.c.e.PropertySourcesPropertyResolver -> Searching for key 'USER_NAME' in [servletContextInitParams]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.c.e.PropertySourcesPropertyResolver -> Searching for key 'USER_NAME' in [jndiProperties]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.springframework.jndi.JndiTemplate -> Looking up JNDI object with name [java:comp/env/USER_NAME]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.jndi.JndiLocatorDelegate -> Located object with JNDI name [java:comp/env/USER_NAME]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.jndi.JndiPropertySource -> JNDI lookup for name [USER_NAME] returned: [initech]
12:50:45.214 [RMI TCP Connection(3)-127.0.0.1] DEBUG o.s.c.e.PropertySourcesPropertyResolver -> Found key 'USER_NAME' in [jndiProperties] with type [String] and value 'initech'
但是,如果可能的话,我想使用@Value注释来使代码更清晰;一些东西
@Bean
public Foo foo(
@Value( "#{USER_NAME}" ) String userName,
@Value( "#{USER_PASSWORD}" ) String userPassword
){
return new Foo( userName, userPassword );
}
但是这段代码会导致错误:
nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'USER_NAME' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'USER_NAME' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'oauthTokenRequestClient' defined in class path resource [com/initech/set/gw/api/config/GatewayRepositoryConfig.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.net.URI]: : Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'USER_NAME' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Property or field 'USER_NAME' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext' - maybe not public?
org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:747)
org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:462)
我尝试了“#{systemEnvironment.USER_NAME}”,“#{systemProperties.USER_NAME}”,“#{jndiProperties.USER_NAME}”和“#{java:comp / env / USER_NAME}”:但是,它们都不起作用.
PS-我试图简化这个问题,所以如果其中一个应该有用,请告诉我,因为我可能在我的帖子中犯了错误.
解决方法:
我使用问题Java Spring: How to use @Value
annotation to inject an Environment
property?中的the selected answer解决了它.
所以我的代码看起来像:
@Bean
public Foo foo(
@Value( "#{environment.USER_NAME}" ) String userName,
@Value( "#{environment.USER_PASSWORD}" ) String userPassword
){
return new Foo( userName, userPassword );
}
我假设从the most upvoted answer的内容(即“假设你有一个PropertySourcesPlaceHolderConfigurer注册”)到该问题,这意味着我们缺少一个配置的PropertySourcesPlaceHolderConfigurer.