spring配备加载(部分)
发布时间:2011-06-18 12:14:27 文章来源:www.iduyao.cn 采编人员:星星草
spring配置加载(部分)
package com.suning.crawler.util;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 手动加载spring
*
* @author xxx
*/
public final class SpringBean
{
/**
* spring容器加载
*/
private static ApplicationContext context;
/**
* 私有构造函数
*/
private SpringBean()
{
}
/**
* @param beanId
* beanID
* @return bean
*/
public static Object getBean(String beanId)
{
// 初始化Spring
if (null == context)
{
initial();
}
return context.getBean(beanId);
}
/**
* 初始化Spring容器
*/
public static void initial()
{
// 获取spring配置文件
String[] configLocation = getConfigLocation();
// spring 初始化实例
context = new ClassPathXmlApplicationContext(configLocation);
}
/**
* 获取spring配置文件地址
*
* @return config目录下的spring配置文件地址
*/
private static String[] getConfigLocation()
{
// spring文件地址
List<String> configLocation = new ArrayList<String>();
// 当前路径
String basePath = SpringBean.class.getClassLoader().getResource("").getPath();
File currentPath = new File(basePath);
// 递归遍历spring文件
recursizeConfigLocation(configLocation, currentPath.getAbsolutePath(), currentPath);
return configLocation.toArray(new String[configLocation.size()]);
}
private static void recursizeConfigLocation(List<String> configLocation, String relativePath, File currentFile)
{
// 目录
if (currentFile.exists() && currentFile.isDirectory())
{
// 遍历当前所有文件
File[] currentFiles = currentFile.listFiles();
// 文件名称
for (File targetFile : currentFiles)
{
recursizeConfigLocation(configLocation, relativePath, targetFile);
}
}
// 文件
else if (currentFile.exists() && currentFile.isFile())
{
// 获取文件名称
String targetName = currentFile.getName();
// 过滤spring文件
if (null != targetName && targetName.matches(".+\\.spring\\.xml"))
{
targetName = currentFile.getAbsolutePath();
targetName = targetName.replace(relativePath, ".");
configLocation.add(targetName);
}
}
}
}
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。
其他相似内容:
-
SigMOD2012感兴趣的论文(时空数据处理)——记下慢慢看
图中联合做距离数据处理:
Efficient Processing of Distance Queries in Larg...
-
怎样控制你的情绪在项目中,释放压力
在项目中由于甲方,乙方的关系,涉及到彼此的利益,所以争吵是在所难免的;客户要求你把功能,字段...
-
ISTQB AL-TM连载系列14:处理缺陷的严重程度与优先级
正确处理和区分缺陷的严重程度和优先级是所有的软件开发和测试相关人员的重要...
-
ISTQB AL-TM连载系列11:基于会话的探索性测试管理
探索性测试是一个特殊的测试过程,它的测试活动和测试内容是动态变化的,更多的是通...
-
【在线研讨】《用户故事分类与组织结构(一期)》2012-06-26(周二)
主题:《用户故事分类与组织结构——火星人的需求管理理念(一期)》日期:2...
-
探索流程的奥秘之三, 如何梳理业务流程
软件开发的难点之一是如何了解客户的需求,现实工作中,开发者们就像瞎子摸象一样从用...
-
ISTQB AL-TM连载系列13:搭建有效的缺陷管理过程
缺陷是测试过程中的重要输出和工作产品。缺陷的生命周期是一系列的活动和状态所组...
-
项目经理思考什么是团队
在软件项目实施过程中大家谈的最多的可能是“需求”这个词,之后谈的较多的是“团队”相关的话题。求职者...
-
通过robbin的管理问题谈自己遇到的管理问题
首先澄清每个公司,每个管理团队都有着不同的背景和状况。很多事情都不能一概而论,就和...
-
loadrunner、QTP、TestDirector学习笔记
在Windows XP下安装了loadrunner 8.1、QTP 9.2、TestDirector 8.0
运行菜单Samples Set...