【问题标题】:Spring environment.getActiveProfiles(); is always nullSpring environment.getActiveProfiles();始终为空
【发布时间】:2020-12-14 02:04:18
【问题描述】:

我在 xhtml 中有页面,我不想在生产中呈现此页面,所以我用面板将所有元素包装在页面上

<p:panel id="main" rendered="#{swaggerBean.activeProfiles}" >

SwaggerBean.class:

@ManagedBean
@ViewScoped
@Data
public class SwaggerBean{

   @Autowired
   Environment environment;


   public boolean getActiveProfiles() {
       String[] activeProfiles = environment.getActiveProfiles();
       for (String activeProfile : activeProfiles) {
           if (activeProfile.contains("prod"))
               return true;
       }
       return false;
  }
}

在我尝试进入我的页面后,出现空指针异常:

String[] activeProfiles = environment.getActiveProfiles();

我也尝试通过@ManagedProperty 注入Environment,但结果是一样的

    @ManagedProperty(value="#{environment}")
    private Environment environment;

你知道如何在我的@ManagedBean 上避免这个空指针吗? 我尝试使用来自 Autowired Environment is null 的 EnvironmentAware 解决方案,但仍然存在 NPE,或者也许有更好的方法来禁用我的生产页面?

【问题讨论】:

    标签: spring environment-variables spring-profiles


    【解决方案1】:

    使用@Component 而不是@ManagedBean 注释SwaggerBean

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-09
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 2018-06-07
      • 2012-10-14
      • 2012-07-06
      • 2020-04-19
      相关资源
      最近更新 更多