Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

196
Visualizações
How do you get current active/default Environment profile programmatically in Spring?

I need to code different logic based on different current Environment profile.

How can you get the currently active and default profiles from Spring?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can auto connect the Environment

 @Autowired Environment env;

Environment offers:

  • String[] getActiveProfiles() ,
  • String[] getDefaultProfiles() , and
  • boolean acceptsProfiles(String... profiles)
over 4 years ago · Santiago Trujillo Relatório

0

Expanding on User1648825's nice and simple answer:

 @Value("${spring.profiles.active}") private String activeProfile;

This can throw an IllegalArgumentException if no profiles are set (I get null). This can be a good thing if you need to configure it; if not, use the 'default' syntax for @Value, ie:

 @Value("${spring.profiles.active:Unknown}") private String activeProfile;

...activeProfile now contains 'Unknown' if spring.profiles.active could not be resolved

over 4 years ago · Santiago Trujillo Relatório

0

Here is a more complete example.

Autowire Environment

First you will want to autowire the environment bean.

@Autowired
private Environment environment;

Check if Profiles exist in Active Profiles

Then you can use getActiveProfiles() to find out if the profile exists in the list of active profiles. Here is an example that takes the String[] from getActiveProfiles(), gets a stream from that array, then uses matchers to check for multiple profiles(Case-Insensitive) which returns a boolean if they exist.

//Check if Active profiles contains "local" or "test"
if(Arrays.stream(environment.getActiveProfiles()).anyMatch(
   env -> (env.equalsIgnoreCase("test") 
   || env.equalsIgnoreCase("local")) )) 
{
   doSomethingForLocalOrTest();
}
//Check if Active profiles contains "prod"
else if(Arrays.stream(environment.getActiveProfiles()).anyMatch(
   env -> (env.equalsIgnoreCase("prod")) )) 
{
   doSomethingForProd();
}

You can also achieve similar functionality using the annotation @Profile("local") Profiles allow for selective configuration based on a passed-in or environment parameter. Here is more information on this technique: Spring Profiles

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda