Several ways to get webapplicationcontext,applicationcontext in spring

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 is possible to remove the Webapplicationcontext object directly from the ServletContext: 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:

public class AppContext {

private static AppContext instance;

Private Abstractapplicationcontext AppContext;

Public synchronized static AppContext getinstance () {
if (instance = = null) {
Instance = new AppContext ();
}
return instance;
}

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>

<listener>
<listener-class>com.ibatis.jpetstore.util.SpringInit</listener-class>
</listener>

Where Springinit implements interface Servletcontextlistener:

Package com.ibatis.jpetstore.util;

Import javax.servlet.ServletContextEvent;
Import Javax.servlet.ServletContextListener;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.web.context.WebApplicationContext;
Import Org.springframework.web.context.support.WebApplicationContextUtils;


public class Springinit implements Servletcontextlistener {


private static Webapplicationcontext Springcontext;

Public Springinit () {
Super ();
}

public void contextinitialized (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 gets ApplicationContext, code:

Public Orderbean () {
This
(Accountservice) Springinit.getapplicationcontext (). Getbean ("Accountservice"),
(OrderService) Springinit.getapplicationcontext (). Getbean ("OrderService"));
}

Several ways to get webapplicationcontext,applicationcontext in spring

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.