spring-classPathXmlApplicationContextを使用してspringプロファイルを手動でロードしてください

4023 ワード

Springでデータ・ソース構成を行う場合、コードにC l a ssPathXmlApplicationContextを使用してspringプロファイルをロードすると、ここでコードを実行するたびにspringはデータベース接続を再取得します.
ブラウズ量が大きすぎると、oracleがORA-12519エラーを報告したり、データベース接続数を一時的に変更したりして、データベース接続セッションに書かれたエラーを超えてしまいます.
処理方法
1.P r a s P h t h e XmlApplicationContextで手動でspringファイルをロードする必要があるクラスをspringプロファイルに配置してインスタンス化し、ClassPathXmlApplicationContextの直接呼び出しを無効にする.
2.C l a s s PathXmlApplicationContextをspringファイルにロードしてグローバル定数に入れる(static ID).
3.WebApplicationContextUtilsを使用する.getRequiredWebApplicationContext(servletContext)はサーバの起動時に直接webをロードする.xml構成のspringプロファイル
3つ目をお勧めします.詳しく説明します.
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構成:

		springInitServlet
		com.panda.util.springDB.SpringInitServlet
		1