Get spring ApplicationContext several ways to "go"

Source: Internet
Author: User

Transferred from: http://blog.sina.com.cn/s/blog_9c7ba64d0101evar.html

Java class gets the bean for the spring container

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

Method One: Save the ApplicationContext object at initialization time

Code:

1 New Filesystemxmlapplicationcontext ("Applicationcontext.xml"); 2 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:

1 Import org.springframework.web.context.support.WebApplicationContextUtils; 2 applicationcontext ac1 = Webapplicationcontextutils.getrequiredwebapplicationcontext (ServletContext SC); 3 ApplicationContext AC2 = webapplicationcontextutils.getwebapplicationcontext (ServletContext SC); 4 Ac1.getbean ("Beanid"); 5 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.

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 latter three methods that can be implemented in ordinary classes to inherit or implement the corresponding class or interface to obtain spring's ApplicationContext object, However, it is important to note that ordinary Java classes that implement these classes or interfaces must be configured in the spring configuration file Application-context.xml file. Otherwise, the obtained ApplicationContext object will be null.



Here is an example of the Applicationcontextaware interface I implemented

1  PackageQuartz.util;2 3 Importorg.springframework.beans.BeansException;4 ImportOrg.springframework.context.ApplicationContext;5 ImportOrg.springframework.context.ApplicationContextAware;6 7  Public classSpringconfigtoolImplementsapplicationcontextaware{//extends applicationobjectsupport{8 9 Private StaticApplicationContext context =NULL;Ten Private StaticSpringconfigtool stools =NULL; One  Public synchronized Staticspringconfigtool init () { A   if(Stools = =NULL){ -Stools =NewSpringconfigtool (); -   } the   returnstools; - } -  -  Public voidSetapplicationcontext (ApplicationContext applicationcontext) + throwsbeansexception { -Context =ApplicationContext; + } A  at  Public synchronized StaticObject Getbean (String beanname) { -   returnContext.getbean (beanname); - } -  -}

Configuration information in the XML file

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, if you need details to view the source code

1 Org.springframework.web.context.ContextLoader 2 3 Import  4import5  6 webapplicationcontext WAC =  7   Wac.getbean (Beanid);

Get spring ApplicationContext several ways to "go"

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.