Get ApplicationContext in Spring

Source: Internet
Author: User

5 Common ways to get the beans in spring are summarized:

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.

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.

Although spring provides the following three methods that can be implemented in ordinary classes to inherit or implement the corresponding class or interface to obtain the spring ApplicationContext object, but in use it is important to note that the ordinary Java classes that implement these classes or interfaces must be in the spring Configuration file in the Application-context.xml file. Otherwise, the obtained ApplicationContext object will be null.

Here is an example of the Applicationcontextaware interface I implemented

Package quartz.util;

Import org.springframework.beans.BeansException;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.ApplicationContextAware;

public class Springconfigtool implements Applicationcontextaware{//extends applicationobjectsupport{

private static ApplicationContext context = NULL;
private static springconfigtool stools = null;
Public synchronized Static Springconfigtool init () {
if (stools = = null) {
Stools = new Springconfigtool ();
}
return stools;
}

public void Setapplicationcontext (ApplicationContext applicationcontext)
Throws Beansexception {
context = ApplicationContext;
}

Public synchronized static Object Getbean (String beanname) {
Return Context.getbean (Beanname);
}

}

Configuration information in the XML file

<bean id= "Springconfigtool" class= "Quartz.util.SpringConfigTool" ></bean>

Finally, provide a way to not rely on a servlet and not inject
Note that when the server is started, the spring container cannot be initialized with the following methods to get the spring container, as the details can be viewed in source Org.springframework.web.context.ContextLoader

Title1 import org.springframework.web.context.ContextLoader;
2 Import Org.springframework.web.context.WebApplicationContext;
3
4 Webapplicationcontext WAC = Contextloader.getcurrentwebapplicationcontext ();
5 Wac.getbean (Beanid);

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.