This commit is contained in:
Phillip Webb 2014-01-10 12:55:25 -08:00
parent 26d900f3ad
commit 47da8a817a
26 changed files with 112 additions and 66 deletions

View File

@ -935,7 +935,7 @@ additional ones:
* declarative for all applications by adding a
`MTEA-INF/spring.factories` and packaging a jar file that the
applications all use as a library
Any `ApplicationContextInitializer` registered programmatically or via
`spring.factories` that is also an `ApplicationListener` will be
automatically cross registered (and vice versa for listeners that are

View File

@ -128,7 +128,8 @@ public class ManagementSecurityAutoConfiguration {
IgnoredRequestConfigurer ignoring = builder.ignoring();
// The ignores are not cumulative, so to prevent overwriting the defaults we
// add them back.
List<String> ignored = SpringBootWebSecurityConfiguration.getIgnored(this.security);
List<String> ignored = SpringBootWebSecurityConfiguration
.getIgnored(this.security);
ignored.addAll(Arrays.asList(getEndpointPaths(this.endpointHandlerMapping,
false)));
if (!this.management.getSecurity().isEnabled()) {

View File

@ -142,7 +142,8 @@ public class EndpointWebMvcAutoConfigurationTests {
@Test
public void contextPath() throws Exception {
EnvironmentTestUtils.addEnvironment(this.applicationContext, "management.contextPath:/test");
EnvironmentTestUtils.addEnvironment(this.applicationContext,
"management.contextPath:/test");
this.applicationContext.register(RootConfig.class,
PropertyPlaceholderAutoConfiguration.class,
ManagementServerPropertiesAutoConfiguration.class,

View File

@ -59,7 +59,8 @@ public class EnvironmentMvcEndpointTests {
@Before
public void setUp() {
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
EnvironmentTestUtils.addEnvironment((ConfigurableApplicationContext) this.context, "foo:bar");
EnvironmentTestUtils.addEnvironment(
(ConfigurableApplicationContext) this.context, "foo:bar");
}
@Test

View File

@ -66,7 +66,8 @@ public class JolokiaEndpointTests {
@Before
public void setUp() {
this.mvc = MockMvcBuilders.webAppContextSetup(this.context).build();
EnvironmentTestUtils.addEnvironment((ConfigurableApplicationContext) this.context, "foo:bar");
EnvironmentTestUtils.addEnvironment(
(ConfigurableApplicationContext) this.context, "foo:bar");
}
@Test

View File

@ -15,6 +15,7 @@
*/
package org.springframework.boot.autoconfigure;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
@ -37,7 +38,6 @@ import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.util.Assert;
/**
* Sort {@link EnableAutoConfiguration auto-configuration} classes into priority order by
* reading {@link Ordered} and {@link AutoConfigureAfter} annotations (without loading

View File

@ -27,7 +27,8 @@ import org.springframework.security.authentication.AuthenticationManager;
@Configuration
@ConditionalOnClass(AuthenticationManager.class)
@EnableConfigurationProperties
@Import({ SpringBootWebSecurityConfiguration.class, AuthenticationManagerConfiguration.class })
@Import({ SpringBootWebSecurityConfiguration.class,
AuthenticationManagerConfiguration.class })
public class SecurityAutoConfiguration {
@Bean(name = "org.springframework.autoconfigure.security.SecurityProperties")

View File

@ -65,9 +65,10 @@ public class RabbitAutoconfigurationTests {
public void testRabbitTemplateWithOverrides() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class, RabbitAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.rabbitmq.host:remote-server",
"spring.rabbitmq.port:9000", "spring.rabbitmq.username:alice",
"spring.rabbitmq.password:secret", "spring.rabbitmq.virtual_host:/vhost");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.rabbitmq.host:remote-server", "spring.rabbitmq.port:9000",
"spring.rabbitmq.username:alice", "spring.rabbitmq.password:secret",
"spring.rabbitmq.virtual_host:/vhost");
this.context.refresh();
CachingConnectionFactory connectionFactory = this.context
.getBean(CachingConnectionFactory.class);
@ -80,7 +81,8 @@ public class RabbitAutoconfigurationTests {
public void testRabbitTemplateEmptyVirtualHost() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class, RabbitAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.rabbitmq.virtual_host:");
EnvironmentTestUtils
.addEnvironment(this.context, "spring.rabbitmq.virtual_host:");
this.context.refresh();
CachingConnectionFactory connectionFactory = this.context
.getBean(CachingConnectionFactory.class);
@ -91,7 +93,8 @@ public class RabbitAutoconfigurationTests {
public void testRabbitTemplateVirtualHostMissingSlash() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class, RabbitAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.rabbitmq.virtual_host:foo");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.rabbitmq.virtual_host:foo");
this.context.refresh();
CachingConnectionFactory connectionFactory = this.context
.getBean(CachingConnectionFactory.class);
@ -102,7 +105,8 @@ public class RabbitAutoconfigurationTests {
public void testRabbitTemplateDefaultVirtualHost() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class, RabbitAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.rabbitmq.virtual_host:/");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.rabbitmq.virtual_host:/");
this.context.refresh();
CachingConnectionFactory connectionFactory = this.context
.getBean(CachingConnectionFactory.class);
@ -126,7 +130,8 @@ public class RabbitAutoconfigurationTests {
public void testStaticQueues() {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class, RabbitAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.rabbitmq.dynamic:false");
EnvironmentTestUtils
.addEnvironment(this.context, "spring.rabbitmq.dynamic:false");
this.context.refresh();
// There should NOT be an AmqpAdmin bean when dynamic is switch to false
this.thrown.expect(NoSuchBeanDefinitionException.class);

View File

@ -56,7 +56,8 @@ public class AopAutoConfigurationTests {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class, AopAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.aop.proxyTargetClass:true");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.aop.proxyTargetClass:true");
this.context.refresh();
TestAspect aspect = this.context.getBean(TestAspect.class);
assertFalse(aspect.isCalled());
@ -70,7 +71,8 @@ public class AopAutoConfigurationTests {
this.context = new AnnotationConfigApplicationContext();
this.context.register(TestConfiguration.class, AopAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.aop.proxyTargetClass:false");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.aop.proxyTargetClass:false");
this.context.refresh();
TestAspect aspect = this.context.getBean(TestAspect.class);
assertFalse(aspect.isCalled());

View File

@ -56,7 +56,8 @@ public class TomcatDataSourceConfigurationTests {
@Test
public void testDataSourcePropertiesOverridden() throws Exception {
this.context.register(TomcatDataSourceConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.url:jdbc:foo//bar/spam");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.datasource.url:jdbc:foo//bar/spam");
this.context.refresh();
assertEquals("jdbc:foo//bar/spam",
this.context.getBean(org.apache.tomcat.jdbc.pool.DataSource.class)

View File

@ -118,7 +118,8 @@ public class JmsTemplateAutoConfigurationTests {
this.context = new AnnotationConfigApplicationContext();
this.context
.register(TestConfiguration.class, JmsTemplateAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.jms.pubSubDomain:false");
EnvironmentTestUtils
.addEnvironment(this.context, "spring.jms.pubSubDomain:false");
this.context.refresh();
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
ActiveMQConnectionFactory connectionFactory = this.context
@ -134,7 +135,8 @@ public class JmsTemplateAutoConfigurationTests {
this.context = new AnnotationConfigApplicationContext();
this.context
.register(TestConfiguration.class, JmsTemplateAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.activemq.inMemory:false");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.activemq.inMemory:false");
this.context.refresh();
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);
ActiveMQConnectionFactory connectionFactory = this.context
@ -153,7 +155,8 @@ public class JmsTemplateAutoConfigurationTests {
this.context = new AnnotationConfigApplicationContext();
this.context
.register(TestConfiguration.class, JmsTemplateAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "spring.activemq.inMemory:false",
EnvironmentTestUtils.addEnvironment(this.context,
"spring.activemq.inMemory:false",
"spring.activemq.brokerURL:tcp://remote-host:10000");
this.context.refresh();
JmsTemplate jmsTemplate = this.context.getBean(JmsTemplate.class);

View File

@ -38,8 +38,9 @@ public class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigura
@Test
public void testCustomNamingStrategy() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "spring.jpa.hibernate.namingstrategy:"
+ "org.hibernate.cfg.EJB3NamingStrategy");
EnvironmentTestUtils.addEnvironment(this.context,
"spring.jpa.hibernate.namingstrategy:"
+ "org.hibernate.cfg.EJB3NamingStrategy");
setupTestConfiguration();
this.context.refresh();
LocalContainerEntityManagerFactoryBean bean = this.context

View File

@ -86,8 +86,8 @@ public class ServerPropertiesAutoConfigurationTests {
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(Config.class, ServerPropertiesAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "server.tomcat.basedir:target/foo",
"server.port:9000");
EnvironmentTestUtils.addEnvironment(this.context,
"server.tomcat.basedir:target/foo", "server.port:9000");
this.context.refresh();
ServerProperties server = this.context.getBean(ServerProperties.class);
assertNotNull(server);

View File

@ -75,8 +75,8 @@ public class InitCommand extends OptionParsingCommand {
ClassLoader loader = Thread.currentThread().getContextClassLoader();
boolean enhanced = false;
SourceOptions sourceOptions = new SourceOptions(options, loader, "init.groovy",
"spring.groovy");
SourceOptions sourceOptions = new SourceOptions(options, loader,
"init.groovy", "spring.groovy");
String[] sources = sourceOptions.getSourcesArray();
if (!(loader instanceof GroovyClassLoader)) {

View File

@ -43,7 +43,8 @@ public class TestRunner {
* @param sources
* @param args
*/
public TestRunner(TestRunnerConfiguration configuration, String[] sources, String[] args) {
public TestRunner(TestRunnerConfiguration configuration, String[] sources,
String[] args) {
this.sources = sources.clone();
this.compiler = new GroovyCompiler(configuration);
}

View File

@ -41,7 +41,8 @@ public class SampleSecureApplication implements CommandLineRunner {
.commaSeparatedStringToAuthorityList("ROLE_USER")));
try {
System.out.println(service.secure());
} finally {
}
finally {
SecurityContextHolder.clearContext();
}
}

View File

@ -22,11 +22,11 @@ import org.springframework.stereotype.Service;
/**
* @author Dave Syer
*
*
*/
@Service
public class SampleService {
@Secured("ROLE_USER")
public String secure() {
return "Hello Security";

View File

@ -51,7 +51,7 @@ public class SampleServletApplication extends SpringBootServletInitializer {
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleServletApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SampleServletApplication.class);

View File

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<!-- logger name="org.springframework" level="DEBUG"/-->
</configuration>

View File

@ -74,6 +74,7 @@ import org.springframework.boot.loader.util.SystemPropertyUtils;
* </ul>
*
* @author Dave Syer
* @author Janne Valkealahti
*/
public class PropertiesLauncher extends Launcher {
@ -326,27 +327,39 @@ public class PropertiesLauncher extends Launcher {
@Override
protected ClassLoader createClassLoader(List<Archive> archives) throws Exception {
ClassLoader loader = super.createClassLoader(archives);
String classLoaderType = getProperty("loader.classLoader");
if (classLoaderType != null) {
Class<?> type = Class.forName(classLoaderType, true, loader);
try {
loader = (ClassLoader) type.getConstructor(ClassLoader.class)
.newInstance(loader);
}
catch (NoSuchMethodException e) {
try {
loader = (ClassLoader) type.getConstructor(URL[].class,
ClassLoader.class).newInstance(new URL[0], loader);
}
catch (NoSuchMethodException ex) {
loader = (ClassLoader) type.newInstance();
}
}
this.logger.info("Using custom class loader: " + classLoaderType);
String customLoaderClassName = getProperty("loader.classLoader");
if (customLoaderClassName != null) {
loader = wrapWithCustomClassLoader(loader, customLoaderClassName);
this.logger.info("Using custom class loader: " + customLoaderClassName);
}
return loader;
}
@SuppressWarnings("unchecked")
private ClassLoader wrapWithCustomClassLoader(ClassLoader parent,
String loaderClassName) throws Exception {
Class<ClassLoader> loaderClass = (Class<ClassLoader>) Class.forName(
loaderClassName, true, parent);
try {
return loaderClass.getConstructor(ClassLoader.class).newInstance(parent);
}
catch (NoSuchMethodException e) {
// Ignore and try with URLs
}
try {
return loaderClass.getConstructor(URL[].class, ClassLoader.class)
.newInstance(new URL[0], parent);
}
catch (NoSuchMethodException ex) {
// Ignore and try without any arguments
}
return loaderClass.newInstance();
}
private String getProperty(String propertyKey) throws Exception {
return getProperty(propertyKey, null);
}
@ -356,18 +369,21 @@ public class PropertiesLauncher extends Launcher {
manifestKey = propertyKey.replace(".", "-");
manifestKey = toCamelCase(manifestKey);
}
String property = SystemPropertyUtils.getProperty(propertyKey);
if (property != null) {
String value = SystemPropertyUtils.resolvePlaceholders(property);
this.logger.fine("Property '" + propertyKey + "' from environment: " + value);
return value;
}
if (this.properties.containsKey(propertyKey)) {
String value = SystemPropertyUtils.resolvePlaceholders(this.properties
.getProperty(propertyKey));
this.logger.fine("Property '" + propertyKey + "' from properties: " + value);
return value;
}
try {
// Prefer home dir for MANIFEST if there is one
Manifest manifest = new ExplodedArchive(this.home).getManifest();
@ -379,7 +395,9 @@ public class PropertiesLauncher extends Launcher {
}
}
catch (IllegalStateException ex) {
// Ignore
}
// Otherwise try the parent archive
Manifest manifest = createArchive().getManifest();
if (manifest != null) {

View File

@ -50,6 +50,7 @@ public class EnvironmentDelegateApplicationListener implements
private SimpleApplicationEventMulticaster multicaster;
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof SpringApplicationEnvironmentAvailableEvent) {
List<ApplicationListener<ApplicationEvent>> delegates = getListeners(((SpringApplicationEnvironmentAvailableEvent) event)

View File

@ -42,7 +42,8 @@ public class ContextIdApplicationContextInitializerTests {
@Test
public void testNameAndPort() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.application.name:foo", "PORT:8080");
EnvironmentTestUtils.addEnvironment(context, "spring.application.name:foo",
"PORT:8080");
this.initializer.initialize(context);
assertEquals("foo:8080", context.getId());
}
@ -59,8 +60,9 @@ public class ContextIdApplicationContextInitializerTests {
@Test
public void testCloudFoundry() {
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(context, "spring.config.name:foo", "PORT:8080",
"vcap.application.name:bar", "vcap.application.instance_index:2");
EnvironmentTestUtils.addEnvironment(context, "spring.config.name:foo",
"PORT:8080", "vcap.application.name:bar",
"vcap.application.instance_index:2");
this.initializer.initialize(context);
assertEquals("bar:2", context.getId());
}

View File

@ -46,9 +46,8 @@ public class EnvironmentDelegateApplicationContextInitializerTests {
@Test
public void orderedInitialize() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
EnvironmentTestUtils.addEnvironment(context,
"context.initializer.classes:" + MockInitB.class.getName() + ","
+ MockInitA.class.getName());
EnvironmentTestUtils.addEnvironment(context, "context.initializer.classes:"
+ MockInitB.class.getName() + "," + MockInitA.class.getName());
this.initializer.initialize(context);
assertThat(context.getBeanFactory().getSingleton("a"), equalTo((Object) "a"));
assertThat(context.getBeanFactory().getSingleton("b"), equalTo((Object) "b"));
@ -79,8 +78,8 @@ public class EnvironmentDelegateApplicationContextInitializerTests {
@Test
public void notAnInitializerClass() throws Exception {
StaticApplicationContext context = new StaticApplicationContext();
EnvironmentTestUtils.addEnvironment(context,
"context.initializer.classes:" + Object.class.getName());
EnvironmentTestUtils.addEnvironment(context, "context.initializer.classes:"
+ Object.class.getName());
this.thrown.expect(IllegalArgumentException.class);
this.initializer.initialize(context);
}

View File

@ -25,6 +25,8 @@ import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.StandardEnvironment;
/**
* Tests for {@link FileEncodingApplicationListener}.
*
* @author Dave Syer
*/
public class FileEncodingApplicationListenerTests {
@ -36,7 +38,8 @@ public class FileEncodingApplicationListenerTests {
@Test(expected = IllegalStateException.class)
public void testIllegalState() {
EnvironmentTestUtils.addEnviroment(this.environment, "spring.mandatory_file_encoding:FOO");
EnvironmentTestUtils.addEnviroment(this.environment,
"spring.mandatory_file_encoding:FOO");
this.initializer.onApplicationEvent(this.event);
}
@ -48,8 +51,8 @@ public class FileEncodingApplicationListenerTests {
@Test
public void testSunnyDayMandated() {
Assume.assumeNotNull(System.getProperty("file.encoding"));
EnvironmentTestUtils.addEnviroment(this.environment, "spring.mandatory_file_encoding:"
+ System.getProperty("file.encoding"));
EnvironmentTestUtils.addEnviroment(this.environment,
"spring.mandatory_file_encoding:" + System.getProperty("file.encoding"));
this.initializer.onApplicationEvent(this.event);
}

View File

@ -108,7 +108,8 @@ public class LoggingApplicationListenerTests {
@Test
public void testOverrideConfigDoesNotExist() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "logging.config: doesnotexist.xml");
EnvironmentTestUtils.addEnvironment(this.context,
"logging.config: doesnotexist.xml");
this.initializer.initialize(this.context.getEnvironment(),
this.context.getClassLoader());
// Should not throw

View File

@ -184,7 +184,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testCollectionPropertiesBindingFromYamlArray() {
this.context.register(TestConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, "name:foo", "list[0]:1", "list[1]:2");
EnvironmentTestUtils.addEnvironment(this.context, "name:foo", "list[0]:1",
"list[1]:2");
this.context.refresh();
assertEquals(2, this.context.getBean(TestProperties.class).getList().size());
}
@ -227,7 +228,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testBindingDirectlyToFileResolvedFromEnvironment() {
EnvironmentTestUtils.addEnvironment(this.context, "binding.location:classpath:other.yml");
EnvironmentTestUtils.addEnvironment(this.context,
"binding.location:classpath:other.yml");
this.context.register(ResourceBindingProperties.class, TestConfiguration.class);
this.context.refresh();
assertEquals(1,
@ -317,7 +319,8 @@ public class EnableConfigurationPropertiesTests {
@Test
public void testMultipleExplicitTypes() throws Exception {
EnvironmentTestUtils.addEnvironment(this.context, "external.name:foo", "another.name:bar");
EnvironmentTestUtils.addEnvironment(this.context, "external.name:foo",
"another.name:bar");
this.context.register(FurtherExampleConfig.class);
this.context.refresh();
assertEquals("foo", this.context.getBean(External.class).getName());