您的位置:首页 > 移动开发

请慎用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配置文件

推荐第三种,并做详细介绍:

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