Ensure that @Bean methods are only declared on @Configuration classes

Closes gh-16190
This commit is contained in:
Andy Wilkinson 2019-03-11 15:01:59 +00:00
parent 3fdf2818b3
commit 821ee0cf83
20 changed files with 73 additions and 24 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -334,6 +334,7 @@ public class EndpointAutoConfigurationTests {
}
@Configuration
static class DataSourceConfig {
@Bean

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -198,6 +198,7 @@ public class EndpointWebMvcManagementContextConfigurationTests {
}
@Configuration
static class EnvConfiguration {
@Bean
@ -208,6 +209,7 @@ public class EndpointWebMvcManagementContextConfigurationTests {
}
@Configuration
static class MetricsConfiguration {
@Bean
@ -217,6 +219,7 @@ public class EndpointWebMvcManagementContextConfigurationTests {
}
@Configuration
static class LoggersConfiguration {
@Bean
@ -236,6 +239,7 @@ public class EndpointWebMvcManagementContextConfigurationTests {
}
@Configuration
static class LogFileConfiguration {
@Bean
@ -245,6 +249,7 @@ public class EndpointWebMvcManagementContextConfigurationTests {
}
@Configuration
static class AuditEventsConfiguration {
@Bean
@ -264,6 +269,7 @@ public class EndpointWebMvcManagementContextConfigurationTests {
}
@Configuration
static class HeapdumpConfiguration {
@Bean
@ -273,6 +279,7 @@ public class EndpointWebMvcManagementContextConfigurationTests {
}
@Configuration
static class ShutdownConfiguration {
@Bean

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -627,6 +627,7 @@ public class HealthIndicatorAutoConfigurationTests {
}
@Configuration
protected static class JestClientConfiguration {
@Bean

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
@ -33,6 +34,7 @@ import org.springframework.dao.annotation.PersistenceExceptionTranslationPostPro
* @author Stephane Nicoll
* @since 1.2.0
*/
@Configuration
@ConditionalOnClass(PersistenceExceptionTranslationPostProcessor.class)
public class PersistenceExceptionTranslationAutoConfiguration {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -29,6 +29,7 @@ import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractHttpMessageConverter;
@ -41,6 +42,7 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
*
* @author Andy Wilkinson
*/
@Configuration
public class HypermediaHttpMessageConverterConfiguration {
@Bean

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -26,6 +26,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* Actual DataSource configurations imported by {@link DataSourceAutoConfiguration}.
@ -45,6 +46,7 @@ abstract class DataSourceConfiguration {
/**
* Tomcat Pool DataSource configuration.
*/
@Configuration
@ConditionalOnClass(org.apache.tomcat.jdbc.pool.DataSource.class)
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "org.apache.tomcat.jdbc.pool.DataSource", matchIfMissing = true)
@ -71,6 +73,7 @@ abstract class DataSourceConfiguration {
/**
* Hikari DataSource configuration.
*/
@Configuration
@ConditionalOnClass(HikariDataSource.class)
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "com.zaxxer.hikari.HikariDataSource", matchIfMissing = true)
@ -89,6 +92,7 @@ abstract class DataSourceConfiguration {
*
* @deprecated as of 1.5 in favor of DBCP2
*/
@Configuration
@ConditionalOnClass(org.apache.commons.dbcp.BasicDataSource.class)
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "org.apache.commons.dbcp.BasicDataSource", matchIfMissing = true)
@ -116,6 +120,7 @@ abstract class DataSourceConfiguration {
/**
* DBCP DataSource configuration.
*/
@Configuration
@ConditionalOnClass(org.apache.commons.dbcp2.BasicDataSource.class)
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type", havingValue = "org.apache.commons.dbcp2.BasicDataSource", matchIfMissing = true)
@ -134,6 +139,7 @@ abstract class DataSourceConfiguration {
/**
* Generic DataSource configuration.
*/
@Configuration
@ConditionalOnMissingBean(DataSource.class)
@ConditionalOnProperty(name = "spring.datasource.type")
static class Generic {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -34,6 +34,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.boot.jdbc.DatabaseDriver;
import org.springframework.boot.jta.XADataSourceWrapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@ -46,6 +47,7 @@ import org.springframework.util.StringUtils;
* @author Josh Long
* @since 1.2.0
*/
@Configuration
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@EnableConfigurationProperties(DataSourceProperties.class)
@ConditionalOnClass({ DataSource.class, TransactionManager.class,

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -371,6 +371,7 @@ public class BatchAutoConfigurationTests {
}
@EnableBatchProcessing
@Configuration
protected static class NamedJobConfigurationWithRegisteredJob {
@Autowired
@ -413,6 +414,7 @@ public class BatchAutoConfigurationTests {
}
@EnableBatchProcessing
@Configuration
protected static class NamedJobConfigurationWithLocalJob {
@Autowired
@ -444,6 +446,7 @@ public class BatchAutoConfigurationTests {
}
@Configuration
@EnableBatchProcessing
protected static class JobConfiguration {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -434,6 +434,7 @@ public class ConditionalOnPropertyTests {
}
@Configuration
@ConditionalOnMyFeature
protected static class MetaAnnotation {
@ -444,6 +445,7 @@ public class ConditionalOnPropertyTests {
}
@Configuration
@ConditionalOnMyFeature
@ConditionalOnProperty(prefix = "my.other.feature", name = "enabled", havingValue = "true", matchIfMissing = false)
protected static class MetaAnnotationAndDirectAnnotation {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -33,6 +33,7 @@ import org.springframework.boot.autoconfigure.jndi.TestableInitialContextFactory
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jmx.export.MBeanExporter;
import static org.assertj.core.api.Assertions.assertThat;
@ -167,7 +168,8 @@ public class JndiDataSourceAutoConfigurationTests {
TestableInitialContextFactory.bind(name, dataSource);
}
private static class MBeanExporterConfiguration {
@Configuration
static class MBeanExporterConfiguration {
@Bean
MBeanExporter mbeanExporter() {
@ -176,7 +178,8 @@ public class JndiDataSourceAutoConfigurationTests {
}
private static class AnotherMBeanExporterConfiguration {
@Configuration
static class AnotherMBeanExporterConfiguration {
@Bean
MBeanExporter anotherMbeanExporter() {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -29,6 +29,7 @@ import org.junit.Test;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.KafkaHeaders;
@ -98,6 +99,7 @@ public class KafkaAutoConfigurationIntegrationTests {
return File.separatorChar == '\\';
}
@Configuration
public static class KafkaConfig {
@Bean

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -324,6 +324,7 @@ public class ResourceServerTokenServicesConfigurationTests {
}
@Configuration
@Import({ OAuth2RestOperationsConfiguration.class })
protected static class ResourceNoClientConfiguration extends ResourceConfiguration {
@ -351,6 +352,7 @@ public class ResourceServerTokenServicesConfigurationTests {
}
@Configuration
@Import({ FacebookAutoConfiguration.class, SocialWebAutoConfiguration.class })
protected static class SocialResourceConfiguration extends ResourceConfiguration {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -373,6 +373,7 @@ public class MultipartAutoConfigurationTests {
}
@Configuration
public static class ContainerWithCustomMultipartResolver {
@Bean
@ -382,6 +383,7 @@ public class MultipartAutoConfigurationTests {
}
@Configuration
public static class ContainerWithCommonsMultipartResolver {
@Bean

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -89,6 +89,7 @@ public class WebSocketAutoConfigurationTests {
}
@Configuration
static class CommonConfiguration {
@Bean

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -133,6 +133,7 @@ public class RemoteClientConfiguration {
/**
* LiveReload configuration.
*/
@Configuration
@ConditionalOnProperty(prefix = "spring.devtools.livereload", name = "enabled", matchIfMissing = true)
static class LiveReloadConfiguration {
@ -179,6 +180,7 @@ public class RemoteClientConfiguration {
/**
* Client configuration for remote update and restarts.
*/
@Configuration
@ConditionalOnProperty(prefix = "spring.devtools.remote.restart", name = "enabled", matchIfMissing = true)
static class RemoteRestartClientConfiguration {
@ -242,6 +244,7 @@ public class RemoteClientConfiguration {
/**
* Client configuration for remote debug HTTP tunneling.
*/
@Configuration
@ConditionalOnProperty(prefix = "spring.devtools.remote.debug", name = "enabled", matchIfMissing = true)
@ConditionalOnClass(Filter.class)
@Conditional(LocalDebugPortAvailableCondition.class)

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
@ -41,6 +42,7 @@ public class SampleParentContextApplication {
.child(SampleParentContextApplication.class).run(args);
}
@Configuration
@EnableAutoConfiguration
protected static class Parent {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -70,8 +70,9 @@ public class JsonTestersAutoConfiguration {
null);
}
@Configuration
@ConditionalOnClass(ObjectMapper.class)
private static class JacksonJsonTestersConfiguration {
static class JacksonJsonTestersConfiguration {
@Bean
@Scope("prototype")
@ -84,8 +85,9 @@ public class JsonTestersAutoConfiguration {
}
@Configuration
@ConditionalOnClass(Gson.class)
private static class GsonJsonTestersConfiguration {
static class GsonJsonTestersConfiguration {
@Bean
@Scope("prototype")

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -242,6 +242,7 @@ public class BeanCurrentlyInCreationFailureAnalyzerTests {
return new BeanOne();
}
@org.springframework.context.annotation.Configuration
public static class BeanTwoConfiguration {
@SuppressWarnings("unused")
@ -255,6 +256,7 @@ public class BeanCurrentlyInCreationFailureAnalyzerTests {
}
@org.springframework.context.annotation.Configuration
public static class BeanThreeConfiguration {
@Bean

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2016 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -154,6 +154,7 @@ public class NoUniqueBeanDefinitionFailureAnalyzerTests {
}
@Configuration
static class ParentProducer {
@Bean

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -32,6 +32,7 @@ import org.junit.Test;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
@ -66,6 +67,7 @@ public class ServletContextInitializerBeansTests {
this.context = new AnnotationConfigApplicationContext(configuration);
}
@Configuration
static class ServletConfiguration {
@Bean
@ -75,6 +77,7 @@ public class ServletContextInitializerBeansTests {
}
@Configuration
static class FilterConfiguration {
@Bean