0

我想做什么?
- 为了测试,我想为我的应用程序设置serverKeyvalidity希望货物注入到 TokenUtils,所以这些值在应用程序加载时可用

我试过什么?
JBoss用作容器,相关代码是

@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实例化不是问题

我希望在 cargo 实例化 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=nullvalidity=0
- 此外,在 bean 实例化期间,我应该能够看到LOGGER.info值,但我无法在日志中找到它们

我做错了什么?

4

1 回答 1

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
于 2013-09-12T13:40:28.900 回答