1、注解方式启动方法
//参数是使用@Configuration标记的类
//参数可以是多个
AnnotationConfigApplicationContext ctx =
new AnnotationConfigApplicationContext(SpringConfig.class);
2、加载Bean定义的流程
- 2.1) 把Spring内部Bean的后置处理器添加到Spring的上下文中的beanDefinitionMap中去
- 2.2) 添加包扫描策略的过滤器,默认的是使用扫描@Component注解标记的类,这里包含@Controller
@Service,@Repository,因为这三个注解都被@Component标记
- 2.3) 把启动类传入的被@Configuration标记的类的Bean定义添加到Spring的上下文中的beanDefinitionMap中去
- 2.4) 依次处理被@Configuration标记的类,解析@Configuration标记的类的流程为
@PropertySource -> @ComponentScan -> @Import -> @Bean ...
2.4.1) @ComponentScan 使用2.2)中添加的默认的过滤器找到@Component注解标记的类
2.4.2) 遍历通过@ComponentScan扫描的类,递归处理,处理流程和@Configuration一样