Search in sources :

Example 1 with SimpleCommandLinePropertySource

use of org.springframework.core.env.SimpleCommandLinePropertySource in project spring-boot by spring-projects.

the class SpringApplication method configurePropertySources.

/**
 * Add, remove or re-order any {@link PropertySource}s in this application's
 * environment.
 * @param environment this application's environment
 * @param args arguments passed to the {@code run} method
 * @see #configureEnvironment(ConfigurableEnvironment, String[])
 */
protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
    MutablePropertySources sources = environment.getPropertySources();
    if (!CollectionUtils.isEmpty(this.defaultProperties)) {
        DefaultPropertiesPropertySource.addOrMerge(this.defaultProperties, sources);
    }
    if (this.addCommandLineProperties && args.length > 0) {
        String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
        if (sources.contains(name)) {
            PropertySource<?> source = sources.get(name);
            CompositePropertySource composite = new CompositePropertySource(name);
            composite.addPropertySource(new SimpleCommandLinePropertySource("springApplicationCommandLineArgs", args));
            composite.addPropertySource(source);
            sources.replace(name, composite);
        } else {
            sources.addFirst(new SimpleCommandLinePropertySource(args));
        }
    }
}
Also used : CompositePropertySource(org.springframework.core.env.CompositePropertySource) SimpleCommandLinePropertySource(org.springframework.core.env.SimpleCommandLinePropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources)

Example 2 with SimpleCommandLinePropertySource

use of org.springframework.core.env.SimpleCommandLinePropertySource in project spring-boot by spring-projects.

the class ConfigFileApplicationListenerTests method commandLineWins.

@Test
public void commandLineWins() throws Exception {
    this.environment.getPropertySources().addFirst(new SimpleCommandLinePropertySource("--the.property=fromcommandline"));
    this.initializer.setSearchNames("testproperties");
    this.initializer.postProcessEnvironment(this.environment, this.application);
    String property = this.environment.getProperty("the.property");
    assertThat(property).isEqualTo("fromcommandline");
}
Also used : SimpleCommandLinePropertySource(org.springframework.core.env.SimpleCommandLinePropertySource) Test(org.junit.Test)

Example 3 with SimpleCommandLinePropertySource

use of org.springframework.core.env.SimpleCommandLinePropertySource in project nixmash-blog by mintster.

the class BatchLauncher method main.

public static void main(String[] args) throws Exception {
    PropertySource commandLineProperties = new SimpleCommandLinePropertySource(args);
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.getEnvironment().getPropertySources().addFirst(commandLineProperties);
    context.register(ApplicationConfiguration.class);
    context.refresh();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) SimpleCommandLinePropertySource(org.springframework.core.env.SimpleCommandLinePropertySource) SimpleCommandLinePropertySource(org.springframework.core.env.SimpleCommandLinePropertySource) PropertySource(org.springframework.core.env.PropertySource)

Example 4 with SimpleCommandLinePropertySource

use of org.springframework.core.env.SimpleCommandLinePropertySource in project mica2 by obiba.

the class Application method main.

/**
 * Main method, used to run the application.
 * <p/>
 * To run the application with hot reload enabled, add the following arguments to your JVM:
 * "-javaagent:spring_loaded/springloaded-jhipster.jar -noverify -Dspringloaded=plugins=io.github.jhipster.loaded.instrument.JHipsterLoadtimeInstrumentationPlugin"
 */
public static void main(String... args) {
    checkSystemProperty("MICA_HOME");
    SpringApplicationBuilder app = new SpringApplicationBuilder(Application.class).bannerMode(Banner.Mode.OFF);
    SimpleCommandLinePropertySource source = new SimpleCommandLinePropertySource(args);
    // Check if the selected profile has been set as argument.
    // if not the development profile will be added
    addDefaultProfile(app, source);
    app.run(args);
}
Also used : SimpleCommandLinePropertySource(org.springframework.core.env.SimpleCommandLinePropertySource) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder)

Example 5 with SimpleCommandLinePropertySource

use of org.springframework.core.env.SimpleCommandLinePropertySource in project spring-boot by spring-projects.

the class ConfigDataEnvironmentPostProcessorIntegrationTests method runWhenHasCommandLinePropertiesLoadsWithCommandLineTakingPrecedence.

@Test
void runWhenHasCommandLinePropertiesLoadsWithCommandLineTakingPrecedence() {
    StandardEnvironment environment = new StandardEnvironment();
    environment.getPropertySources().addFirst(new SimpleCommandLinePropertySource("--the.property=fromcommandline"));
    this.application.setEnvironment(environment);
    ConfigurableApplicationContext context = this.application.run("--spring.config.name=testproperties");
    String property = context.getEnvironment().getProperty("the.property");
    assertThat(property).isEqualTo("fromcommandline");
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) SimpleCommandLinePropertySource(org.springframework.core.env.SimpleCommandLinePropertySource) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Aggregations

SimpleCommandLinePropertySource (org.springframework.core.env.SimpleCommandLinePropertySource)5 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 CompositePropertySource (org.springframework.core.env.CompositePropertySource)1 MutablePropertySources (org.springframework.core.env.MutablePropertySources)1 PropertySource (org.springframework.core.env.PropertySource)1 StandardEnvironment (org.springframework.core.env.StandardEnvironment)1