gpt4 book ai didi

spring - 为什么@Value 不能从环境中注入(inject) JNDI 值?

转载 作者:行者123 更新时间:2023-11-28 21:47:53 26 4
gpt4 key购买 nike

我在使用 @Value 注释将 JNDI 值从我的 Tomcat 注入(inject) spring java 配置时遇到问题,而我通过 Environment 检索这些值没有问题。类(class)。我们正在使用 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- 我已尝试简化问题,因此如果其中一个应该 有效,请告诉我,因为我的帖子可能有误。

最佳答案

我用 the selected answer 解决了它来自问题Java Spring: How to use @Value annotation to inject an Environment property? .

所以我的代码是这样的:

@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

关于spring - 为什么@Value 不能从环境中注入(inject) JNDI 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24147198/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com