Spring get Webapplicationcontext,applicationcontext several ways to explain (go)

Source: Internet
Author: User

Method One: Save the ApplicationContext object at initialization time
Code:
ApplicationContext ac = new Filesystemxmlapplicationcontext ("Applicationcontext.xml");
Ac.getbean ("Beanid");
Description: This approach applies to standalone applications that use the spring framework, requiring the program to manually initialize the spring with a configuration file.

Method Two: Get the ApplicationContext object from the tool class provided by spring
Code:
Import Org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext AC1 = webapplicationcontextutils.getrequiredwebapplicationcontext (ServletContext SC);
ApplicationContext AC2 = webapplicationcontextutils.getwebapplicationcontext (ServletContext SC);
Ac1.getbean ("Beanid");
Ac2.getbean ("Beanid");
Description
This method is suitable for the B/s system with spring Framework, obtains the ApplicationContext object through the ServletContext object, and obtains the required class instance through it.

The difference between the above two tool methods is that the former throws an exception when it gets failed, and the latter returns null.

Where ServletContext SC can be specifically replaced by Servlet.getservletcontext () or This.getservletcontext () or request.getsession (). Getservletcontext (); In addition, since spring is an injected object placed in the ServletContext, it can be removed directly from the ServletContext Webapplicationcontext Object: Webapplicationcontext Webapplicationcontext = (webapplicationcontext) Servletcontext.getattribute ( Webapplicationcontext.root_web_application_context_attribute);

Method Three: Inherit from abstract class Applicationobjectsupport
Description: Abstract class Applicationobjectsupport provides the Getapplicationcontext () method, which can be easily obtained to ApplicationContext.
When spring initializes, the ApplicationContext object is injected through the Setapplicationcontext (ApplicationContext context) method of the abstract class.

Method Four: Inherit from abstract class Webapplicationobjectsupport
Description: Similar to the above method, call Getwebapplicationcontext () to get Webapplicationcontext

Method Five: Implement Interface Applicationcontextaware
Description: Implements the Setapplicationcontext (ApplicationContext context) method of the interface and saves the ApplicationContext object.
When spring initializes, the ApplicationContext object is injected through this method.


In Web applications, it is common to load webapplication with Contextloaderlistener, and if you need to get WebApplication out of action or outside of the control class, Write a class separately in the static variable,
Similar to:

PublicClassAppContext {

PrivateStaticAppContext instance;

PrivateAbstractapplicationcontext AppContext;

PublicSynchronizedStaticAppContext getinstance () {
If(Instance==Null) {
Instance=NewAppContext ();
}
ReturnInstance
}

private appcontext ()  {
     this.appcontext = new  Classpathxmlapplicationcontext (
         "/applicationcontext.xml ");
  }

  public  Abstractapplicationcontext getappcontext ()  {
     return appcontext;
  }
}


However, it is loaded 2 times applicationcontext,servlet once, the path is loaded once, it is better to use the path to load, the servlet load
On the internet also found some other statements: Implement Applicationcontextaware,,, interface, or Servletcontextaware interface, also write configuration file. Some should put the configuration file in the listener, replaced by their own class, so pure superfluous. But some applications are not replacements, they are a listener,
I found this in a version of Jpetstore (the specific version does not know):
[Web. xml]

<Listener>
<Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class>
</listener>< Span style= "color: #000000;" >
    
    < listener>
        < listener-class> Com.ibatis.jpetstore.util.springinit</ Listener-class>
    < Span style= "color: #0000ff;" ></listener>

Where Springinit implements interface Servletcontextlistener:

PackageCom.ibatis.jpetstore.util;

ImportJavax.servlet.ServletContextEvent;
ImportJavax.servlet.ServletContextListener;
ImportOrg.springframework.context.ApplicationContext;
ImportOrg.springframework.web.context.WebApplicationContext;
ImportOrg.springframework.web.context.support.WebApplicationContextUtils;


PublicClassSpringinitImplementsServletcontextlistener {


PrivateStaticWebapplicationcontext Springcontext;

PublicSpringinit () {
Super();
}

PublicvoidContextinitialized (Servletcontextevent event) {
Springcontext= webapplicationcontextutils.getwebapplicationcontext ( Event.getservletcontext ());
    }
    

    public void contextdestroyed ( servletcontextevent event)  {
    }
    
     public  static applicationcontext  Getapplicationcontext ()  {
         return springcontext;
    }

    
}


in one of the Bean's constructs springinit get applic Ationcontext, code:

  public  orderbean ()  {
    this              ( Accountservice)  springinit.getapplicationcontext (). Getbean ( "accountservice "),
              (OrderService)  springinit.getapplicationcontext (). Getbean ( "orderservice" )  );
  }


Well, the way to get applicationcontext in a bean outside of Action,servlet is worth referencing and should be useful.

Transferred from: http://www.blogjava.net/Todd/archive/2009/09/15/295112.html

Related Article

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.