[Turn]spring get webapplicationcontext,applicationcontext several methods of explanation

Source: Internet
Author: User

Method One: Save the ApplicationContext object code at initialization: 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 code from the tool class provided by spring: 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 using the spring framework of the B/s system, through the ServletContext object to obtain the ApplicationContext object, and then through it to obtain the required class instance.

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 Getapplicationcontext () method, Can be easily obtained to the 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 () get Webapplicationcontext

Method Five: Implement Interface Applicationcontextaware Description: Implementation of the Interface Setapplicationcontext (ApplicationContext context) method, and save 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 the following:

Public classAppContext {
Private StaticAppContext instance;
PrivateAbstractapplicationcontext AppContext;
Public synchronized StaticAppContext getinstance () {if(Instance== NULL) {instance= NewAppContext (); }     returninstance; }
PrivateAppContext () { This. AppContext= NewClasspathxmlapplicationcontext ("/applicationcontext.xml"); }
PublicAbstractapplicationcontext Getappcontext () {returnAppContext; } }

However, it is loaded 2 times applicationcontext,servlet once, the path is loaded once, it is not as good as directly with the path to load, drop the servlet loaded on the internet also found some other claims: to achieve Applicationcontextaware,,, interface, or ServletContextaware interface, also write the configuration file. Some should put the configuration file in the listener, replaced by their own class, so pure superfluous. However, some applications are not replacement, is to fill a listener, I in a version of the Jpetstore (the specific version does not know) found this: [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 :

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;
Public classSpringinitImplementsServletcontextlistener {
Private StaticWebapplicationcontext Springcontext; PublicSpringinit () {Super(); }           Public voidcontextinitialized (Servletcontextevent event) {Springcontext=Webapplicationcontextutils.getwebapplicationcontext (Event.getservletcontext ()); }
Public voidcontextdestroyed (Servletcontextevent event) {} Public StaticApplicationContext Getapplicationcontext () {returnSpringcontext; }
}

In one of the Bean's constructs springinit gets ApplicationContext, code:

   PublicOrderbean () { 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.

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.