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

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

Substitution and injection from bootstrap.properties doesn' t work

问题

在我的pom文件中,我定义了一些与配置文件相关的属性。

  1. <profiles>
  2. <profile>
  3. <!-- localhost -->
  4. <id>LOCAL</id>
  5. <activation>
  6. <activeByDefault>true</activeByDefault>
  7. </activation>
  8. <properties>
  9. <ADServiceUID>uid-c0a84980-33489f60-15ca1a4bd66--7ff7</ADServiceUID>
  10. <LDAPServiceUID>uid-c0a84980-4e8eaad4-15d55058ca2--7efe</LDAPServiceUID>
  11. <WLAN_VPNServiceUID>uid-c0a84980-4e8eaad4-15d55058ca2--7ef6</WLAN_VPNServiceUID>
  12. <MailboxServiceUID>uid-c0a84980-33489f60-15ca1a4bd66--7f1b</MailboxServiceUID>
  13. <BiolAuthServiceUID>uid-7f001-5698ac8b-15e9a06bf32--7fbf</BiolAuthServiceUID>
  14. <HestAuthServiceUID>uid-c0a84980--166b2589-1646afda4f8--7ff0</HestAuthServiceUID>
  15. <ItetAuthServiceUID>uid-7f001-5698ac8b-15e9a06bf32--7fbd</ItetAuthServiceUID>
  16. <LdapsProxyServiceUID>uid-c0a84980--562f2389-15f29329a10--7126</LdapsProxyServiceUID>
  17. <OpenDirServiceUID>uid-c0a8f781--4baed631-15ec752b318--7d1a</OpenDirServiceUID>
  18. <PhysicMailServiceUID>uid-c0a84980-33489f60-15ca1a4bd66--7fed</PhysicMailServiceUID>
  19. <ADUserPrivGrpServiceUID>uid-ac10be8b-172512a1-15f483b1c37--7fc9</ADUserPrivGrpServiceUID>
  20. </properties>
  21. </profile>
  22. </profiles>

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

  1. ## IT服务的UID(在pom中设置)
  2. it-service.ad = ${ADServiceUID}
  3. it-service.ldap = ${LDAPServiceUID}
  4. it-service.wlan_vpn = ${WLAN_VPNServiceUID}
  5. it-service.mailbox = ${MailboxServiceUID}
  6. it-service.biolauth = ${BiolAuthServiceUID}
  7. it-service.hestauth = ${HestAuthServiceUID}
  8. it-service.itetauth = ${ItetAuthServiceUID}
  9. it-service.ldaps_proxy = ${LdapsProxyServiceUID}
  10. it-service.opendirectory = ${OpenDirServiceUID}
  11. it-service.physik_mail = ${PhysicMailServiceUID}
  12. it-service.ad_userprivategroup = ${ADUserPrivGrpServiceUID}

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

  1. ## IT服务的UID(在pom中设置)
  2. it-service.ad = uid-c0a84980-33489f60-15ca1a4bd66--7ff7
  3. it-service.ldap = uid-c0a84980-4e8eaad4-15d55058ca2--7efe
  4. it-service.wlan_vpn = uid-c0a84980-4e8eaad4-15d55058ca2--7ef6
  5. it-service.mailbox = uid-c0a84980-33489f60-15ca1a4bd66--7f1b
  6. it-service.biolauth = uid-7f001-5698ac8b-15e9a06bf32--7fbf
  7. it-service.hestauth = uid-c0a84980--166b2589-1646afda4f8--7ff0
  8. it-service.itetauth = uid-7f001-5698ac8b-15e9a06bf32--7fbd
  9. it-service.ldaps_proxy = uid-c0a84980--562f2389-15f29329a10--7126
  10. it-service.opendirectory = uid-c0a8f781--4baed631-15ec752b318--7d1a
  11. it-service.physik_mail = uid-c0a84980-33489f60-15ca1a4bd66--7fed
  12. it-service.ad_userprivategroup = uid-ac10be8b-172512a1-15f483b1c37--7fc9

然而,位于src/main/liberty/config目录下的相同文件仍然具有占位符。

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

  1. @Inject
  2. @ConfigProperty( name = "it-service.ad" )
  3. private Provider<String> itServiceADUID;
  4. @Inject
  5. @ConfigProperty( name = "it-service.ldap" )
  6. private Provider<String> itServiceLdapUID;
  7. @Inject
  8. @ConfigProperty( name = "it-service.wlan_vpn" )
  9. private Provider<String> itServiceWlanVpnUID;
  10. @Inject
  11. @ConfigProperty( name = "it-service.mailbox" )
  12. private Provider<String> itServiceMailboxUID;
  13. ...
  14. private void initITServiceUIDs()
  15. {
  16. this.itServiceUIDs = new HashMap<>();
  17. this.itServiceUIDs.put("AD", this.itServiceADUID.get()); <--- 230
  18. this.itServiceUIDs.put("Ldap", this.itServiceLdapUID.get());
  19. this.itServiceUIDs.put("Mailbox", this.itServiceMailboxUID.get());
  20. this.itServiceUIDs.put("Wlan_Vpn", this.itServiceWlanVpnUID.get());
  21. ...
  22. }

错误消息:

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

我漏掉或做错了什么?

英文:

