我正在尝试注入一个在server.env中定义的属性。
@Inject
@ConfigProperty(name = "property")
private String serverProperty;server.env的内容
property=server-env-property我已经在pom.xml中添加了它
<serverEnv>src/main/liberty/config/server.env</serverEnv>它是在liberty-maven-plugin:2.2:package-server期间读取的
CWWKM2144I: Update server configuration file server.env from /Users/abalaniuc/code/config/src/main/liberty/config/server.env.然而,当执行应用程序时,我得到了这个错误:
The property property was not found in the configuration.堆栈跟踪:
[ERROR ] CWMCG5003E: The [BackedAnnotatedField] @Inject @ConfigProperty private com.microprofile.study.config.config.ConfigTestController.serverProperty InjectionPoint dependency was not resolved. Error: java.util.NoSuchElementException: CWMCG0015E: The property property was not found in the configuration.
at com.ibm.ws.microprofile.config.impl.AbstractConfig.getValue(AbstractConfig.java:175)
at [internal classes]显然,我遗漏了一些东西,但不能确切地找出是什么。你能帮帮我吗?
正如下面的答案所建议的,我已经从属性名中删除了.,但仍然得到了相同的结果。
Liberty-插件配置:
<plugin>
<groupId>net.wasdev.wlp.maven.plugins</groupId>
<artifactId>liberty-maven-plugin</artifactId>
<version>${openliberty.maven.version}</version>
<executions>
<execution>
<id>package-server</id>
<phase>package</phase>
<goals>
<goal>create-server</goal>
<goal>install-apps</goal>
<goal>package-server</goal>
</goals>
<configuration>
<outputDirectory>target/wlp-package</outputDirectory>
</configuration>
</execution>
</executions>
<configuration>
<assemblyArtifact>
<groupId>io.openliberty</groupId>
<artifactId>openliberty-runtime</artifactId>
<version>${openliberty.version}</version>
<type>zip</type>
</assemblyArtifact>
<configFile>src/main/liberty/config/server.xml</configFile>
<serverEnv>src/main/liberty/config/server.env</serverEnv>
<bootstrapPropertiesFile>src/main/liberty/config/bootstrap.properties</bootstrapPropertiesFile>
<appArchive>${project.build.directory}/${final.name}.war</appArchive>
<packageFile>${project.build.directory}/${final.name}.jar</packageFile>
<include>runnable</include>
<serverName>${final.name}</serverName>
<installAppPackages>project</installAppPackages>
</configuration>
</plugin>要启动应用程序,我会这样做:
mvn clean package
java -jar target/config.jar发布于 2019-09-23 23:25:43
环境变量通常不允许句点-有关更多详细信息,请参阅this post。但是您可以在环境变量中使用下划线。
MicroProfile配置应该将下划线从环境变量转换为句点-它还应该重新映射大小写敏感度。here讨论了这种映射。
因此,我的建议是尝试将server.env中的server.property=server-env-property更改为server_property=server-env-property或SERVER_PROPERTY=server-env-property,看看是否有效。
更新:主要问题是为Liberty服务器运行的环境定义环境变量的方式。
在使用server命令启动/运行服务器时,它将读取服务器的server.env文件,并为服务器设置这些环境变量(以及在shell中已经定义的任何环境变量)。
在使用java -jar server.jar方法时,它不会读取server.env文件,但会读取shell中定义的环境变量。使用此方法时,用户应显式设置环境变量(如export MY_VAR=MyValue)或使用特定于外壳的命令读取server.env文件(如. server.env、env X=Y等)。
希望这能有所帮助!
发布于 2019-09-23 23:23:50
句点不是环境变量中的有效字符。如果您在server.env中指定它,它应该可以工作:
server_property=server-env-propertyMicroProfile配置规范将自动将ConfigProperty注释中的句点转换为查找所需的_。
https://stackoverflow.com/questions/58065407
复制相似问题