Spring environment.getActiveProfiles();总是空的
Spring environment.getActiveProfiles(); is always null
我在 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 注入环境,但结果是一样的
@ManagedProperty(value="#{environment}")
private Environment environment;
你知道如何在我的@ManagedBean 上避免这个空指针吗?
我尝试使用来自 Autowired Environment is null 的 EnvironmentAware 解决方案,但仍然存在 NPE,或者也许有更好的方法来禁用我的生产页面?
使用@Component 而不是@ManagedBean 注释SwaggerBean
我在 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 注入环境,但结果是一样的
@ManagedProperty(value="#{environment}")
private Environment environment;
你知道如何在我的@ManagedBean 上避免这个空指针吗? 我尝试使用来自 Autowired Environment is null 的 EnvironmentAware 解决方案,但仍然存在 NPE,或者也许有更好的方法来禁用我的生产页面?
使用@Component 而不是@ManagedBean 注释SwaggerBean