三、初始化Ioc容器
spring对bean的管理提供了三种配置方式
1.基于XML配置bean
2.基于注解配置bean
3.基于java代码配置bean
三种配置方式本质是一样的,都是告诉springIoc容器如何实例化,如何管理这些bean
基于XML配置bean
在applicationContext.xml中所有的bean统一使用
<bean id="sweetApple" class="com.spring.ioc.entity.Apple">
<property name="title" value="红富士">property>
<property name="origin" value="欧洲">property>
<property name="color" value="红色">property>
bean>
spring提供了一个核心对象ApplicationContext,ApplicationContext本质是一个接口,有很多具体实现类,其中ClassPathXMLApplicationContext类实现了从当前类路径读取xml文件并解析加载
//创建IoC容器并根据配置文件创建对象
ApplicationContext context =
new ClassPathXmlApplicationContext("classpath:applicationContext.xml");