标签:figure text com 技术 class 容器 eve aop img
主要包:org.springframework.beans 和 org.springframework.context
主要类:
BeanFactory:提供基本的类的管理的配置机制
ApplicationContext:在BeanFactory的基础上提供 SpringAOP,消息资源管理,事件发布,以及应用层context例如WebApplicationContext
ApplicationContext通过加载xml,JavaAnnotations,JavaCode等形式的元数据来实例化,配置和组装Bean
一些开箱即用的ApplicationContext,在单独的应用中常用的有ClassPathXmlApplicationContext 或者 FileSystemXmlApplicationContext.
xml配置文件的基本框架
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
从CLASSPATH加载配置
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
<beans>
<import resource="services.xml"/>
<import resource="resources/messageSource.xml"/>
<import resource="/resources/themeSource.xml"/>
<bean id="bean1" class="..."/>
<bean id="bean2" class="..."/>
</beans>
从容器中获得Bean
// create and configure beans
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
// retrieve configured instance
PetStoreService service = context.getBean("petStore", PetStoreService.class);
// use configured instance
List<String> userList = service.getUsernameList();
更灵活的容器
GenericApplicationContext context = new GenericApplicationContext();
new XmlBeanDefinitionReader(context).loadBeanDefinitions("services.xml", "daos.xml");
context.refresh();
标签:figure text com 技术 class 容器 eve aop img
原文地址:https://www.cnblogs.com/zhouyu0-0/p/11784903.html