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

334
Visualizações
How to set active profile programmatically for any environment?

I want to set active profile host dependent for any envrionment and cannot find an environment independent hook.

Following factory will set the active profile before application context will build.

/META-INF/spring.factories

org.springframework.context.ApplicationContextInitializer=MyApplicationContextInitializer

MyApplicationContextInitializer.java

public class MyApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
    @Override
    public void initialize(ConfigurableApplicationContext ca) {
        ConfigurableEnvironment environment = ca.getEnvironment();
        environment.addActiveProfile("myHost");
    }
}

If this example is executed in a mock environment by JUnit ...

*Test.java

...
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
...

... following will be logged.

...
... RestControllerTests   : The following profiles are active: myHost
...

but profile myHost is not active and default profile will be used in context of JUnit!

A test as Java Application and JUnit with VM arguments works ...

-Dspring.profiles.active=myHost

I use a war packaged spring-boot-starter-web app and programmatically profile shall be set and used in any environment

  • Java Application
  • JUnit
  • Servlet Container

How do I set the profile programmatically for any environmnet?

I do not want to use VM arguments or environemt variables because the profile shall be set by the current host name.

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

0

Simplest answer after a lot of googling :)

@SpringBootApplication
public class ExampleMain2 {

  public static void main(String[] args) {
      ConfigurableEnvironment environment = new StandardEnvironment();
      environment.setActiveProfiles("dev");

      SpringApplication sa = new SpringApplication(ExampleMain2.class);
      sa.setEnvironment(environment);
      sa.setAdditionalProfiles("remote","live");
      sa.run(args);
  }
}
about 4 years ago · Santiago Trujillo Relatório

0

I had the same issue and I finally solved it implementing the ActiveProfilesResolver interface.

In your case you could do something like this:

public class MyActivateProfilesResolver implements ActiveProfilesResolver {

    @Override
    public String[] resolve(Class<?> testClass) {
         // some code to find out your active profiles
         return new String[] {"myHost"};
    }
}

And then you need to link your test with your resolver like this:

@ActiveProfiles(resolver = MyActivateProfilesResolver.class)
about 4 years ago · Santiago Trujillo Relatório

0

I don't think there is such a thing as setting active profile dynamically (programmatically) at runtime once the application is running, any modification to the profile will not have any effect on the loaded beans and properties files.

However, before running the application you can configure the available profiles of the application. for example:

@SpringBootApplication
public class DemoApplicationWithSysProperty {

    public static void main(String[] args) {

        System.setProperty(AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME, 
        PROFILE_NAME);
        SpringApplication.run(DemoApplicationWithSysProperty.class, args);
    }
}
about 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