In my pom I define some profile-dependent properties

  1. &lt;profiles&gt;
  2. &lt;profile&gt;
  3. &lt;!-- localhost --&gt;
  4. &lt;id&gt;LOCAL&lt;/id&gt;
  5. &lt;activation&gt;
  6. &lt;activeByDefault&gt;true&lt;/activeByDefault&gt;
  7. &lt;/activation&gt;
  8. &lt;properties&gt;
  9. &lt;ADServiceUID&gt;uid-c0a84980-33489f60-15ca1a4bd66--7ff7&lt;/ADServiceUID&gt;
  10. &lt;LDAPServiceUID&gt;uid-c0a84980-4e8eaad4-15d55058ca2--7efe&lt;/LDAPServiceUID&gt;
  11. &lt;WLAN_VPNServiceUID&gt;uid-c0a84980-4e8eaad4-15d55058ca2--7ef6&lt;/WLAN_VPNServiceUID&gt;
  12. &lt;MailboxServiceUID&gt;uid-c0a84980-33489f60-15ca1a4bd66--7f1b&lt;/MailboxServiceUID&gt;
  13. &lt;BiolAuthServiceUID&gt;uid-7f001-5698ac8b-15e9a06bf32--7fbf&lt;/BiolAuthServiceUID&gt;
  14. &lt;HestAuthServiceUID&gt;uid-c0a84980--166b2589-1646afda4f8--7ff0&lt;/HestAuthServiceUID&gt;
  15. &lt;ItetAuthServiceUID&gt;uid-7f001-5698ac8b-15e9a06bf32--7fbd&lt;/ItetAuthServiceUID&gt;
  16. &lt;LdapsProxyServiceUID&gt;uid-c0a84980--562f2389-15f29329a10--7126&lt;/LdapsProxyServiceUID&gt;
  17. &lt;OpenDirServiceUID&gt;uid-c0a8f781--4baed631-15ec752b318--7d1a&lt;/OpenDirServiceUID&gt;
  18. &lt;PhysicMailServiceUID&gt;uid-c0a84980-33489f60-15ca1a4bd66--7fed&lt;/PhysicMailServiceUID&gt;
  19. &lt;ADUserPrivGrpServiceUID&gt;uid-ac10be8b-172512a1-15f483b1c37--7fc9&lt;/ADUserPrivGrpServiceUID&gt;
  20. &lt;/properties&gt;
  21. &lt;/profile&gt;
  22. &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

  1. ## UIDs of the IT-Services (set in pom)
  2. it-service.ad = ${ADServiceUID}
  3. it-service.ldap = ${LDAPServiceUID}
  4. it-service.wlan_vpn = ${WLAN_VPNServiceUID}
  5. it-service.mailbox = ${MailboxServiceUID}
  6. it-service.biolauth = ${BiolAuthServiceUID}
  7. it-service.hestauth = ${HestAuthServiceUID}
  8. it-service.itetauth = ${ItetAuthServiceUID}
  9. it-service.ldaps_proxy = ${LdapsProxyServiceUID}
  10. it-service.opendirectory = ${OpenDirServiceUID}
  11. it-service.physik_mail = ${PhysicMailServiceUID}
  12. it-service.ad_userprivategroup = ${ADUserPrivGrpServiceUID}

I run mvn clean install and the bootstrap.properties under target/classes has the values set

  1. ## UIDs of the IT-Services (set in pom)
  2. it-service.ad = uid-c0a84980-33489f60-15ca1a4bd66--7ff7
  3. it-service.ldap = uid-c0a84980-4e8eaad4-15d55058ca2--7efe
  4. it-service.wlan_vpn = uid-c0a84980-4e8eaad4-15d55058ca2--7ef6
  5. it-service.mailbox = uid-c0a84980-33489f60-15ca1a4bd66--7f1b
  6. it-service.biolauth = uid-7f001-5698ac8b-15e9a06bf32--7fbf
  7. it-service.hestauth = uid-c0a84980--166b2589-1646afda4f8--7ff0
  8. it-service.itetauth = uid-7f001-5698ac8b-15e9a06bf32--7fbd
  9. it-service.ldaps_proxy = uid-c0a84980--562f2389-15f29329a10--7126
  10. it-service.opendirectory = uid-c0a8f781--4baed631-15ec752b318--7d1a
  11. it-service.physik_mail = uid-c0a84980-33489f60-15ca1a4bd66--7fed
  12. 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

  1. @Inject
  2. @ConfigProperty( name = &quot;it-service.ad&quot; )
  3. private Provider&lt;String&gt; itServiceADUID;
  4. @Inject
  5. @ConfigProperty( name = &quot;it-service.ldap&quot; )
  6. private Provider&lt;String&gt; itServiceLdapUID;
  7. @Inject
  8. @ConfigProperty( name = &quot;it-service.wlan_vpn&quot; )
  9. private Provider&lt;String&gt; itServiceWlanVpnUID;
  10. @Inject
  11. @ConfigProperty( name = &quot;it-service.mailbox&quot; )
  12. private Provider&lt;String&gt; itServiceMailboxUID;
  13. ...
  14. private void initITServiceUIDs()
  15. {
  16. this.itServiceUIDs = new HashMap&lt;&gt;();
  17. this.itServiceUIDs.put(&quot;AD&quot;, this.itServiceADUID.get()); &lt;--- line 230
  18. this.itServiceUIDs.put(&quot;Ldap&quot;, this.itServiceLdapUID.get());
  19. this.itServiceUIDs.put(&quot;Mailbox&quot;, this.itServiceMailboxUID.get());
  20. this.itServiceUIDs.put(&quot;Wlan_Vpn&quot;, this.itServiceWlanVpnUID.get());
  21. ...
  22. }

The error message:

  1. Caused by: java.lang.NullPointerException
  2. [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:

确定