Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 检查环境变量是否设置为运行时spring boot_Java_Maven_Spring Boot - Fatal编程技术网

Java 检查环境变量是否设置为运行时spring boot

Java 检查环境变量是否设置为运行时spring boot,java,maven,spring-boot,Java,Maven,Spring Boot,我的springBoot应用程序使用在我的客户机上设置的一些环境变量,我在application.properties中设置了它们,一切都按预期工作。 所有客户端使用相同的变量。 现在出现了一个问题,即某个特定客户是否拥有某个变量集。您只需要在某些客户端上配置变量。 我们的想法是每个人只有一个代码,但如果我尝试上载应用程序的属性中包含应用程序中不存在的变量: Unsatisfied dependency expressed through field 'config'; nested excep

我的springBoot应用程序使用在我的客户机上设置的一些环境变量,我在application.properties中设置了它们,一切都按预期工作。 所有客户端使用相同的变量。 现在出现了一个问题,即某个特定客户是否拥有某个变量集。您只需要在某些客户端上配置变量。 我们的想法是每个人只有一个代码,但如果我尝试上载应用程序的属性中包含应用程序中不存在的变量:

Unsatisfied dependency expressed through field 'config'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configClass': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'X' in value " ${INFO}"

是否有任何方法可以检查变量是否已设置且仅在我获取其内容时进行检查?

您可以简单地执行以下操作:

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationContextException;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.core.env.Environment;

@Configuration
public class VerifierBean implements BeanFactoryPostProcessor, PriorityOrdered {

   @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException {
      final Environment environment = configurableListableBeanFactory.getBean(Environment.class);

      if(environment.getProperty("property1.something") == null) {
         throw new ApplicationContextException("Missing property on context bootstrap" + " property1.something");
      }
   }

   @Override public int getOrder() {
      return Ordered.HIGHEST_PRECEDENCE;
   }
}
您将在console中看到类似的内容:

[  restartedMain] o.s.boot.SpringApplication : Application run failed

org.springframework.context.ApplicationContextException: Missing property on context bootstrap property1.something

使用调试模式查看是否有任何其他信息抱歉,我缺乏经验,但我不知道如何使用此类,我应该如何创建此对象的实例configurableListableBeanFactory?您可以复制我发布的所有代码,并使用application.properties文件中您自己的一些属性更改
property1.something
。我添加了
@Configuration
注释,因此当应用程序启动时,这个bean将自动注册到spring上下文中。我这样做了,但应用程序没有启动,显示错误“error starting ApplicationContext”。要显示条件报告,请在启用“调试”的情况下重新运行应用程序。2019年9月19日13:46:59.988[main]错误o.s.boot.SpringApplication.reportFailure-应用程序运行失败org.springframework.context.ApplicationContextException:com.speedsoft.bws.usive.VerifierBean.postProcessBeanFactory(VerifierBean.java:20)上的上下文引导${bws.info.siteremoto}缺少属性'我不应该稍后调用我的启动类吗?您可以发布您添加的内容而不是
property1.一些内容
?您可以将if条件更改为此并重试:
if(environment.getProperty(“bws.info.siteremoto)==null).