请慎用spring-ClassPathXmlApplicationContext手动加载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>



  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值