Java Spring@Value变量未按预期记录
我使用Springs@Value注释声明了一个字符串,如下所示:Java Spring@Value变量未按预期记录,java,spring,Java,Spring,我使用Springs@Value注释声明了一个字符串,如下所示: @Value("${blah.blah.authKey}") private String authorizationKey; 我希望,如果在.properties文件中正确定义了这个属性,那么我应该能够使用logger.trace()这个变量来查看它的注入值。但出于某种原因: logger.trace("Bearer Authorization header: " + authorizationKey); 输出:${blah
@Value("${blah.blah.authKey}")
private String authorizationKey;
我希望,如果在.properties文件中正确定义了这个属性,那么我应该能够使用logger.trace()这个变量来查看它的注入值。但出于某种原因:
logger.trace("Bearer Authorization header: " + authorizationKey);
输出:${blah.blah.authKey}
鉴于
logger.trace("Bearer Authorization header: " + this.authorizationKey);
输出:来自属性文件的实际键值
为什么这些日志记录语句之间存在差异
下面是包含Java类的代码
package com.tsgrp.opencontent.allstate.leela.sharedecon.service;
... imports. . .
public class SeLeelaService implements ILeelaService {
public static RestTemplate rt = new RestTemplate();
@Value("${blah.blah.authKey}")
private String authorizationKey;
public LeelaWrapper getLeelaInfo(String claimNumber, String ticket) {
String paddedClaimNumber = AllstateClaimsUtil.zeroPadClaim(claimNumber, 12);
String queryJson = "query-here"
String claimNumJson = "\"variables\":{\"claimNumber\":\""+paddedClaimNumber+"\"}";
String finalBodyStr = queryJson.concat(claimNumJson);
long startTime = System.currentTimeMillis();
SeLeelaClaimInfo claimInfo;
SharedEconomyClaimWrapper seClaimWrapper;
LeelaWrapper claimWrapper = null;
try {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost claimReq = new HttpPost(graphQlUrl);
// add headers
claimReq.addHeader(HttpHeaders.CONTENT_TYPE, "application/json");
claimReq.addHeader("Accept","application/json");
logger.trace("Bearer Authorization header: " + authorizationKey);
claimReq.addHeader(HttpHeaders.AUTHORIZATION, authorizationKey);
. . .
. . .
. . .
只有一个。实例。这个实例变量的值-为什么在两个logger语句中都不使用这个值?(如果有必要,这是log4j2)Spring容器尚未扫描@Value注释 如果您使用的是Spring注释,或者(JavaEnterpriseEdition的新名称)注释,那么您需要将带有这些注释的bean添加到Spring上下文中。它生成容器管理的bean。这允许容器拦截创建、扫描所有这些注释并应用它们 您可以通过许多不同的方式将bean添加到上下文中。但是通常,如果您使用注释,那么整个配置将通过注释完成。 所以只需添加其中一个注释:
@Component/@Service/@Repository/@Controller
对于您的情况,您需要
@Service
注释Spring容器尚未扫描@Value注释
如果您使用的是Spring注释,或者(JavaEnterpriseEdition的新名称)注释,那么您需要将带有这些注释的bean添加到Spring上下文中。它生成容器管理的bean。这允许容器拦截创建、扫描所有这些注释并应用它们
您可以通过许多不同的方式将bean添加到上下文中。但是通常,如果您使用注释,那么整个配置将通过注释完成。
所以只需添加其中一个注释:@Component/@Service/@Repository/@Controller
对于您的情况,您需要
@Service
annotation您是否用@Configuration
注释了您的SeLeelaService
类?您的类SeLeelaService不是Spring beanSide注意:不要使用字段注入(@Value
字段是字段注入)。此源代码中的.authorizationKey和authorizationKey
都将编译为完全相同的字节码。问题出在别处。对于那些没有用@Configuration
注释该类的帖子,@Service
更适合给出该类的名称。此外,如果对接口进行了注释,则注释是多余的。您是否使用@Configuration
注释对SeLeelaService
类进行了注释?您的类SeLeelaService不是Spring beanSide注意:不要使用字段注入(@Value
字段是字段注入)。此源代码中的.authorizationKey和authorizationKey
都将编译为完全相同的字节码。问题出在别处。对于那些没有用@Configuration
注释该类的帖子,@Service
更适合给出该类的名称。此外,如果对接口进行了注释,则注释是多余的。