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
- Spring加载applicationContext.xml应用上下文的方式
- Spring 加载xml配置文件的方式 ApplicationContext
- Spring加载配置文件applicationContext.xml的方式
- 应用上下文-applicationContext.xml
- Spring中加载ApplicationContext.xml文件的方式
- Spring加载applicationContext.xml的几种方式
- Spring中加载ApplicationContext.xml文件的方式
- Spring中加载ApplicationContext.xml文件的方式【转】
- Spring中加载ApplicationContext.xml文件的方式
- Spring加载applicationContext.xml的几种方式
- Spring中加载ApplicationContext.xml文件的方式
- Spring加载applicationContext.xml的几种方式
- Spring中加载ApplicationContext.xml文件的方式
- Spring中加载ApplicationContext.xml文件的方式
- Spring中加载ApplicationContext.xml文件的方式
- Spring中加载ApplicationContext.xml文件的方式
- spring加载ApplicationContext.xml的四种方式
- spring ApplicationContext.xml几种加载方式
- Java中的数字基本运算和精度问题
- gdb 死锁线程
- JAVA的用途是什么?如果学好JAVA能干什么【转】
- 关于子查询的一些小东西
- 【C语言】模拟实现strchr函数.即在一个字符串中查找一个字符第一次出现的位置并返回
- Spring加载applicationContext.xml应用上下文的方式
- git实用命令集锦
- springAOP日志记录数据库项目实例
- Python Windows error code
- 处理json中的null
- 开店须知!2015年淘宝搜索重点优化
- 第28题:求整数的二进制表示中1的个数
- 1041. Be Unique (20)
- 直线与边缘检测