Considerations for new Classpathxmlapplicationcontext () in the program

Source: Internet
Author: User

Background: In our web program, we use spring to manage individual instances (beans), and sometimes in a program to use a bean that has already been instantiated, this code is often used:

[Java]View PlainCopy
    1. ApplicationContext AppContext =
    2. New Classpathxmlapplicationcontext ("Classpath:meta-inf/spring/applicationcontext-common.xml");
    3. ApplicationContext Appcontext=appcontextutil.getcontext ();
    4. Projectservicefacade projectservicefacade= (Projectservicefacade) Appcontext.getbean ("  Biz.projectservicefacade ");

The code above poses a problem: because it reloads the applicationcontext-common.xml and instantiates the context bean, if some of the thread configuration classes are in this configuration file, then the threads that do the same work will be restarted two times. One is started when the Web container is initialized, and the other is instantiated once by the code shown above. This is to be avoided in business.

Workaround: Do not use a new Classpathxmlapplicationcontext ()-like method to get the instantiated bean from the existing spring context.

Steps: 1) Add a Applicationcontextutil class to get the context ApplicationContext

[Java]View PlainCopy
  1. Public class Applicationcontextutil implements Applicationcontextaware {
  2. private ApplicationContext context; //Declare a static variable to save
  3. @Override
  4. public void Setapplicationcontext (ApplicationContext applicationcontext)
  5. throws Beansexception {
  6. This.context=applicationcontext;
  7. }
  8. Public ApplicationContext GetContext () {
  9. return context;
  10. }
  11. }


2) Add spring configuration to the Applicationcontextutil class, let spring complete the loading of this tool class and complete the injection of the ApplicationContext context ; The applicationcontext-common.xml is configured as follows:

[Java]View PlainCopy
[Java]View PlainCopy
    1. Initializes the Appliationutil class and completes the ApplicationContext injection
[Java]View PlainCopy
    1. <bean id="Applicationcontextutil" class="Com.service.utils.ApplicationContextUtil" ></ bean>
[Java]View PlainCopy
    1. Injecting Applicationcontextutil instances into the business class
[Java]View PlainCopy
    1. <bean id="Worksigncheckjob" class="Com.service.tools.quartz.WorkSignCheckJob" >
    2. <property name="Appcontextutil" ref="Applicationcontextutil"/>
    3. </bean>

3) The business class calling code is as follows:

[Java]View PlainCopy
      1. ApplicationContext Appcontext=appcontextutil.getcontext ();
      2. Projectservicefacade projectservicefacade= (Projectservicefacade) Appcontext.getbean ("  Biz.projectservicefacade ");
      3. Worksignservice worksignservice= (Worksignservice) Appcontext.getbean ("Biz.worksignservice");

Considerations for new Classpathxmlapplicationcontext () in the program

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.