spring核心API

来源:互联网 发布:英语音标学习软件 编辑:程序博客网 时间:2024/04/29 06:41

spring核心API


BeanFactory :

这是一个工厂,用于生成任意bean。

采取延迟加载,第一次getBean时才会初始化Bean

@Testpublic void demo02(){//使用BeanFactory  --第一次条用getBean实例化String xmlPath = "com/itheima/b_di/beans.xml";BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(xmlPath));BookService bookService = (BookService) beanFactory.getBean("bookServiceId");bookService.addBook();}

ApplicationContext:

是BeanFactory的子接口,功能更强大。(国际化处理、事件传递、Bean自动装配、各种不同应用层的Context实现)。当配置文件被加载,就进行对象实例化。

       ClassPathXmlApplicationContext 用于加载classpath(类路径、src)下的xml

              加载xml运行时位置 --> /WEB-INF/classes/...xml

       FileSystemXmlApplicationContext 用于加载指定盘符下的xml

              加载xml运行时位置 --> /WEB-INF/...xml

                     通过java webServletContext.getRealPath() 获得具体盘符

@Testpublic void demo01(){//从spring容器获得String xmlPath = "com/itheima/b_di/beans.xml";ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);BookService bookService = (BookService) applicationContext.getBean("bookServiceId");bookService.addBook();}





0 0