spring配置文件有以下3种配置方式:
一、简单单个spring配置文件:applicationContext.xml
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
RegisterDAO registerDAO = (RegisterDAO)ac.getBean("registerDAO");
(registerDAO为在applicationContext.xml中注入的bean)
二、多个spring配置文件,文件名称数组:
ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","dao.xml"});
RegisterDAO registerDAO = (RegisterDAO)ac.getBean("registerDAO");
(registerDAO为在dao.xml中注入的bean)
三、多个spring配置文件,也可以使用通配符:
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:/*.xml");
说明:以上spring配置文件都是放在src目录下的,如果src下还有package,需要带上package路径,例如:spring配置文件放在src下的springxml包下,则使用通配符的配置方式为:
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:springxml/*.xml");
本文深入探讨了Spring配置文件的三种主要配置方式:简单单个配置文件、多个配置文件以及使用通配符配置。通过具体实例展示了如何在不同场景下灵活运用这些配置方式。

被折叠的 条评论
为什么被折叠?



