文章字数:216,阅读全文大约需要1分钟
将普通类导入到
IOC
容器中的注解
导入Bean
的方法有
@Bean
注解@Controller
@Service
@Repository
@Component
等注解类,使用@ComponentScan
扫描包@Import
方法注入(4.2之前只能导入配置类)
常用方式
- 在
@Enablexxx
注解中使用,此注解声明的类在加载之前会加载@inport
注解中的类 - 配合
ImportSelector.class
接口使用
1 | // BeanFactoryAware可以获取beanFactory相关信息 |
1 | // 声明注解,并使用MyImportSelector |
1 | // 在配置类上使用注解,就能在加载配置类之前加载好选择的类 |