从@Import
注释 Javadoc 中:
* <p>Provides functionality equivalent to the {@code <import/>} element in Spring XML.
* Only supported for classes annotated with {@code @Configuration} or declaring at least
* one {@link Bean @Bean} method, as well as {@link ImportSelector} and
* {@link ImportBeanDefinitionRegistrar} implementations.
*
* <p>{@code @Bean} definitions declared in imported {@code @Configuration} classes
* should be accessed by using {@link org.springframework.beans.factory.annotation.Autowired @Autowired}
* injection. Either the bean itself can be autowired, or the configuration class instance
* declaring the bean can be autowired. The latter approach allows for explicit,
* IDE-friendly navigation between {@code @Configuration} class methods.
*
* <p>May be declared at the class level or as a meta-annotation.
*
* <p>If XML or other non-{@code @Configuration} bean definition resources need to be
* imported, use {@link ImportResource @ImportResource}
我假设如果您导入@Configuration
类本身,即“后一种方法”,那么您只需@Bean
在导入的类上显式调用该方法,例如
@Configuration
@Import(BarConfiguration.class)
public class FooConfiguration {
@Autowired
private BarConfiguration barConfiguration;
@Bean
public Foo foo() {
Foo foo = new Foo();
foo.setBar(barConfiguration.bar());
return foo;
}
}