转载:https://z724130632.iteye.com/blog/2373607
@Import注解在4.2之前只支持导入配置类;在4.2以及之后,@Import注解支持导入普通java类,并将其声明成一个bean。
业务类:
Java代码
public class DemoService {
public void doSomething(){
System.out.println("everything is all fine");
}
}
配置类:
Java代码
@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();
}
}