Five ways to get the beans in a spring container

Source: Internet
Author: User

Spring the applicationcontexts can be limited to different scopes. In the web framework, each dispatcherservlet has its own webapplicationcontext, which contains the beans required for the Dispatcherservlet configuration. The default beanfactory used by Dispatcherservlet is xmlbeanfactory, and Dispatcherservlet will look for it in the Web-inf directory of your web app when it is initialized [ Servlet-name]-servlet.xml file . The default values used by Dispatcherservlet can be modified using the servlet initialization parameter.

webapplicationcontext is just a common applicationcontext that has the necessary functionality for Web applications. It differs from a standard applicationcontext in that it is able to parse the subject and that it knows to associate with that servlet (by connecting to the ServletContext). Webapplicationcontext is tied to ServletContext, and you can use Requestcontextutils to find webapplicationcontext when you need it.

Spring Dispatcherservlet has a special set of beans that are used to process requests and display the corresponding views. These beans are contained within the spring framework and can optionally be configured in Webapplicationcontext as if they were configured with other beans. Each of these beans is described in detail below. We'll mention them later, but it's just to let you know they exist so that we can continue to discuss dispatcherservlet. For most beans, default values are provided, all of which you don't need to worry about.


A bit from the network

ServletContext is a great environment for Web applications that store objects at the level of the entire Web application and do not know if that is true.

Applicationcontext,webapplicationcontext is spring's beanfactory, from the name can know the difference pull, one is to support the web features of the beanfactory.

Spring acquires several methods of Webapplicationcontext and ApplicationContext:

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

As follows:

1.

public class Springcontextutil implements applicationcontextaware{

private static ApplicationContext applicationcontext;//spring application Context
@Override
public void Setapplicationcontext (ApplicationContext applicationcontext)
Throws Beansexception {
TODO auto-generated Method Stub
Springcontextutil.applicationcontext=applicationcontext;
}
public static Object Getbean (String name) {
return Applicationcontext.getbean (name);
}
}

Applicationcontext.xml Configuration
<bean id= "Springcontextutil" class= "Com.zjd.util.SpringContextUtil"/>

Springcontextutil.getbean ("Bean id");

2.

Webapplicationcontext Ctx=webapplicationcontextutils.getwebapplicationcontext (ServletContext);

Ctx.getbean ("Bean id");

Excerpt from: http://my.oschina.net/zjds/blog/371962

Five ways to get the beans in a spring container

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.