spring获取bean工具类
2016-01-20 13:33
441 查看
import java.io.ByteArrayInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.util.Calendar; import java.util.Date; import java.util.Properties; import javax.servlet.ServletContext; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; /** * 应用的共用方法 * */ public class ApplicationUtil { public static ServletContext WEB_CONTEXT=null; private static Properties config=null; private static ApplicationContext context=null; static{ load(); } private static void load(){ try { InputStream is=ApplicationUtil.class.getResourceAsStream("/config4j.properties"); config=new Properties(); config.load(is); config.clone(); context=new FileSystemXmlApplicationContext("classpath:applicationContext.xml"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 获得config4中的配置属性的值 * @param propertyName 属性名 * @return 属性值 */ public static String getProperty(String propertyName) { String result=""; if (config!=null) { result=config.getProperty(propertyName); } return result; } /** * 获得配置的Bean的方法 * @param beanName * @return */ public static Object getBean(String beanName) { return context.getBean(beanName); } /** * 解析XML获得根节点 * @param xml * @return 根节点的Element对象 * @throws DocumentException */ public static Element getRootElementFromXML(String xml) throws DocumentException{ SAXReader reader=new SAXReader(); Document document=null; try { document = reader.read(new ByteArrayInputStream(xml.getBytes("utf-8"))); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return document.getRootElement(); } public static synchronized String getOnlyNumber() { Calendar ca=Calendar.getInstance(); String result=""+ca.get(Calendar.HOUR)+ca.get(Calendar.MINUTE)+ca.get(Calendar.SECOND)+ca.get(Calendar.MILLISECOND); return result; } }
相关文章推荐
- SpringMVC日期类型转换问题三大处理方法归纳
- Spring集成Quartz定时任务框架介绍和Cron表达式详解
- spring回滚事务
- Gradle学习基础(2):构建简单的JAVA项目
- Java 原子操作与并发
- Spring 定时调度任务
- Java:String和Date、Timestamp之间的转换
- eclipse设置模板注释(新建类)
- 【ThinkingInJava】笔记
- struts2——(3)(①struts配置文件struts.xml三个元素package,action,result详解)
- 蓝桥杯-基础练习-闰年判断
- Spring在代码中获取bean的几种方式
- SpringMvc (五) 验证国际化/本地国际化解析器的配置(手动更改 locale)/spring 标签 messagee
- eclipse中部署web项目,出现A child container failed during start错误
- JavaWeb过滤器的运行机制理解及过滤器.监听器.拦截器-原理&区别
- java中try-catch-finally中的return语句
- Eclipse 使用 Gradle 下载 Jar 包全纪录
- java 中获取文件路径
- Struts2入门
- 'JAVAC' 不是内部或外部命令解决方法