ApplicationContext 的三個常用實現類
(1)區別
ClassPathXmlApplicationContext:
可以加載類路徑下的配置文件,要求配置文件必須在類路徑下。不在的話,加載不了
FileSystemXmlApplicationContext
可以加載磁盤任意路徑下的配置文件(必須有訪問權限)
AnnotationConfigApplicationContext
用於讀取註解創建容器
(2)ClassPathXmlApplicationContext案例
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
Hello hello = (Hello) act.getBean("hello");
hello.show();
(3)FileSystemXmlApplicationContext案例
ApplicationContext act = new FileSystemXmlApplicationContext("D:/javaWorkSpace/spring2/src/applicationContext.xml");
person persons = (person) act.getBean("persons");
persons.show();
其中applicationContext.xml文件如下:
<bean id="hello" class="com.njau.services.HelloDaoImpl">
<property name="pname" value=""></property>
</bean>
<bean id="persons" class="com.njau.services.person" init-method="init">
<property name="pid" value="10"></property>
<property name="pname" value="AA"></property>
</bean>