@Import注解

转自:https://blog.csdn.net/heyutao007/article/details/74994161

@Import注解就是之前xml配置中的import标签,可以用于依赖第三方包中bean的配置和加载
在4.2之前只支持导入配置类
在4.2,@Import注解支持导入普通的java类,并将其声明成一个bean

[java] view plain copy
 
  1. public class DemoService {  
  2.     public void doSomething(){  
  3.         System.out.println("ok");  
  4.     }  
  5.   
  6. }  



[java] view plain copy
 
  1. import org.springframework.context.annotation.Configuration;  
  2. import org.springframework.context.annotation.Import;  
  3.   
  4. @Configuration  
  5. @Import(DemoService.class)//在spring 4.2之前是不不支持的  
  6. public class DemoConfig {  
  7.   
  8. }  



运行

[java] view plain copy
 
  1. import org.springframework.context.annotation.AnnotationConfigApplicationContext;  
  2.   
  3. public class Main {  
  4.     public static void main(String[] args) {  
  5.         AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("com..example");  
  6.         DemoService ds = context.getBean(DemoService.class);  
  7.         ds.doSomething();  
  8.     }  
  9.   
  10. }  



输出结果
ok

原文地址:https://www.cnblogs.com/sharpest/p/7991723.html