-
Notifications
You must be signed in to change notification settings - Fork 38.6k
Closed
Labels
in: coreIssues in core modules (aop, beans, core, context, expression)Issues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancementA general enhancement
Milestone
Description
I'm not sure it's a bug or undocumented limitation.
If no @Bean
defined in Config
then @Bean
defined in BaseConfig
will be ignored.
@ContextConfiguration(classes = Config.class)
@ExtendWith(SpringExtension.class)
public class InjectionTests {
@Autowired
private String bean1;
@Autowired
private String bean2;
@Test
void test() {
assertThat(bean1).isEqualTo("bean1");
assertThat(bean2).isEqualTo("bean2");
}
}
class Config extends BaseConfig {
}
class BaseConfig {
@Bean
String bean1() {
return "bean1";
}
@Bean
String bean2() {
return "bean2";
}
}
there are many workarounds
- move any one of
@Bean
toConfig
class Config extends BaseConfig {
@Bean
String bean2() {
return "bean2";
}
}
class BaseConfig {
@Bean
String bean1() {
return "bean1";
}
}
- mark
@Configuration
or@Component
onConfig
@Configuration
// @Component
class Config extends BaseConfig {
}
class BaseConfig {
@Bean
String bean1() {
return "bean1";
}
@Bean
String bean2() {
return "bean2";
}
}
- use
@Import
instead ofContextConfiguration
@Import(Config.class)
@ExtendWith(SpringExtension.class)
public class InjectionTests {
@Autowired
private String bean1;
@Autowired
private String bean2;
@Test
void test() {
assertThat(bean1).isEqualTo("bean1");
assertThat(bean2).isEqualTo("bean2");
}
}
Metadata
Metadata
Assignees
Labels
in: coreIssues in core modules (aop, beans, core, context, expression)Issues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancementA general enhancement