英文:
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
<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>
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 = "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());
...
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论