Skip to content

Commit fc8d5c0

Browse files
committed
Support final @configuration(proxyBeanMethods = false) classes
Closes gh-22869
1 parent a2a6bc3 commit fc8d5c0

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,15 +211,16 @@ public Map<String, Class<? extends BeanDefinitionReader>> getImportedResources()
211211
}
212212

213213
public void validate(ProblemReporter problemReporter) {
214-
// A configuration class may not be final (CGLIB limitation)
215-
if (getMetadata().isAnnotated(Configuration.class.getName())) {
216-
if (getMetadata().isFinal()) {
214+
// A configuration class may not be final (CGLIB limitation) unless it declares proxyBeanMethods=false
215+
String annotationName = Configuration.class.getName();
216+
if (this.metadata.isAnnotated(annotationName) &&
217+
(Boolean) this.metadata.getAnnotationAttributes(annotationName).get("proxyBeanMethods")) {
218+
if (this.metadata.isFinal()) {
217219
problemReporter.error(new FinalConfigurationProblem());
218220
}
219-
}
220-
221-
for (BeanMethod beanMethod : this.beanMethods) {
222-
beanMethod.validate(problemReporter);
221+
for (BeanMethod beanMethod : this.beanMethods) {
222+
beanMethod.validate(problemReporter);
223+
}
223224
}
224225
}
225226

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2002-2019 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+
* https://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 org.springframework.context.annotation
18+
19+
import org.junit.Assert.assertEquals
20+
import org.junit.Test
21+
import org.springframework.beans.factory.getBean
22+
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException
23+
24+
class KotlinConfigurationClassTests {
25+
26+
@Test(expected = BeanDefinitionParsingException::class)
27+
fun `Final configuration with default proxyBeanMethods value`() {
28+
AnnotationConfigApplicationContext(FinalConfigurationWithProxy::class.java)
29+
}
30+
31+
@Test
32+
fun `Final configuration with proxyBeanMethods set to false`() {
33+
val context = AnnotationConfigApplicationContext(FinalConfigurationWithoutProxy::class.java)
34+
val foo = context.getBean<Foo>()
35+
assertEquals(foo, context.getBean<Bar>().foo)
36+
}
37+
38+
39+
@Configuration
40+
class FinalConfigurationWithProxy {
41+
42+
@Bean
43+
fun foo() = Foo()
44+
45+
@Bean
46+
fun bar(foo: Foo) = Bar(foo)
47+
}
48+
49+
@Configuration(proxyBeanMethods = false)
50+
class FinalConfigurationWithoutProxy {
51+
52+
@Bean
53+
fun foo() = Foo()
54+
55+
@Bean
56+
fun bar(foo: Foo) = Bar(foo)
57+
}
58+
59+
class Foo
60+
61+
class Bar(val foo: Foo)
62+
}

0 commit comments

Comments
 (0)