替换和注入从bootstrap.properties中无法生效。

huangapple go评论71阅读模式
英文:

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文件中,我定义了相同的属性,这些属性将根据我在构建应用程序时指定的配置文件进行设置。

## IT服务的UID(在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}

我运行mvn clean install命令,位于target/classes目录下的bootstrap.properties文件已经设置了这些值。

## IT服务的UID(在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());  <--- 第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)

我漏掉或做错了什么?

英文:

In my pom I define some profile-dependent properties

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

Then in my bootstrap.properties I define the same props which should be set depending on the profile I specify when building my application

## 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 run mvn clean install and the bootstrap.properties under target/classes has the values set

## 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

while the the same file under src/main/liberty/config still has the placeholders.

Moreover, when I try to read these properties in my application, they are null

@Inject
  @ConfigProperty( name = &quot;it-service.ad&quot; )
  private Provider&lt;String&gt; itServiceADUID;

  @Inject
  @ConfigProperty( name = &quot;it-service.ldap&quot; )
  private Provider&lt;String&gt; itServiceLdapUID;

  @Inject
  @ConfigProperty( name = &quot;it-service.wlan_vpn&quot; )
  private Provider&lt;String&gt; itServiceWlanVpnUID;

  @Inject
  @ConfigProperty( name = &quot;it-service.mailbox&quot; )
  private Provider&lt;String&gt; itServiceMailboxUID;
...

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

The error message:

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

What am I missing or doing wrong?

答案1

得分: 2

弗朗西斯科,首先检查您要将配置注入的类是否已正确启用为CDI bean(具有有效的bean定义注释或存在有效的beans.xml)。

其次,您从何处调用initITServiceUIDs?如果您是从构造函数调用它,那么字段可能尚未被注入。

英文:

Francesco, first check that the class you are injecting config into is properly enabled as a CDI bean (with a bean definiting annotation or with a valid beans.xml present).

Second, where are you calling initITServiceUIDs from? If you call it from a constructor then the fields will not yet have been injected.

huangapple
  • 本文由 发表于 2020年10月27日 23:43:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64557948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定