beanFactory:
public void testIOC1() throws Exception{ Resource resource =new ClassPathResource("cn/spy/first/applicationContext.xml"); @SuppressWarnings("deprecation") BeanFactory factory =new XmlBeanFactory(resource); User user =(User) factory.getBean("user"); System.out.println(user.getId()+" : "+user.getUsername()); }
Applicetion
public void testIOC2() throws Exception{ ApplicationContext context= new ClassPathXmlApplicationContext("cn/spy/first/applicationContext.xml"); System.out.println("-----容器创建完成-----"); User user =(User) context.getBean("user"); System.out.println(user.getId()+" : "+user.getUsername()); }