Spring加载applicationContext.xml应用上下文的方式

来源:互联网 发布:美图拼图软件 编辑:程序博客网 时间:2024/05/16 11:30

一、利用ClassPathXmlApplicationContext从classpath中读取XML文件

例1:
/* 加载应用上下文(单个文件) ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");ApplicationContext applicationContext = new ClassPathXmlApplicationContext("resource/applicationContext.xml");"src/resource/"ApplicationContext applicationContext = new ClassPathXmlApplicationContext("file:D:/usermanage/src/applicationContext.xml");*//* 加载应用上下文(多个文件) */ApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml","applicationContext_pro.xml"});/* 获取beanBeanFactory factory = applicationContext; IUserService userService = (IUserService) factory.getBean("userService"); */IUserService userService = (IUserService) applicationContext.getBean("userService");testAddUser(userService);//业务

例2:
/* 加载应用上下文(单个文件)ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); *//* 加载应用上下文(多个文件) */ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });// 获取beanIUserService userService = (IUserService) applicationContext.getBean("userService");testAddUser(userService);// 关闭上下文applicationContext.close();

以上两例区别可参考:http://blog.csdn.net/chenlong220192/article/details/46534991

二、利用ClassPathResource,从classpath中读取XML文件

例3:
/* 加载应用上下文(单个文件) */Resource resource = new ClassPathResource("applicationContext.xml"); // 获取beanBeanFactory factory=new XmlBeanFactory(resource); IUserService userService = (IUserService)factory.getBean("userService");testAddUser(userService);

三、利用XmlWebApplicationContext读取( 专为Web工程定制)

例4:
/* 加载应用上下文 */XmlWebApplicationContext xmlWebApplicationContext = new XmlWebApplicationContext();xmlWebApplicationContext.setConfigLocations(new String[] {"applicationContext.xml"}); xmlWebApplicationContext.setServletContext(pageContext.getServletContext()); xmlWebApplicationContext.refresh();// 获取beanIUserService userService = (IUserService ) xmlWebApplicationContext.getBean("userService ");testAddUser(userService);// 关闭上下文xmlWebApplicationContext.close();

四、利用FileSystemResource读取

例5:
/* 加载应用上下文 */Resource resource = new FileSystemResource("D:/tomcat/webapps/test/WEB-INF/classes/applicationContext.xml"); // 获取beanBeanFactory factory = new XmlBeanFactory(resource);IUserService userService = (IUserService) factory.getBean("userService");testAddUser(userService);
注意:利用FileSystemResource读取,则配置文件必须放在project根目录下,或者写明绝对路径,否则就会抛出找不到文件的异常。

五、利用FileSystemXmlApplicationContext读取,可以指定XML定义文件的相对路径或者绝对路径来读取定义文件。

例6:
/* 1 */String[] path={"WebContent/WEB-INF/applicationContext.xml","WebContent/WEB-INF/applicationContext_task.xml"};ApplicationContext context = new FileSystemXmlApplicationContext(path);/* 2String path="WebContent/WEB-INF/applicationContext*.xml"; ApplicationContext context = new FileSystemXmlApplicationContext(path); *//* 3ApplicationContext ctx = new FileSystemXmlApplicationContext("classpath:地址"); */

1 0
原创粉丝点击