@Import注解在4.2之前只支持导入配置类;在4.2以及之后,@Import注解支持导入普通java类,并将其声明成一个bean。
业务类:
public class DemoService {
public void doSomething(){
System.out.println("everything is all fine");
}
}
配置类:
@Configuration
@Import(DemoService.class)
public class DemoConfig {
}
运行:
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.example");
DemoService ds = context.getBean(DemoService.class);
ds.doSomething();
}
}
输出结果:everything is all fine