Skip to content

Commit 9d497cb

Browse files
committed
@bean definitions override scanned classes, plus check whether overriding is actually allowed
Issue: SPR-9567 Issue: SPR-9682
1 parent 9d47a2b commit 9d497cb

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassBeanDefinitionReader.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.commons.logging.Log;
2828
import org.apache.commons.logging.LogFactory;
2929

30+
import org.springframework.beans.factory.BeanDefinitionStoreException;
3031
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
3132
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
3233
import org.springframework.beans.factory.annotation.Autowire;
@@ -42,6 +43,7 @@
4243
import org.springframework.beans.factory.support.BeanDefinitionReader;
4344
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
4445
import org.springframework.beans.factory.support.BeanNameGenerator;
46+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
4547
import org.springframework.beans.factory.support.RootBeanDefinition;
4648
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
4749
import org.springframework.context.annotation.ConfigurationCondition.ConfigurationPhase;
@@ -287,6 +289,12 @@ protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String
287289
return (ccbd.getMetadata().getClassName().equals(beanMethod.getConfigurationClass().getMetadata().getClassName()));
288290
}
289291

292+
// A bean definition resulting from a component scan can be silently overridden
293+
// by an @Bean method, as of 4.2...
294+
if (existingBeanDef instanceof ScannedGenericBeanDefinition) {
295+
return false;
296+
}
297+
290298
// Has the existing bean definition bean marked as a framework-generated bean?
291299
// -> allow the current bean method to override it, since it is application-level
292300
if (existingBeanDef.getRole() > BeanDefinition.ROLE_APPLICATION) {
@@ -295,6 +303,11 @@ protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String
295303

296304
// At this point, it's a top-level override (probably XML), just having been parsed
297305
// before configuration class processing kicks in...
306+
if (this.registry instanceof DefaultListableBeanFactory &&
307+
!((DefaultListableBeanFactory) this.registry).isAllowBeanDefinitionOverriding()) {
308+
throw new BeanDefinitionStoreException(beanMethod.getConfigurationClass().getResource().getDescription(),
309+
beanName, "@Bean definition illegally overridden by existing bean definition: " + existingBeanDef);
310+
}
298311
if (logger.isInfoEnabled()) {
299312
logger.info(String.format("Skipping bean definition for %s: a definition for bean '%s' " +
300313
"already exists. This top-level bean definition is considered as an override.",

spring-context/src/test/java/example/scannable_implicitbasepackage/ComponentScanAnnotatedConfigWithImplicitBasePackage.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,8 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package example.scannable_implicitbasepackage;
1718

19+
import org.springframework.context.annotation.Bean;
1820
import org.springframework.context.annotation.ComponentScan;
1921
import org.springframework.context.annotation.Configuration;
2022

@@ -24,4 +26,10 @@
2426
@Configuration
2527
@ComponentScan
2628
public class ComponentScanAnnotatedConfigWithImplicitBasePackage {
29+
30+
@Bean // override of scanned class
31+
public ConfigurableComponent configurableComponent() {
32+
return new ConfigurableComponent(true);
33+
}
34+
2735
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2002-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package example.scannable_implicitbasepackage;
18+
19+
import org.springframework.stereotype.Component;
20+
21+
/**
22+
* @author Juergen Hoeller
23+
*/
24+
@Component
25+
public class ConfigurableComponent {
26+
27+
private final boolean flag;
28+
29+
public ConfigurableComponent() {
30+
this(false);
31+
}
32+
33+
public ConfigurableComponent(boolean flag) {
34+
this.flag = flag;
35+
}
36+
37+
public boolean isFlag() {
38+
return this.flag;
39+
}
40+
41+
}

spring-context/src/test/java/example/scannable_implicitbasepackage/ScannedComponent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
1617
package example.scannable_implicitbasepackage;
1718

1819
import org.springframework.stereotype.Component;

spring-context/src/test/java/org/springframework/context/annotation/ComponentScanAnnotationIntegrationTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
import example.scannable.MessageBean;
3131
import example.scannable.ScopedProxyTestBean;
3232
import example.scannable_implicitbasepackage.ComponentScanAnnotatedConfigWithImplicitBasePackage;
33+
import example.scannable_implicitbasepackage.ConfigurableComponent;
3334
import example.scannable_scoped.CustomScopeAnnotationBean;
3435
import example.scannable_scoped.MyScope;
3536
import org.junit.Test;
@@ -107,6 +108,7 @@ public void viaContextRegistration_FromPackageOfConfigClass() {
107108
assertThat("@ComponentScan annotated @Configuration class registered directly against " +
108109
"AnnotationConfigApplicationContext did not trigger component scanning as expected",
109110
ctx.containsBean("scannedComponent"), is(true));
111+
assertThat("@Bean method overrides scanned class", ctx.getBean(ConfigurableComponent.class).isFlag(), is(true));
110112
}
111113

112114
@Test

0 commit comments

Comments
 (0)