public void testIoc2() {ApplicationContext applicationContext = new AnnotationConfigApplicationContext( SpringConfig.class); Phone phone = applicationContext.getBean(Phone.class); System.out.println(phone.toString()); }Copy the code

or

public void testIoc() {   
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");  
    Phone phone = applicationContext.getBean(Phone.class);  
    System.out.println(phone.toString());
}
Copy the code

Generally we go through

Annotations to start class AnnotationConfigApplicationContext or ClassPathXmlApplicationContext to start, this is two different ways to achieve the load configuration information.

XML realize ClassPathXmlApplicationContext start class

Will use AbstractRefreshableApplicationContext implementation of container, the container locate the XML configuration files, and through BeanDefinition load and parse the XML configuration file.

The start of the annotation AnnotationConfigApplicationContext class

Through the rear ConfigurationClassPostProcessor processor to complete loading of the bean