Last active
June 9, 2016 10:07
-
-
Save george-hawkins/e6ba76c2f42ac1e670979869f07b50ea to your computer and use it in GitHub Desktop.
Accessing Spring properties in a non-Spring application. This test demonstrates creating an application context that doesn't create beans but does setup the environment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example; | |
import java.util.Arrays; | |
import java.util.NavigableSet; | |
import java.util.Set; | |
import java.util.TreeSet; | |
import org.junit.Assert; | |
import org.junit.Test; | |
import org.springframework.boot.ApplicationArguments; | |
import org.springframework.boot.Banner; | |
import org.springframework.boot.SpringApplication; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.ConfigurableApplicationContext; | |
import org.springframework.core.env.ConfigurableEnvironment; | |
import org.springframework.core.env.EnumerablePropertySource; | |
import org.springframework.core.env.Environment; | |
import org.springframework.core.env.PropertySource; | |
public class SpringPropertiesTest { | |
private final static Class<?> APPLICATION_CLASS = SpringPropertiesTest.class; | |
private final static String[] NO_ARGS = { }; | |
@Test | |
public void testCommandLineProperty() { | |
int port = 8081; | |
String[] args = { "--port=" + port }; | |
Environment environment = getEnvironment(APPLICATION_CLASS, args); | |
// Retrieve property as both string and int. | |
Assert.assertEquals(Integer.toString(port), environment.getProperty("port")); | |
Assert.assertEquals(port, environment.getProperty("port", Integer.class).intValue()); | |
} | |
@Test | |
public void testStandardKey() { | |
Environment environment = getEnvironment(APPLICATION_CLASS, NO_ARGS); | |
Assert.assertTrue(environment.containsProperty("spring.beaninfo.ignore")); | |
} | |
@Test | |
public void testGetAllKeys() { | |
Environment environment = getEnvironment(APPLICATION_CLASS, NO_ARGS); | |
Set<String> keys = getAllKeys(environment); | |
Assert.assertTrue(keys.contains("java.version")); | |
} | |
@Test | |
public void testSystemProperty() { | |
Environment environment = getEnvironment(APPLICATION_CLASS, NO_ARGS); | |
String systemPropertyKey = "java.version"; | |
// Test that system properties are available via Spring environment. | |
Assert.assertEquals(System.getProperty(systemPropertyKey), environment.getProperty(systemPropertyKey)); | |
} | |
private static Environment getEnvironment(Object source, String[] args) { | |
SpringApplication springApplication = new BeanlessSpringApplication(source); | |
ApplicationContext context = springApplication.run(args); | |
return context.getEnvironment(); | |
} | |
private static NavigableSet<String> getAllKeys(Environment environment) { | |
NavigableSet<String> allKeys = new TreeSet<>(); | |
for (PropertySource<?> propertySource : ((ConfigurableEnvironment)environment).getPropertySources()) { | |
if (propertySource instanceof EnumerablePropertySource) { | |
String[] keys = ((EnumerablePropertySource<?>)propertySource).getPropertyNames(); | |
allKeys.addAll(Arrays.asList(keys)); | |
} | |
} | |
return allKeys; | |
} | |
// Normal usage of BeanlessSpringApplication would be: | |
// | |
// SpringApplication springApplication = new BeanlessSpringApplication(Application.class); | |
// ApplicationContext context = springApplication.run(args); | |
// | |
// Where "Application.class" is our main class and "args" are the arguments passed to its main method. | |
// | |
/** A SpringApplication subclass that does not create beans (the most expensive part of application startup). */ | |
public static class BeanlessSpringApplication extends SpringApplication { | |
public BeanlessSpringApplication(Object... sources) { | |
super(sources); | |
setBannerMode(Banner.Mode.OFF); | |
} | |
@Override | |
protected void refresh(ApplicationContext applicationContext) { | |
// Do nothing. | |
} | |
@Override | |
protected void afterRefresh(ConfigurableApplicationContext context, ApplicationArguments args) { | |
// Do nothing. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment