Example usage for org.springframework.core.env SimpleCommandLinePropertySource SimpleCommandLinePropertySource

List of usage examples for org.springframework.core.env SimpleCommandLinePropertySource SimpleCommandLinePropertySource

Introduction

In this page you can find the example usage for org.springframework.core.env SimpleCommandLinePropertySource SimpleCommandLinePropertySource.

Prototype

public SimpleCommandLinePropertySource(String name, String[] args) 

Source Link

Document

Create a new SimpleCommandLinePropertySource having the given name and backed by the given String[] of command line arguments.

Usage

From source file:ch.sdi.SocialDataImporterRunner.java

/**
 * Initializes the environment./*from  w w w.ja v  a  2s  .  c  o  m*/
 * <p>
 * @param aArgs the command line arguments
 * @throws SdiException on any problems
 */
private void initialize(String[] aArgs) throws SdiException {
    if (myConversionService == null) {
        throw new SdiException("ConversionService not injected", SdiException.EXIT_CODE_CONFIG_ERROR);
    } // if myConversionService == null

    ConfigUtils.setConversionService(myConversionService);

    myUserPropertyOverloader.overrideByUserProperties();

    String s = Arrays.toString(aArgs);
    myLog.debug("adding command line arguments to the environment: " + s);

    MutablePropertySources propertySources = myEnv.getPropertySources();
    propertySources.addFirst(new SimpleCommandLinePropertySource(ConfigUtils.PROP_SOURCE_NAME_CMD_LINE, aArgs));

    if (myLog.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder(
                "After self-configuration, and after user property " + "override. ");
        sb.append(myLog.isTraceEnabled() ? "Loaded properties: " : "Loaded property sources: ");

        MutablePropertySources mps = myEnv.getPropertySources();
        for (PropertySource<?> propertySource : mps) {
            sb.append("\n    PropertySource: ").append(propertySource.getName());

            if (myLog.isTraceEnabled()) {
                sb.append("\n        ").append("" + propertySource.getSource());
            } // if myLog.isTraceEnabled()
        }

        if (myLog.isTraceEnabled()) {
            myLog.trace(sb.toString());
        } // if myLog.isTraceEnabled()
        else if (myLog.isDebugEnabled()) {
            myLog.debug(sb.toString());
        }
    } // if myLog.isDebugEnabled()

    myLog.trace(
            "inputcollector.thing.alternateName: " + myEnv.getProperty("inputcollector.thing.alternateName"));
}

From source file:org.springframework.boot.SpringApplication.java

/**
 * Add, remove or re-order any {@link PropertySource}s in this application's
 * environment./*from  w ww  .  jav a 2 s .  c  om*/
 * @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 (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
        sources.addLast(new MapPropertySource("defaultProperties", this.defaultProperties));
    }
    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(name + "-" + args.hashCode(), args));
            composite.addPropertySource(source);
            sources.replace(name, composite);
        } else {
            sources.addFirst(new SimpleCommandLinePropertySource(args));
        }
    }
}