Use spring-ClassPathXmlApplicationContext to manually load the spring configuration file with caution.

Source: Internet
Author: User

When spring is used for data source configuration, if the Code uses ClassPathXmlApplicationContext to load the spring configuration file, spring will obtain a new database connection every time it runs the code here. If the page views are too large, it will lead to a write error that exceeds the database connection session, for example, oracle will report a ORA-12519 error, temporary modification of the database connection to the permanent cure. Processing Method; 1. Place the class that needs to be manually loaded with ClassPathXmlApplicationContext in the spring configuration file for instantiation, and disable ClassPathXmlApplicationContext for direct calls. 2. Load the spring file into the ClassPathXmlApplicationContext and put it into a global constant (static identifier ). 3. Use WebApplicationContextUtils. getRequiredWebApplicationContext (servletContext) loads the web directly when the server is started. the third type of spring configuration file in xml configuration is recommended and detailed: [java] public class SpringDBInit {/*** System Application spring environment */private static ApplicationContext ctx; /*** Single instance Object */private static SpringDBInit instance = null; private SpringDBInit () {}/*** get a single instance object ** @ return */public static SpringDBInit getInstance () {if (instance = nu Ll) {synchronized (SpringDBInit. class) {if (instance = null) {instance = new SpringDBInit () ;}} return instance ;} /*** initialize Spring component */public void init (Properties props) throws Exception {loadContextXML (props );} /*** load spring object ** @ param props */private void loadContextXML (Properties props) throws Exception {/** LogFactory. getInstance (). logRun (RunPriority. INFORMATIONAL, * LogConstan Ts. 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) {}}/*** get a spring configuration Object ** @ param name * @ return */public Object getBean (String name) {if (ctx = null) return null; else return ctx. getBean (name);}/*** get a single message ** @ param key * @ param object * @ param request * @ return */public static String getMessage (String key, object [] object, Locale locale) {return ctx. getMessage (key, object, locale) ;}} [java] public class SpringDBUtil {/*** sjb management class instance */private static SpringDBInit sdb = SpringDBInit. getInstance (); /*** get a system configuration bean ** @ param name bean configuration name * @ return if the system does not load return null */public static Object getBean (String name) {return sdb. getBean (name) ;}} [java] public class SpringInitServlet extends HttpServlet {static final long serialVersionUID =-1111516993124229949L;/*** start object instance */private SpringDBInit sdbinit = SpringDBInit. getInstance ();/*** servlet initialization */public void init (ServletConfig config) throws ServletException {super. init (config); Properties props = new Properties (); props. put ("APP_CONTEXT", config. getServletContext (); // file path String prefix = getServletContext (). getRealPath ("/"); // web Application Path props. put ("APP_PATH", prefix); try {sdbinit. init (props);} catch (Exception e) {}} web. xml configuration: [html] <servlet> <servlet-name> springInitServlet </servlet-name> <servlet-class> com. panda. util. springDB. springInitServlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.