1
1
/*
2
- * Copyright 2002-2024 the original author or authors.
2
+ * Copyright 2002-2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .context .annotation .configuration ;
18
18
19
- import java .util .Collections ;
20
-
21
19
import org .aspectj .lang .annotation .Aspect ;
22
20
import org .aspectj .lang .annotation .Before ;
23
21
import org .junit .jupiter .api .Test ;
24
22
25
23
import org .springframework .aop .support .AopUtils ;
26
24
import org .springframework .beans .factory .annotation .Autowired ;
27
25
import org .springframework .beans .factory .annotation .Value ;
26
+ import org .springframework .beans .factory .support .BeanDefinitionRegistry ;
28
27
import org .springframework .beans .testfixture .beans .TestBean ;
29
28
import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
30
29
import org .springframework .context .annotation .Bean ;
31
30
import org .springframework .context .annotation .Configuration ;
32
31
import org .springframework .context .annotation .ImportResource ;
33
- import org .springframework .core .env .MapPropertySource ;
34
- import org .springframework .core .env .PropertySource ;
32
+ import org .springframework .core .testfixture .env .MockPropertySource ;
35
33
36
34
import static org .assertj .core .api .Assertions .assertThat ;
37
35
38
36
/**
39
- * Integration tests for {@link ImportResource} support.
37
+ * Integration tests for {@link ImportResource @ImportResource } support.
40
38
*
41
39
* @author Chris Beams
42
40
* @author Juergen Hoeller
45
43
class ImportResourceTests {
46
44
47
45
@ Test
48
- void importXml () {
49
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlConfig .class );
50
- assertThat (ctx .containsBean ("javaDeclaredBean" )).as ("did not contain java-declared bean" ).isTrue ();
51
- assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("did not contain xml-declared bean" ).isTrue ();
52
- TestBean tb = ctx .getBean ("javaDeclaredBean" , TestBean .class );
53
- assertThat (tb .getName ()).isEqualTo ("myName" );
54
- ctx . close ();
46
+ void importResource () {
47
+ try ( AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlConfig .class )) {
48
+ assertThat (ctx .containsBean ("javaDeclaredBean" )).as ("did not contain java-declared bean" ).isTrue ();
49
+ assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("did not contain xml-declared bean" ).isTrue ();
50
+ TestBean tb = ctx .getBean ("javaDeclaredBean" , TestBean .class );
51
+ assertThat (tb .getName ()).isEqualTo ("myName" );
52
+ }
55
53
}
56
54
57
55
@ Test
58
- void importXmlIsInheritedFromSuperclassDeclarations () {
59
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (FirstLevelSubConfig .class );
60
- assertThat (ctx .containsBean ("xmlDeclaredBean" )).isTrue ();
61
- ctx . close ();
56
+ void importResourceIsInheritedFromSuperclassDeclarations () {
57
+ try ( AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (FirstLevelSubConfig .class )) {
58
+ assertThat (ctx .containsBean ("xmlDeclaredBean" )).isTrue ();
59
+ }
62
60
}
63
61
64
62
@ Test
65
- void importXmlIsMergedFromSuperclassDeclarations () {
66
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (SecondLevelSubConfig .class );
67
- assertThat (ctx .containsBean ("secondLevelXmlDeclaredBean" )).as ("failed to pick up second-level-declared XML bean" ).isTrue ();
68
- assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("failed to pick up parent-declared XML bean" ).isTrue ();
69
- ctx . close ();
63
+ void importResourceIsMergedFromSuperclassDeclarations () {
64
+ try ( AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (SecondLevelSubConfig .class )) {
65
+ assertThat (ctx .containsBean ("secondLevelXmlDeclaredBean" )).as ("failed to pick up second-level-declared XML bean" ).isTrue ();
66
+ assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("failed to pick up parent-declared XML bean" ).isTrue ();
67
+ }
70
68
}
71
69
72
70
@ Test
73
- void importXmlWithNamespaceConfig () {
74
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlWithAopNamespaceConfig .class );
75
- Object bean = ctx .getBean ("proxiedXmlBean" );
76
- assertThat (AopUtils .isAopProxy (bean )).isTrue ();
77
- ctx . close ();
71
+ void importResourceWithNamespaceConfig () {
72
+ try ( AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlWithAopNamespaceConfig .class )) {
73
+ Object bean = ctx .getBean ("proxiedXmlBean" );
74
+ assertThat (AopUtils .isAopProxy (bean )).isTrue ();
75
+ }
78
76
}
79
77
80
78
@ Test
81
- void importXmlWithOtherConfigurationClass () {
82
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlWithConfigurationClass .class );
83
- assertThat (ctx .containsBean ("javaDeclaredBean" )).as ("did not contain java-declared bean" ).isTrue ();
84
- assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("did not contain xml-declared bean" ).isTrue ();
85
- TestBean tb = ctx .getBean ("javaDeclaredBean" , TestBean .class );
86
- assertThat (tb .getName ()).isEqualTo ("myName" );
87
- ctx . close ();
79
+ void importResourceWithOtherConfigurationClass () {
80
+ try ( AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlWithConfigurationClass .class )) {
81
+ assertThat (ctx .containsBean ("javaDeclaredBean" )).as ("did not contain java-declared bean" ).isTrue ();
82
+ assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("did not contain xml-declared bean" ).isTrue ();
83
+ TestBean tb = ctx .getBean ("javaDeclaredBean" , TestBean .class );
84
+ assertThat (tb .getName ()).isEqualTo ("myName" );
85
+ }
88
86
}
89
87
90
88
@ Test
91
89
void importWithPlaceholder () {
92
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ();
93
- PropertySource <?> propertySource = new MapPropertySource ("test" ,
94
- Collections .<String , Object > singletonMap ("test" , "springframework" ));
95
- ctx .getEnvironment ().getPropertySources ().addFirst (propertySource );
96
- ctx .register (ImportXmlConfig .class );
97
- ctx .refresh ();
98
- assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("did not contain xml-declared bean" ).isTrue ();
99
- ctx .close ();
90
+ try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ()) {
91
+ ctx .getEnvironment ().getPropertySources ().addFirst (new MockPropertySource ("test" ).withProperty ("test" , "springframework" ));
92
+ ctx .register (ImportXmlConfig .class );
93
+ ctx .refresh ();
94
+ assertThat (ctx .containsBean ("xmlDeclaredBean" )).as ("did not contain xml-declared bean" ).isTrue ();
95
+ }
100
96
}
101
97
102
98
@ Test
103
- void importXmlWithAutowiredConfig () {
104
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlAutowiredConfig .class );
105
- String name = ctx .getBean ("xmlBeanName" , String .class );
106
- assertThat (name ).isEqualTo ("xml.declared" );
107
- ctx . close ();
99
+ void importResourceWithAutowiredConfig () {
100
+ try ( AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportXmlAutowiredConfig .class )) {
101
+ String name = ctx .getBean ("xmlBeanName" , String .class );
102
+ assertThat (name ).isEqualTo ("xml.declared" );
103
+ }
108
104
}
109
105
110
106
@ Test
111
107
void importNonXmlResource () {
112
- AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportNonXmlResourceConfig .class );
113
- assertThat (ctx .containsBean ("propertiesDeclaredBean" )).isTrue ();
114
- ctx .close ();
108
+ try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportNonXmlResourceConfig .class )) {
109
+ assertThat (ctx .containsBean ("propertiesDeclaredBean" )).isTrue ();
110
+ }
111
+ }
112
+
113
+ @ Test
114
+ void importResourceWithPrivateReader () {
115
+ try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext (ImportWithPrivateReaderConfig .class )) {
116
+ assertThat (ctx .containsBean ("propertiesDeclaredBean" )).isTrue ();
117
+ }
115
118
}
116
119
117
120
118
121
@ Configuration
119
122
@ ImportResource ("classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml" )
120
123
static class ImportXmlConfig {
124
+
121
125
@ Value ("${name}" )
122
126
private String name ;
127
+
123
128
@ Bean public TestBean javaDeclaredBean () {
124
129
return new TestBean (this .name );
125
130
}
@@ -146,6 +151,7 @@ static class ImportXmlWithAopNamespaceConfig {
146
151
147
152
@ Aspect
148
153
static class AnAspect {
154
+
149
155
@ Before ("execution(* org.springframework.beans.testfixture.beans.TestBean.*(..))" )
150
156
public void advice () { }
151
157
}
@@ -158,18 +164,37 @@ static class ImportXmlWithConfigurationClass {
158
164
@ Configuration
159
165
@ ImportResource ("classpath:org/springframework/context/annotation/configuration/ImportXmlConfig-context.xml" )
160
166
static class ImportXmlAutowiredConfig {
161
- @ Autowired TestBean xmlDeclaredBean ;
162
167
163
- @ Bean public String xmlBeanName () {
168
+ @ Autowired
169
+ TestBean xmlDeclaredBean ;
170
+
171
+ @ Bean
172
+ public String xmlBeanName () {
164
173
return xmlDeclaredBean .getName ();
165
174
}
166
175
}
167
176
168
177
@ SuppressWarnings ("deprecation" )
169
178
@ Configuration
170
- @ ImportResource (locations = "classpath: org/springframework/context/annotation/configuration/ImportNonXmlResourceConfig-context .properties" ,
179
+ @ ImportResource (locations = "org/springframework/context/annotation/configuration/ImportNonXmlResourceConfig.properties" ,
171
180
reader = org .springframework .beans .factory .support .PropertiesBeanDefinitionReader .class )
172
181
static class ImportNonXmlResourceConfig {
173
182
}
174
183
184
+ @ SuppressWarnings ("deprecation" )
185
+ @ Configuration
186
+ @ ImportResource (locations = "org/springframework/context/annotation/configuration/ImportNonXmlResourceConfig.properties" ,
187
+ reader = PrivatePropertiesBeanDefinitionReader .class )
188
+ static class ImportWithPrivateReaderConfig {
189
+ }
190
+
191
+ @ SuppressWarnings ("deprecation" )
192
+ private static class PrivatePropertiesBeanDefinitionReader
193
+ extends org .springframework .beans .factory .support .PropertiesBeanDefinitionReader {
194
+
195
+ PrivatePropertiesBeanDefinitionReader (BeanDefinitionRegistry registry ) {
196
+ super (registry );
197
+ }
198
+ }
199
+
175
200
}
0 commit comments