我想做什么?
- 为了进行测试,我想为我的应用程序设置serverKey
和validity
,并希望将货物注入TokenUtils,因此在加载应用程序时可以使用这些值
我尝试了什么?
我使用JBoss
作为Container,相关代码是
@Stateless
public class TokenUtils {
public static final String AUTH_TOKEN = "X-BB-AUTH";
public static final String BB_AUTH_PRIVATE_KEY = "auth.key";
public static final String BB_AUTH_VALIDITY = "auth.validity";
private static final Pattern PATTERN = Pattern.compile(":");
private String serverPrivateKey;
private long validity;
private static final Logger LOGGER = LoggerFactory.getLogger(TokenUtils.class);
@SuppressWarnings("UnusedDeclaration")
public TokenUtils() {
}
public TokenUtils(@Nonnull @Configuration(BB_AUTH_PRIVATE_KEY) final String serverPrivateKey,
@Configuration(BB_AUTH_VALIDITY) final long validity) {
this.serverPrivateKey = serverPrivateKey;
this.validity = validity;
LOGGER.info("serverPrivateKey:" + serverPrivateKey);
LOGGER.info("validity:" + validity);
}
}
另外,我在创建META-INF/beans.xml
的同一个包的src/main/resources
中有TokenUtils
,所以bean实例化不是问题我想
我希望在货物实例化bean时注入值,因此我将pom.xml
中的值设置为
<container>
<systemProperties>
<auth.key>secure</auth.key>
<auth.validity>720000</auth.validity>
</systemProperties>
</container>
当我开始装货时,我发现以下情况发生了
INFO] [talledLocalContainer] 06:07:08,068 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-1) JNDI bindings for session bean named TokenUtils in deployment unit deployment "market.war" are as follows:
[INFO] [talledLocalContainer]
[INFO] [talledLocalContainer] java:global/market/TokenUtils!com.org.proj.service.authentication.TokenUtils
[INFO] [talledLocalContainer] java:app/market/TokenUtils!com.com.proj.service.authentication.TokenUtils
[INFO] [talledLocalContainer] java:module/TokenUtils!com.com.proj.service.authentication.TokenUtils
[INFO] [talledLocalContainer] java:global/market/TokenUtils
[INFO] [talledLocalContainer] java:app/market/TokenUtils
[INFO] [talledLocalContainer] java:module/TokenUtils
问题吗
- 但值为serverPrivateKey=null
和validity=0
- 此外,在bean实例化期间,我应该能够看到LOGGER.info
值,但我在日志中找不到它们
我做错了什么?
答案 0 :(得分:0)
我意识到了这个错误。我没有在@Inject
构造函数中使用TokenUtils
。以下更改修复了它
@Inject
public TokenUtils(@Nonnull @Configuration(BB_AUTH_PRIVATE_KEY) final String serverPrivateKey,
@Configuration(BB_AUTH_VALIDITY) final long validity) {
this.serverPrivateKey = serverPrivateKey;
this.validity = validity;
LOGGER.info("serverPrivateKey:" + serverPrivateKey);
LOGGER.info("validity:" + validity);
}
我可以在日志中看到以下值
[INFO] [talledLocalContainer] 06:39:30,042 INFO [com.org.proj.commons.cdi.config.ConfigurationProvider] (http--0.0.0.0-9090-1) getProperty(auth.key) is secure
[INFO] [talledLocalContainer] 06:39:30,043 INFO [com.org.proj.commons.cdi.config.ConfigurationProvider] (http--0.0.0.0-9090-1) getProperty(auth.validity) is 720000