读取spring配置文件的位置
来源:互联网 发布:浙大教授被骗婚 知乎 编辑:程序博客网 时间:2024/05/05 18:17
在spring 中 ,我们可以从项目的不同的文件位置读取spring 配置文件,相关位置有四种情况:
1> 在 源代码 src或与src 平齐的目录下
2> 在 WEB-INF 目录下,相对于WEB 工程
3> 在 源代码 src或src 的包下
4> 在 任意位置
相关代码如下:
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.beans.factory.xml.XmlBeanFactory;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import org.springframework.context.support.FileSystemXmlApplicationContext;
- import org.springframework.core.io.FileSystemResource;
- import org.springframework.core.io.Resource;
- import org.springframework.web.context.support.XmlWebApplicationContext;
- public class ReadSpringContext {
- /**
- * 读取spring配置文件的位置,在工作目录下<p>
- * 在eclipse工程中与工程名同级目录
- */
- public static ApplicationContext readFromProject(String xml) {
- //ApplicationContext context = new FileSystemXmlApplicationContext("one.xml");
- //UserBean ub = (UserBean)context.getBean("ub");
- //System.out.println(ub.getUid());
- return new FileSystemXmlApplicationContext(xml);
- }
- /**
- * 读取spring配置文件的位置,在web-inf目录
- */
- public static ApplicationContext readFromWebinf() {
- //ApplicationContext context = new XmlWebApplicationContext();
- //UserBean ub = (UserBean)context.getBean("ub");
- //System.out.println(ub.getUid());
- return new XmlWebApplicationContext();
- }
- /**
- * 读取spring配置文件的位置,在src或包目录
- */
- public static ApplicationContext readFromSrc(String xml) {
- //ApplicationContext context = new ClassPathXmlApplicationContext("one.xml");
- //ApplicationContext context = new ClassPathXmlApplicationContext("accp/y2/bean/one.xml");
- //UserBean ub = (UserBean)context.getBean("ub");
- //System.out.println(ub.getUid());
- return new ClassPathXmlApplicationContext(xml);
- }
- /**
- * 从任意位置读取spring配置文件
- */
- public static BeanFactory readFromAny(String xml) {
- //Resource rs=new FileSystemResource("d:/_temp/one.xml");
- //BeanFactory factory=new XmlBeanFactory(rs);
- //ApplicationContext context = new GenericApplicationContext();
- //Resource resource = context.getResource("file:d:/_temp/one.xml");
- //BeanFactory factory = new XmlBeanFactory(resource);
- //UserBean ub = (UserBean)factory.getBean("ub");
- //System.out.println(ub.getUid());
- Resource rs=new FileSystemResource(xml);
- return new XmlBeanFactory(rs);
- }
- public static void main(String[] args) {
- //readFromSrc();
- //readFromProject();
- //readFromAny();
- //readFromWebinf();
- }
- }
0 0
- 读取spring配置文件的位置
- 读取Spring配置文件的位置
- 读取spring配置文件的位置
- 读取spring配置文件的位置
- Spring配置文件的位置
- 指定spring配置文件的位置
- spring配置文件的位置问题
- 【Spring】关于spring配置文件的位置
- java中配置文件的位置和读取
- java web中读取配置文件的位置
- Spring读取配置文件的点点滴滴
- spring 读取配置文件的方法
- spring 读取配置文件的方法
- Spring读取配置文件的方法
- Spring读取配置文件的方式
- 如何设定spring配置文件的位置
- 指定spring配置文件的位置 ContextLoaderListener DispatcherServlet
- 自定义spring配置文件位置
- JNDI优缺点
- 3Sum
- 好想和你在一起,和你说缠绵的心语
- docker learn
- [LeetCode]Swap Nodes in Pairs
- 读取spring配置文件的位置
- Nginx日志分析解决方案awstats
- xcode 静态库的制作
- Android中自定义样式与View的构造函数中的第三个参数defStyle的意义
- Android APK优化工具Zipalign详解
- 撤销功能的实现----备忘录模式
- Excel导入Sqlserver中存在混合字符的列为空解决方案
- HashMap工作原理
- JavaScript出现“不能执行已释放的Script代码”错误的原因及解决办法