Skip to content

Commit 78c6d70

Browse files
committed
Refute claims made in SPR-9051
It was claimed that when a {@code @ContextConfiguration} test class references a config class missing an {@code @configuration} annotation, @bean dependencies are wired successfully but the bean lifecycle is not applied (no init methods are invoked, for example). AnnotatedConfigClassesWithoutAtConfigurationTests refutes this claim by demonstrating that @bean methods in non-@configuration classes are properly handled as "annotated factory bean methods" and that lifecycle callbacks in fact apply to such factory beans. Issue: SPR-9051
1 parent d52fc3b commit 78c6d70

File tree

3 files changed

+158
-5
lines changed

3 files changed

+158
-5
lines changed

spring-test/src/test/java/org/springframework/test/context/junit4/profile/importresource/DefaultProfileAnnotationConfigTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,24 @@
1616

1717
package org.springframework.test.context.junit4.profile.importresource;
1818

19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.assertNull;
22+
1923
import org.junit.Test;
2024
import org.junit.runner.RunWith;
21-
2225
import org.springframework.beans.Employee;
2326
import org.springframework.beans.Pet;
2427
import org.springframework.beans.factory.annotation.Autowired;
2528
import org.springframework.test.context.ContextConfiguration;
2629
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
27-
import org.springframework.test.context.support.AnnotationConfigContextLoader;
28-
29-
import static org.junit.Assert.*;
3030

3131
/**
3232
* @author Juergen Hoeller
3333
* @since 3.1
3434
*/
3535
@RunWith(SpringJUnit4ClassRunner.class)
36-
@ContextConfiguration(classes = DefaultProfileConfig.class, loader = AnnotationConfigContextLoader.class)
36+
@ContextConfiguration(classes = DefaultProfileConfig.class)
3737
public class DefaultProfileAnnotationConfigTests {
3838

3939
@Autowired
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
* Copyright 2002-2012 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 org.springframework.test.context.junit4.spr9051;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertNotNull;
22+
import static org.junit.Assert.assertTrue;
23+
24+
import java.util.concurrent.atomic.AtomicInteger;
25+
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.springframework.beans.factory.annotation.Autowired;
29+
import org.springframework.context.annotation.Bean;
30+
import org.springframework.test.context.ContextConfiguration;
31+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
32+
33+
/**
34+
* This set of tests refutes the claims made in
35+
* <a href="https://jira.springsource.org/browse/SPR-9051" target="_blank">SPR-9051</a>.
36+
*
37+
* <p><b>The Claims</b>:
38+
*
39+
* <blockquote>
40+
* When a {@code @ContextConfiguration} test class references a config class
41+
* missing an {@code @Configuration} annotation, {@code @Bean} dependencies are
42+
* wired successfully but the bean lifecycle is not applied (no init methods are
43+
* invoked, for example). Adding the missing {@code @Configuration} annotation
44+
* solves the problem, however the problem and solution isn't obvious since
45+
* wiring/injection appeared to work.
46+
* </blockquote>
47+
*
48+
* @author Sam Brannen
49+
* @since 3.2
50+
*/
51+
@RunWith(SpringJUnit4ClassRunner.class)
52+
@ContextConfiguration(classes = AnnotatedConfigClassesWithoutAtConfigurationTests.AnnotatedFactoryBeans.class)
53+
public class AnnotatedConfigClassesWithoutAtConfigurationTests {
54+
55+
/**
56+
* This is intentionally <b>not</b> annotated with {@code @Configuration}.
57+
* Consequently, this class contains what we call <i>annotated factory bean
58+
* methods</i> instead of standard bean definition methods.
59+
*/
60+
static class AnnotatedFactoryBeans {
61+
62+
static final AtomicInteger enigmaCallCount = new AtomicInteger();
63+
64+
65+
@Bean
66+
public String enigma() {
67+
return "enigma #" + enigmaCallCount.incrementAndGet();
68+
}
69+
70+
@Bean
71+
public LifecycleBean lifecycleBean() {
72+
// The following call to enigma() literally invokes the local
73+
// enigma() method, not a CGLIB proxied version, since these methods
74+
// are essentially factory bean methods.
75+
LifecycleBean bean = new LifecycleBean(enigma());
76+
assertFalse(bean.isInitialized());
77+
return bean;
78+
}
79+
}
80+
81+
82+
@Autowired
83+
private String enigma;
84+
85+
@Autowired
86+
private LifecycleBean lifecycleBean;
87+
88+
89+
@Test
90+
public void simpleStringBean() {
91+
assertNotNull(enigma);
92+
assertEquals("enigma #1", enigma);
93+
}
94+
95+
@Test
96+
public void beanWithLifecycleCallback() {
97+
assertNotNull(lifecycleBean);
98+
assertEquals("enigma #2", lifecycleBean.getName());
99+
assertTrue(lifecycleBean.isInitialized());
100+
}
101+
102+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2002-2012 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 org.springframework.test.context.junit4.spr9051;
18+
19+
import javax.annotation.PostConstruct;
20+
21+
/**
22+
* Simple POJO that contains lifecycle callbacks.
23+
*
24+
* @author Sam Brannen
25+
* @since 3.2
26+
*/
27+
public class LifecycleBean {
28+
29+
private final String name;
30+
31+
private boolean initialized = false;
32+
33+
34+
public LifecycleBean(String name) {
35+
this.name = name;
36+
}
37+
38+
public String getName() {
39+
return this.name;
40+
}
41+
42+
@PostConstruct
43+
public void init() {
44+
initialized = true;
45+
}
46+
47+
public boolean isInitialized() {
48+
return this.initialized;
49+
}
50+
51+
}

0 commit comments

Comments
 (0)