pull/4022/merge
Phillip Webb 9 years ago
parent 0b36ba97b3
commit 6be072ae07

@ -50,12 +50,14 @@ public class RepositoryRestProperties {
private String pageParamName;
/**
* Name of the URL query string parameter that indicates how many results to return at once.
* Name of the URL query string parameter that indicates how many results to return at
* once.
*/
private String limitParamName;
/**
* Name of the URL query string parameter that indicates what direction to sort results.
* Name of the URL query string parameter that indicates what direction to sort
* results.
*/
private String sortParamName;
@ -75,44 +77,11 @@ public class RepositoryRestProperties {
private Boolean returnBodyOnUpdate;
/**
* Enable enum value translation via the Spring Data REST default resource bundle. Will use
* the fully qualified enum name as key.
* Enable enum value translation via the Spring Data REST default resource bundle.
* Will use the fully qualified enum name as key.
*/
private Boolean enableEnumTranslation;
public void applyTo(RepositoryRestConfiguration configuration) {
if (this.basePath != null) {
configuration.setBasePath(this.basePath);
}
if (this.defaultPageSize != null) {
configuration.setDefaultPageSize(this.defaultPageSize);
}
if (this.maxPageSize != null) {
configuration.setMaxPageSize(this.maxPageSize);
}
if (this.pageParamName != null) {
configuration.setPageParamName(this.pageParamName);
}
if (this.limitParamName != null) {
configuration.setLimitParamName(this.limitParamName);
}
if (this.sortParamName != null) {
configuration.setSortParamName(this.sortParamName);
}
if (this.defaultMediaType != null) {
configuration.setDefaultMediaType(this.defaultMediaType);
}
if (this.returnBodyOnCreate != null) {
configuration.setReturnBodyOnCreate(this.returnBodyOnCreate);
}
if (this.returnBodyOnUpdate != null) {
configuration.setReturnBodyOnUpdate(this.returnBodyOnUpdate);
}
if (this.enableEnumTranslation != null) {
configuration.setEnableEnumTranslation(this.enableEnumTranslation);
}
}
public String getBasePath() {
return this.basePath;
}
@ -193,4 +162,37 @@ public class RepositoryRestProperties {
this.enableEnumTranslation = enableEnumTranslation;
}
public void applyTo(RepositoryRestConfiguration configuration) {
if (this.basePath != null) {
configuration.setBasePath(this.basePath);
}
if (this.defaultPageSize != null) {
configuration.setDefaultPageSize(this.defaultPageSize);
}
if (this.maxPageSize != null) {
configuration.setMaxPageSize(this.maxPageSize);
}
if (this.pageParamName != null) {
configuration.setPageParamName(this.pageParamName);
}
if (this.limitParamName != null) {
configuration.setLimitParamName(this.limitParamName);
}
if (this.sortParamName != null) {
configuration.setSortParamName(this.sortParamName);
}
if (this.defaultMediaType != null) {
configuration.setDefaultMediaType(this.defaultMediaType);
}
if (this.returnBodyOnCreate != null) {
configuration.setReturnBodyOnCreate(this.returnBodyOnCreate);
}
if (this.returnBodyOnUpdate != null) {
configuration.setReturnBodyOnUpdate(this.returnBodyOnUpdate);
}
if (this.enableEnumTranslation != null) {
configuration.setEnableEnumTranslation(this.enableEnumTranslation);
}
}
}

@ -90,8 +90,7 @@ public class RepositoryRestMvcAutoConfigurationTests {
@Test
public void testWithCustomSettings() throws Exception {
load(TestConfiguration.class,
"spring.data.rest.default-page-size:42",
load(TestConfiguration.class, "spring.data.rest.default-page-size:42",
"spring.data.rest.max-page-size:78",
"spring.data.rest.page-param-name:_page",
"spring.data.rest.limit-param-name:_limit",
@ -106,13 +105,18 @@ public class RepositoryRestMvcAutoConfigurationTests {
assertEquals("Custom default page size not set", 42, bean.getDefaultPageSize());
assertEquals("Custom max page size not set", 78, bean.getMaxPageSize());
assertEquals("Custom page param name not set", "_page", bean.getPageParamName());
assertEquals("Custom limit param name not set", "_limit", bean.getLimitParamName());
assertEquals("Custom limit param name not set", "_limit",
bean.getLimitParamName());
assertEquals("Custom sort param name not set", "_sort", bean.getSortParamName());
assertEquals("Custom default media type not set",
MediaType.parseMediaType("application/my-json"), bean.getDefaultMediaType());
assertEquals("Custom return body on create flag not set", false, bean.returnBodyOnCreate(null));
assertEquals("Custom return body on update flag not set", false, bean.returnBodyOnUpdate(null));
assertEquals("Custom enable enum translation flag not set", true, bean.isEnableEnumTranslation());
MediaType.parseMediaType("application/my-json"),
bean.getDefaultMediaType());
assertEquals("Custom return body on create flag not set", false,
bean.returnBodyOnCreate(null));
assertEquals("Custom return body on update flag not set", false,
bean.returnBodyOnUpdate(null));
assertEquals("Custom enable enum translation flag not set", true,
bean.isEnableEnumTranslation());
}
@Test

@ -120,8 +120,7 @@ import org.springframework.web.context.support.StandardServletEnvironment;
* your application, however, any of the following sources can also be used:
*
* <ul>
* <li>{@link Class} - A Java class to be loaded by {@link AnnotatedBeanDefinitionReader}
* </li>
* <li>{@link Class} - A Java class to be loaded by {@link AnnotatedBeanDefinitionReader}</li>
*
* <li>{@link Resource} - An XML resource to be loaded by {@link XmlBeanDefinitionReader},
* or a groovy script to be loaded by {@link GroovyBeanDefinitionReader}</li>
@ -166,6 +165,15 @@ public class SpringApplication {
private static final Banner DEFAULT_BANNER = new SpringBootBanner();
private static final Set<String> SERVLET_ENVIRONMENT_SOURCE_NAMES;
static {
Set<String> names = new HashSet<String>();
names.add(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME);
names.add(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME);
names.add(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME);
SERVLET_ENVIRONMENT_SOURCE_NAMES = Collections.unmodifiableSet(names);
}
private final Log log = LogFactory.getLog(getClass());
private final Set<Object> sources = new LinkedHashSet<Object>();
@ -236,8 +244,7 @@ public class SpringApplication {
this.sources.addAll(Arrays.asList(sources));
}
this.webEnvironment = deduceWebEnvironment();
setInitializers((Collection) getSpringFactoriesInstances(
ApplicationContextInitializer.class));
setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
this.mainApplicationClass = deduceMainApplicationClass();
}
@ -283,8 +290,8 @@ public class SpringApplication {
context = doRun(listeners, args);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
new StartupInfoLogger(this.mainApplicationClass).logStarted(
getApplicationLog(), stopWatch);
}
return context;
}
@ -310,11 +317,14 @@ public class SpringApplication {
ConfigurableEnvironment environment = getOrCreateEnvironment();
configureEnvironment(environment, args);
listeners.environmentPrepared(environment);
if (environment instanceof StandardServletEnvironment && !this.webEnvironment) {
environment = convertToStandardEnvironment(environment);
}
if (this.showBanner) {
printBanner(environment);
}
environment = resetEnvironment(environment);
// Create, load, refresh and run the ApplicationContext
context = createApplicationContext();
if (this.registerShutdownHook) {
@ -351,43 +361,11 @@ public class SpringApplication {
return context;
}
private ConfigurableEnvironment resetEnvironment(
ConfigurableEnvironment environment) {
if (this.environment != null && environment != this.environment) {
environment = this.environment;
}
if (environment instanceof StandardServletEnvironment && !this.webEnvironment) {
Set<String> servletSources = new HashSet<>(Arrays.asList(
StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,
StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME,
StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME));
ConfigurableEnvironment copy = environment;
environment = new StandardEnvironment();
environment.setActiveProfiles(copy.getActiveProfiles());
String current = null;
MutablePropertySources sources = environment.getPropertySources();
for (PropertySource<?> source : copy.getPropertySources()) {
if (!servletSources.contains(source.getName())) {
String name = source.getName();
if (sources.contains(name)) {
sources.replace(name, source);
}
else if (current == null) {
sources.addFirst(source);
}
else {
sources.addAfter(current, source);
}
current = name;
}
}
}
return environment;
}
private void configureHeadlessProperty() {
System.setProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, System.getProperty(
SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString(this.headless)));
System.setProperty(
SYSTEM_PROPERTY_JAVA_AWT_HEADLESS,
System.getProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS,
Boolean.toString(this.headless)));
}
private SpringApplicationRunListeners getRunListeners(String[] args) {
@ -420,8 +398,8 @@ public class SpringApplication {
instances.add(instance);
}
catch (Throwable ex) {
throw new IllegalArgumentException(
"Cannot instantiate " + type + " : " + name, ex);
throw new IllegalArgumentException("Cannot instantiate " + type + " : "
+ name, ex);
}
}
@ -451,12 +429,34 @@ public class SpringApplication {
* @see #configureProfiles(ConfigurableEnvironment, String[])
* @see #configurePropertySources(ConfigurableEnvironment, String[])
*/
protected void configureEnvironment(ConfigurableEnvironment environment,
String[] args) {
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
configurePropertySources(environment, args);
configureProfiles(environment, args);
}
private ConfigurableEnvironment convertToStandardEnvironment(
ConfigurableEnvironment environment) {
StandardEnvironment result = new StandardEnvironment();
removeAllPropertySources(result.getPropertySources());
result.setActiveProfiles(environment.getActiveProfiles());
for (PropertySource<?> propertySource : environment.getPropertySources()) {
if (!SERVLET_ENVIRONMENT_SOURCE_NAMES.contains(propertySource.getName())) {
result.getPropertySources().addLast(propertySource);
}
}
return result;
}
private void removeAllPropertySources(MutablePropertySources propertySources) {
Set<String> names = new HashSet<String>();
for (PropertySource<?> propertySource : propertySources) {
names.add(propertySource.getName());
}
for (String name : names) {
propertySources.remove(name);
}
}
/**
* Add, remove or re-order any {@link PropertySource}s in this application's
* environment.
@ -468,16 +468,16 @@ public class SpringApplication {
String[] args) {
MutablePropertySources sources = environment.getPropertySources();
if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
sources.addLast(
new MapPropertySource("defaultProperties", this.defaultProperties));
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(new SimpleCommandLinePropertySource(name
+ "-" + args.hashCode(), args));
composite.addPropertySource(source);
sources.replace(name, composite);
}
@ -544,14 +544,14 @@ public class SpringApplication {
Class<?> contextClass = this.applicationContextClass;
if (contextClass == null) {
try {
contextClass = Class.forName(this.webEnvironment
? DEFAULT_WEB_CONTEXT_CLASS : DEFAULT_CONTEXT_CLASS);
contextClass = Class
.forName(this.webEnvironment ? DEFAULT_WEB_CONTEXT_CLASS
: DEFAULT_CONTEXT_CLASS);
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Unable create a default ApplicationContext, "
+ "please specify an ApplicationContextClass",
ex);
+ "please specify an ApplicationContextClass", ex);
}
}
return (ConfigurableApplicationContext) BeanUtils.instantiate(contextClass);
@ -580,8 +580,8 @@ public class SpringApplication {
.setResourceLoader(this.resourceLoader);
}
if (context instanceof DefaultResourceLoader) {
((DefaultResourceLoader) context)
.setClassLoader(this.resourceLoader.getClassLoader());
((DefaultResourceLoader) context).setClassLoader(this.resourceLoader
.getClassLoader());
}
}
}
@ -632,8 +632,8 @@ public class SpringApplication {
*/
protected void load(ApplicationContext context, Object[] sources) {
if (this.log.isDebugEnabled()) {
this.log.debug(
"Loading source " + StringUtils.arrayToCommaDelimitedString(sources));
this.log.debug("Loading source "
+ StringUtils.arrayToCommaDelimitedString(sources));
}
BeanDefinitionLoader loader = createBeanDefinitionLoader(
getBeanDefinitionRegistry(context), sources);
@ -1054,8 +1054,8 @@ public class SpringApplication {
try {
List<ExitCodeGenerator> generators = new ArrayList<ExitCodeGenerator>();
generators.addAll(Arrays.asList(exitCodeGenerators));
generators
.addAll(context.getBeansOfType(ExitCodeGenerator.class).values());
generators.addAll(context.getBeansOfType(ExitCodeGenerator.class)
.values());
exitCode = getExitCode(generators);
}
finally {

@ -216,13 +216,14 @@ public class SpringApplicationTests {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
final AtomicReference<ApplicationContext> reference = new AtomicReference<ApplicationContext>();
application.setInitializers(Arrays.asList(
new ApplicationContextInitializer<ConfigurableApplicationContext>() {
@Override
public void initialize(ConfigurableApplicationContext context) {
reference.set(context);
}
}));
application
.setInitializers(Arrays
.asList(new ApplicationContextInitializer<ConfigurableApplicationContext>() {
@Override
public void initialize(ConfigurableApplicationContext context) {
reference.set(context);
}
}));
this.context = application.run("--foo=bar");
assertThat(this.context, sameInstance(reference.get()));
// Custom initializers do not switch off the defaults
@ -234,8 +235,8 @@ public class SpringApplicationTests {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
final AtomicReference<SpringApplication> reference = new AtomicReference<SpringApplication>();
class ApplicationReadyEventListener
implements ApplicationListener<ApplicationReadyEvent> {
class ApplicationReadyEventListener implements
ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(ApplicationReadyEvent event) {
reference.set(event.getSpringApplication());
@ -269,8 +270,8 @@ public class SpringApplicationTests {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
final List<ApplicationEvent> events = new ArrayList<ApplicationEvent>();
class ApplicationRunningEventListener
implements ApplicationListener<ApplicationEvent> {
class ApplicationRunningEventListener implements
ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent event) {
events.add((event));
@ -306,8 +307,7 @@ public class SpringApplicationTests {
@Test
public void customEnvironment() throws Exception {
TestSpringApplication application = new TestSpringApplication(
ExampleConfig.class);
TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
ConfigurableEnvironment environment = new StandardEnvironment();
application.setEnvironment(environment);
@ -317,8 +317,7 @@ public class SpringApplicationTests {
@Test
public void customResourceLoader() throws Exception {
TestSpringApplication application = new TestSpringApplication(
ExampleConfig.class);
TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
ResourceLoader resourceLoader = new DefaultResourceLoader();
application.setResourceLoader(resourceLoader);
@ -365,8 +364,9 @@ public class SpringApplicationTests {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
ConfigurableEnvironment environment = new StandardEnvironment();
environment.getPropertySources().addFirst(new MapPropertySource("commandLineArgs",
Collections.<String, Object> singletonMap("foo", "original")));
environment.getPropertySources().addFirst(
new MapPropertySource("commandLineArgs", Collections
.<String, Object>singletonMap("foo", "original")));
application.setEnvironment(environment);
this.context = application.run("--foo=bar", "--bar=foo");
assertTrue(hasPropertySource(environment, CompositePropertySource.class,
@ -440,8 +440,8 @@ public class SpringApplicationTests {
ConfigurableEnvironment environment = new StandardEnvironment();
application.setEnvironment(environment);
this.context = application.run("--foo=bar");
assertFalse(
hasPropertySource(environment, PropertySource.class, "commandLineArgs"));
assertFalse(hasPropertySource(environment, PropertySource.class,
"commandLineArgs"));
}
@Test
@ -467,8 +467,7 @@ public class SpringApplicationTests {
@Test
public void wildcardSources() {
Object[] sources = {
"classpath:org/springframework/boot/sample-${sample.app.test.prop}.xml" };
Object[] sources = { "classpath:org/springframework/boot/sample-${sample.app.test.prop}.xml" };
TestSpringApplication application = new TestSpringApplication(sources);
application.setWebEnvironment(false);
this.context = application.run();
@ -482,8 +481,8 @@ public class SpringApplicationTests {
@Test
public void runComponents() throws Exception {
this.context = SpringApplication.run(
new Object[] { ExampleWebConfig.class, Object.class }, new String[0]);
this.context = SpringApplication.run(new Object[] { ExampleWebConfig.class,
Object.class }, new String[0]);
assertNotNull(this.context);
}
@ -524,8 +523,7 @@ public class SpringApplicationTests {
@Test
public void commandLineArgsApplyToSpringApplication() throws Exception {
TestSpringApplication application = new TestSpringApplication(
ExampleConfig.class);
TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
this.context = application.run("--spring.main.show_banner=false");
assertThat(application.getShowBanner(), is(false));
@ -586,8 +584,7 @@ public class SpringApplicationTests {
@Test
public void headless() throws Exception {
TestSpringApplication application = new TestSpringApplication(
ExampleConfig.class);
TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
this.context = application.run();
assertThat(System.getProperty("java.awt.headless"), equalTo("true"));
@ -595,8 +592,7 @@ public class SpringApplicationTests {
@Test
public void headlessFalse() throws Exception {
TestSpringApplication application = new TestSpringApplication(
ExampleConfig.class);
TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
application.setHeadless(false);
this.context = application.run();
@ -606,8 +602,7 @@ public class SpringApplicationTests {
@Test
public void headlessSystemPropertyTakesPrecedence() throws Exception {
System.setProperty("java.awt.headless", "false");
TestSpringApplication application = new TestSpringApplication(
ExampleConfig.class);
TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
this.context = application.run();
assertThat(System.getProperty("java.awt.headless"), equalTo("false"));
@ -615,8 +610,7 @@ public class SpringApplicationTests {
@Test
public void getApplicationArgumentsBean() throws Exception {
TestSpringApplication application = new TestSpringApplication(
ExampleConfig.class);
TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
application.setWebEnvironment(false);
this.context = application.run("--debug", "spring", "boot");
ApplicationArguments args = this.context.getBean(ApplicationArguments.class);
@ -626,16 +620,14 @@ public class SpringApplicationTests {
@Test
public void webEnvironmentSwitchedOffInListener() throws Exception {
TestSpringApplication application = new TestSpringApplication(
ExampleConfig.class);
application.addListeners(
new ApplicationListener<ApplicationEnvironmentPreparedEvent>() {
TestSpringApplication application = new TestSpringApplication(ExampleConfig.class);
application
.addListeners(new ApplicationListener<ApplicationEnvironmentPreparedEvent>() {
@Override
public void onApplicationEvent(
ApplicationEnvironmentPreparedEvent event) {
assertTrue(event
.getEnvironment() instanceof StandardServletEnvironment);
assertTrue(event.getEnvironment() instanceof StandardServletEnvironment);
EnvironmentTestUtils.addEnvironment(event.getEnvironment(),
"foo=bar");
event.getSpringApplication().setWebEnvironment(false);
@ -645,8 +637,8 @@ public class SpringApplicationTests {
this.context = application.run();
assertFalse(this.context.getEnvironment() instanceof StandardServletEnvironment);
assertEquals("bar", this.context.getEnvironment().getProperty("foo"));
assertEquals("test",
this.context.getEnvironment().getPropertySources().iterator().next());
assertEquals("test", this.context.getEnvironment().getPropertySources()
.iterator().next().getName());
}
private boolean hasPropertySource(ConfigurableEnvironment environment,
@ -670,8 +662,7 @@ public class SpringApplicationTests {
public static class SpyApplicationContext extends AnnotationConfigApplicationContext {
ConfigurableApplicationContext applicationContext = spy(
new AnnotationConfigApplicationContext());
ConfigurableApplicationContext applicationContext = spy(new AnnotationConfigApplicationContext());
@Override
public void registerShutdownHook() {
@ -818,8 +809,8 @@ public class SpringApplicationTests {
}
private static class TestCommandLineRunner extends AbstractTestRunner
implements CommandLineRunner {
private static class TestCommandLineRunner extends AbstractTestRunner implements
CommandLineRunner {
TestCommandLineRunner(int order, String... expectedBefore) {
super(order, expectedBefore);
@ -832,8 +823,8 @@ public class SpringApplicationTests {
}
private static class TestApplicationRunner extends AbstractTestRunner
implements ApplicationRunner {
private static class TestApplicationRunner extends AbstractTestRunner implements
ApplicationRunner {
TestApplicationRunner(int order, String... expectedBefore) {
super(order, expectedBefore);

Loading…
Cancel
Save