请慎用spring-ClassPathXmlApplicationContext手动加载spring配置文件
2016-08-23 23:33
776 查看
(http://blog.csdn.net/maidou_2011/article/details/8447384)
在用spring做数据源配置的时候,如果代码中有用ClassPathXmlApplicationContext去加载spring配置文件,那么每次运行到此处代码,spring都会重新获得一个数据库连接。
如果浏览量太大就会导致超出数据库连接会话上写的错误,比如oracle会报出ORA-12519错误,临时修改数据库连接数治标不治本。
处理方式;
1、将需要用ClassPathXmlApplicationContext手动加载spring文件的类放到spring配置文件中去实例化,禁用ClassPathXmlApplicationContext直接调用。
2、将ClassPathXmlApplicationContext加载spring文件放到全局常量中(static标识)。
3、使用WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)在服务器启动时候直接加载web.xml配置中的spring配置文件
推荐第三种,并做详细介绍:
在用spring做数据源配置的时候,如果代码中有用ClassPathXmlApplicationContext去加载spring配置文件,那么每次运行到此处代码,spring都会重新获得一个数据库连接。
如果浏览量太大就会导致超出数据库连接会话上写的错误,比如oracle会报出ORA-12519错误,临时修改数据库连接数治标不治本。
处理方式;
1、将需要用ClassPathXmlApplicationContext手动加载spring文件的类放到spring配置文件中去实例化,禁用ClassPathXmlApplicationContext直接调用。
2、将ClassPathXmlApplicationContext加载spring文件放到全局常量中(static标识)。
3、使用WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext)在服务器启动时候直接加载web.xml配置中的spring配置文件
推荐第三种,并做详细介绍:
public class SpringDBInit { /** * 系统应用spring环境 */ private static ApplicationContext ctx; /** * 单实例对象 */ private static SpringDBInit instance = null; private SpringDBInit() { } /** * 获得单实例对象 * * @return */ public static SpringDBInit getInstance() { if (instance == null) { synchronized (SpringDBInit.class) { if (instance == null) { instance = new SpringDBInit(); } } } return instance; } /** * 初始化Spring组件 */ public void init(Properties props) throws Exception { loadContextXML(props); } /** * 加载spring对象 * * @param props */ private void loadContextXML(Properties props) throws Exception { /* * LogFactory.getInstance().logRun(RunPriority.INFORMATIONAL, * LogConstants.sysLogConstants.INT_SPRING_START, null ); */ try { ServletContext servletContext = (ServletContext) props .get("APP_CONTEXT"); if (servletContext != null) ctx = WebApplicationContextUtils .getRequiredWebApplicationContext(servletContext); } catch (Exception e) { e.printStackTrace(); } if ((ctx == null) || (ctx.getBeanDefinitionNames().length == 0)) { } } /** * 得到一个spring的配置对象 * * @param name * @return */ public Object getBean(String name) { if (ctx == null) return null; else return ctx.getBean(name); } /** * 获取单个信息 * * @param key * @param object * @param request * @return */ public static String getMessage(String key, Object[] object, Locale locale) { return ctx.getMessage(key, object, locale); } }
public class SpringDBUtil { /** * sjb管理类实例 */ private static SpringDBInit sdb = SpringDBInit.getInstance(); /** * 得到一个系统配置 bean * * @param name bean的配置名称 * @return 如果系统没有加载返回 null */ public static Object getBean(String name) { return sdb.getBean(name); } }
public class SpringInitServlet extends HttpServlet { static final long serialVersionUID = -1111516993124229949L; /** * 启动对象实例 */ private SpringDBInit sdbinit = SpringDBInit.getInstance(); /** * servlet初始化 */ public void init(ServletConfig config) throws ServletException { super.init(config); Properties props = new Properties(); props.put("APP_CONTEXT", config.getServletContext()); // 文件路径 String prefix = getServletContext().getRealPath("/"); // web应用路径 props.put("APP_PATH", prefix); try { sdbinit.init(props); } catch (Exception e) { } } }web.xml配置:
<servlet> <servlet-name>springInitServlet</servlet-name> <servlet-class>com.panda.util.springDB.SpringInitServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet>
相关文章推荐
- 请慎用spring-ClassPathXmlApplicationContext手动加载spring配置文件
- 【Spring学习笔记-2】Myeclipse下第一个Spring程序-通过ClassPathXmlApplicationContext加载配置文件
- 使用Spring中的ClassPathXmlApplicationContext加载配置文件
- Spring ClassPathXmlApplicationContext和FileSystemXmlApplicationContext读取配置文件的方法
- spring学习笔记(3)——配置文件applicationContext.xml的加载
- spring配置文件ApplicationContext.xml里面class等没有提示功能
- java中读取配置文件的方法fileSystemXmlapplicationContext与classPathXmlApplicationContext
- spring 配置文件无法加载,junit找不到xml配置文件java.lang.IllegalStateException: Failed to load ApplicationContext
- spring配置文件ApplicationContext.xml里面class等没有提示功能
- spring加载bean(ClassPathXmlApplicationContext和FileSystemXmlApplicationContext)
- Tomcat启动时,spring加载配置文件applicationContext.xml出错,抛出nested exception is og.xml.sax.SAXParseException
- Spring加载配置文件applicationContext.xml的方式
- IDEA 中读取不了配置文件以及ClassPathXmlApplicationContext与FileSystemXmlApplicationContext
- web.xml 配制加载spring上下文及 Spring context-param param-value classpath 配置加载问题
- Spring加载xml文件错误异常:Offending resource: class path resource [spring-context.xml];
- ClassPathXmlApplicationContext方式读取配置文件
- ClassPathXmlApplicationContext_filesystem方式获取applicationcontext配置文件信息
- applicationContext.xml文件中怎么配置struts2和spring配置文件的加载路径
- ClassPathXmlApplicationContext加载多个XML文件
- Spring的配置文件applicationContext.xml