【发布时间】: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