Search code examples
springenvironment-variablesspring-profiles

Spring environment.getActiveProfiles(); is always null


I have page in xhtml, I want to not render this page on production so I wrapped all elements on page with panel

<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;
  }
}

After I try to enter to my page I have null pointer exception on line:

String[] activeProfiles = environment.getActiveProfiles();

I also tried to inject Environment by @ManagedProperty, but result is the same

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

Do You know how to avoid this null pointer on my @ManagedBean? I tried solution with EnvironmentAware from Autowired Environment is null, but still there's NPE, or maybe there's better way to disable my page on production?


Solution

  • Annotate SwaggerBean with @Component rather than @ManagedBean