来自 bootstrap.properties 的替换和注入不起作用

Substitution and injection from bootstrap.properties doesn' t work

在我的 pom 中,我定义了一些依赖于配置文件的属性

<profiles>
        <profile>
            <!-- localhost -->
            <id>LOCAL</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <ADServiceUID>uid-c0a84980-33489f60-15ca1a4bd66--7ff7</ADServiceUID>
                <LDAPServiceUID>uid-c0a84980-4e8eaad4-15d55058ca2--7efe</LDAPServiceUID> 
                <WLAN_VPNServiceUID>uid-c0a84980-4e8eaad4-15d55058ca2--7ef6</WLAN_VPNServiceUID>
                <MailboxServiceUID>uid-c0a84980-33489f60-15ca1a4bd66--7f1b</MailboxServiceUID>
                <BiolAuthServiceUID>uid-7f001-5698ac8b-15e9a06bf32--7fbf</BiolAuthServiceUID>
                <HestAuthServiceUID>uid-c0a84980--166b2589-1646afda4f8--7ff0</HestAuthServiceUID>
                <ItetAuthServiceUID>uid-7f001-5698ac8b-15e9a06bf32--7fbd</ItetAuthServiceUID>
                <LdapsProxyServiceUID>uid-c0a84980--562f2389-15f29329a10--7126</LdapsProxyServiceUID>
                <OpenDirServiceUID>uid-c0a8f781--4baed631-15ec752b318--7d1a</OpenDirServiceUID>
                <PhysicMailServiceUID>uid-c0a84980-33489f60-15ca1a4bd66--7fed</PhysicMailServiceUID>
                <ADUserPrivGrpServiceUID>uid-ac10be8b-172512a1-15f483b1c37--7fc9</ADUserPrivGrpServiceUID>
            </properties>
        </profile>
    </profiles>

然后在我的 bootstrap.properties 中,我定义了相同的道具,这些道具应该根据我在构建应用程序时指定的配置文件进行设置

## UIDs of the IT-Services (set in pom)
it-service.ad = ${ADServiceUID}
it-service.ldap = ${LDAPServiceUID}
it-service.wlan_vpn = ${WLAN_VPNServiceUID}
it-service.mailbox = ${MailboxServiceUID}
it-service.biolauth = ${BiolAuthServiceUID}
it-service.hestauth = ${HestAuthServiceUID}
it-service.itetauth = ${ItetAuthServiceUID}
it-service.ldaps_proxy = ${LdapsProxyServiceUID}
it-service.opendirectory = ${OpenDirServiceUID}
it-service.physik_mail = ${PhysicMailServiceUID}
it-service.ad_userprivategroup = ${ADUserPrivGrpServiceUID}

I 运行 mvn clean install 和 target/classes 下的 bootstrap.properties 设置了值

## UIDs of the IT-Services (set in pom)
it-service.ad = uid-c0a84980-33489f60-15ca1a4bd66--7ff7
it-service.ldap = uid-c0a84980-4e8eaad4-15d55058ca2--7efe
it-service.wlan_vpn = uid-c0a84980-4e8eaad4-15d55058ca2--7ef6
it-service.mailbox = uid-c0a84980-33489f60-15ca1a4bd66--7f1b
it-service.biolauth = uid-7f001-5698ac8b-15e9a06bf32--7fbf
it-service.hestauth = uid-c0a84980--166b2589-1646afda4f8--7ff0
it-service.itetauth = uid-7f001-5698ac8b-15e9a06bf32--7fbd
it-service.ldaps_proxy = uid-c0a84980--562f2389-15f29329a10--7126
it-service.opendirectory = uid-c0a8f781--4baed631-15ec752b318--7d1a
it-service.physik_mail = uid-c0a84980-33489f60-15ca1a4bd66--7fed
it-service.ad_userprivategroup = uid-ac10be8b-172512a1-15f483b1c37--7fc9

而 src/main/liberty/config 下的同一个文件仍然有占位符。

此外,当我尝试在我的应用程序中读取这些属性时,它们是 null

@Inject
  @ConfigProperty( name = "it-service.ad" )
  private Provider<String> itServiceADUID;

  @Inject
  @ConfigProperty( name = "it-service.ldap" )
  private Provider<String> itServiceLdapUID;

  @Inject
  @ConfigProperty( name = "it-service.wlan_vpn" )
  private Provider<String> itServiceWlanVpnUID;

  @Inject
  @ConfigProperty( name = "it-service.mailbox" )
  private Provider<String> itServiceMailboxUID;
...

private void initITServiceUIDs()
  {
    this.itServiceUIDs = new HashMap<>();
    this.itServiceUIDs.put("AD", this.itServiceADUID.get());  <--- line 230
    this.itServiceUIDs.put("Ldap", this.itServiceLdapUID.get());
    this.itServiceUIDs.put("Mailbox", this.itServiceMailboxUID.get());
    this.itServiceUIDs.put("Wlan_Vpn", this.itServiceWlanVpnUID.get());
...
  }

错误信息:

Caused by: java.lang.NullPointerException
[INFO]  at ch.ethz.id.sws.iamws.dirxwsclient.service.ServiceClient.initITServiceUIDs(ServiceClient.java:230)

我错过了什么或做错了什么?

Francesco,首先检查您要注入配置的 class 是否已正确启用为 CDI bean(带有 bean 定义注释或存在有效的 beans.xml)。

第二,你从哪里打电话给initITServiceUIDs?如果您从构造函数中调用它,那么这些字段还不会被注入。