Remove use of Ordered from auto-configuration classes

Closes gh-4056
This commit is contained in:
Andy Wilkinson 2015-10-06 14:44:44 +01:00
parent 89ed2aa020
commit 953ef7091b
3 changed files with 74 additions and 43 deletions

View File

@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.jdbc;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@ -38,15 +39,12 @@ import org.springframework.transaction.annotation.EnableTransactionManagement;
* *
* @author Dave Syer * @author Dave Syer
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Andy Wilkinson
*/ */
@Configuration @Configuration
@ConditionalOnClass({ JdbcTemplate.class, PlatformTransactionManager.class }) @ConditionalOnClass({ JdbcTemplate.class, PlatformTransactionManager.class })
public class DataSourceTransactionManagerAutoConfiguration implements Ordered { @AutoConfigureOrder(Ordered.LOWEST_PRECEDENCE)
public class DataSourceTransactionManagerAutoConfiguration {
@Override
public int getOrder() {
return Integer.MAX_VALUE;
}
@Autowired(required = false) @Autowired(required = false)
private DataSource dataSource; private DataSource dataSource;

View File

@ -58,7 +58,7 @@ import org.springframework.web.servlet.view.BeanNameViewResolver;
import org.springframework.web.util.HtmlUtils; import org.springframework.web.util.HtmlUtils;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} to render errors via a MVC error * {@link EnableAutoConfiguration Auto-configuration} to render errors via an MVC error
* controller. * controller.
* *
* @author Dave Syer * @author Dave Syer
@ -72,17 +72,10 @@ import org.springframework.web.util.HtmlUtils;
@AutoConfigureBefore(WebMvcAutoConfiguration.class) @AutoConfigureBefore(WebMvcAutoConfiguration.class)
@EnableConfigurationProperties(ErrorProperties.class) @EnableConfigurationProperties(ErrorProperties.class)
@Configuration @Configuration
public class ErrorMvcAutoConfiguration implements EmbeddedServletContainerCustomizer, public class ErrorMvcAutoConfiguration {
Ordered {
@Autowired @Autowired
private ServerProperties properties; private ServerProperties properties;
@Override
public int getOrder() {
return 0;
}
@Bean @Bean
@ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT) @ConditionalOnMissingBean(value = ErrorAttributes.class, search = SearchStrategy.CURRENT)
public DefaultErrorAttributes errorAttributes() { public DefaultErrorAttributes errorAttributes() {
@ -95,10 +88,9 @@ public class ErrorMvcAutoConfiguration implements EmbeddedServletContainerCustom
return new BasicErrorController(errorAttributes, this.properties.getError()); return new BasicErrorController(errorAttributes, this.properties.getError());
} }
@Override @Bean
public void customize(ConfigurableEmbeddedServletContainer container) { public ErrorPageCustomizer errorPageCustomizer() {
container.addErrorPages(new ErrorPage(this.properties.getServletPrefix() return new ErrorPageCustomizer(this.properties);
+ this.properties.getError().getPath()));
} }
@Configuration @Configuration
@ -224,4 +216,30 @@ public class ErrorMvcAutoConfiguration implements EmbeddedServletContainerCustom
} }
/**
* {@link EmbeddedServletContainerCustomizer} that configures the container's error
* pages.
*/
private static class ErrorPageCustomizer implements
EmbeddedServletContainerCustomizer, Ordered {
private final ServerProperties properties;
protected ErrorPageCustomizer(ServerProperties properties) {
this.properties = properties;
}
@Override
public void customize(ConfigurableEmbeddedServletContainer container) {
container.addErrorPages(new ErrorPage(this.properties.getServletPrefix()
+ this.properties.getError().getPath()));
}
@Override
public int getOrder() {
return 0;
}
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2014 the original author or authors. * Copyright 2012-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -37,12 +37,30 @@ import org.springframework.util.StringUtils;
* {@link ConfigurableEmbeddedServletContainer} from a {@link ServerProperties} bean. * {@link ConfigurableEmbeddedServletContainer} from a {@link ServerProperties} bean.
* *
* @author Dave Syer * @author Dave Syer
* @author Andy Wilkinson
*/ */
@Configuration @Configuration
@EnableConfigurationProperties @EnableConfigurationProperties
@ConditionalOnWebApplication @ConditionalOnWebApplication
public class ServerPropertiesAutoConfiguration implements ApplicationContextAware, public class ServerPropertiesAutoConfiguration {
EmbeddedServletContainerCustomizer, Ordered {
@Bean
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
public ServerProperties serverProperties() {
return new ServerProperties();
}
@Bean
public DuplicateServerPropertiesDetector duplicateServerPropertiesDetector() {
return new DuplicateServerPropertiesDetector();
}
/**
* {@link EmbeddedServletContainerCustomizer} that ensures there is exactly one
* {@link ServerProperties} bean in the application context.
*/
private static class DuplicateServerPropertiesDetector implements
EmbeddedServletContainerCustomizer, Ordered, ApplicationContextAware {
private ApplicationContext applicationContext; private ApplicationContext applicationContext;
@ -51,12 +69,6 @@ public class ServerPropertiesAutoConfiguration implements ApplicationContextAwar
return 0; return 0;
} }
@Bean
@ConditionalOnMissingBean(search = SearchStrategy.CURRENT)
public ServerProperties serverProperties() {
return new ServerProperties();
}
@Override @Override
public void setApplicationContext(ApplicationContext applicationContext) public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException { throws BeansException {
@ -72,7 +84,10 @@ public class ServerPropertiesAutoConfiguration implements ApplicationContextAwar
Assert.state( Assert.state(
serverPropertiesBeans.length == 1, serverPropertiesBeans.length == 1,
"Multiple ServerProperties beans registered " "Multiple ServerProperties beans registered "
+ StringUtils.arrayToCommaDelimitedString(serverPropertiesBeans)); + StringUtils
.arrayToCommaDelimitedString(serverPropertiesBeans));
}
} }
} }