1.在classpath路径下寻找
ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/spring/d_applicationContext/applicationContext_dao.xml");
2.
配置文件和Main这个类在同一个包中
ApplicationContext ac2 = new ClassPathXmlApplicationContext("applicationContext_dao.xml", Main.class);
3.有多个配置文件的情况
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] {// "cn/itcast/spring/d_applicationContext/applicationContext_service.xml",// "cn/itcast/spring/d_applicationContext/applicationContext_dao.xml" });
我们知道service会依赖dao,那么是不是意味着与配置文件读取的顺序有关呢?
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] {// "cn/itcast/spring/d_applicationContext/applicationContext_dao.xml",// "cn/itcast/spring/d_applicationContext/applicationContext_service.xml" });
这样也是可以的,它会自己处理依赖关系。它会自动初始化那些被依赖的对象。
4.
ApplicationContext ac2 = new ClassPathXmlApplicationContext(new String[] {// "applicationContext_dao.xml", "applicationContext_service.xml" },Main.class);
时间: 2024-10-29 19:09:47