# springlearn **Repository Path**: dawenxi484/springlearn ## Basic Information - **Project Name**: springlearn - **Description**: 学习spring - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-02-23 - **Last Updated**: 2021-02-23 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # SpringIoC容器的源码解析 ## SpringIoC容器使用 * 解析配置 * 定位与注册对象 * 注入对象 ### 基于xml 1. 编写配置文件 ```xml ``` 2. 解析配置注入对象 ```java String xmlPath = "D:\\projects\\IdeaProject\\spring-framework-5.2.10.RELEASE\\spring_demo\\src\\main\\resources\\spring\\spring-config.xml"; ApplicationContext applicationContext = new FileSystemXmlApplicationContext(xmlPath); WelcomeService welcomeService = (WelcomeService)applicationContext.getBean("welcomeService"); welcomeService.sayHello("强大的spring框架"); ``` ### 基于注解 ```java @Configuration @ComponentScan("com.example.springdemo") public class Entrance { public static void main(String[] args) { // 指定配置类 ApplicationContext applicationContext = new AnnotationConfigApplicationContext(Entrance.class); String[] beanDefinitionNames = applicationContext.getBeanDefinitionNames(); for(String beanDefinitionName: beanDefinitionNames) { System.out.println(beanDefinitionName); } WelcomeController welcomeController = (WelcomeController)applicationContext.getBean("welcomeController"); welcomeController.handleRequest(); } } ```