Java Spring Fetch Bean method Summary _java

Source: Internet
Author: User

Spring is a lightweight control inversion (IoC) and aspect-oriented (AOP) container framework, how do you get a spring-configured bean in a program?

The Bean Factory (com.springframework.beans.factory.BeanFactory) is the core interface of the spring framework, providing a high-level IOC configuration mechanism. Beanfactory makes it possible to manage different types of Java objects, The application context (Com.springframework.context.ApplicationContext), based on Beanfactory, provides more application-oriented functionality, provides internationalization support and framework event systems, and facilitates the creation of practical applications. We generally call beanfactory the IOC container, while the ApplicationContext is called the application context. But sometimes, for the sake of convenience, we also call ApplicationContext the spring container.

For both purposes, we can make a simple distinction: beanfactory is the infrastructure of the spring framework, facing spring itself; ApplicationContext for developers using the spring framework, In almost all applications we use the ApplicationContext instead of the underlying beanfactory.

There is a significant difference between ApplicationContext initialization and Beanfactory: Beanfactory when the container is initialized, the bean is not instantiated until the first time a bean is accessed, the target bean is not instantiated , and ApplicationContext instantiates all single-instance beans when the application context is initialized. So ApplicationContext's initialization time is slightly longer than beanfactory.

This article does not cover the use of @Resource, @Autowired automatic injection, only through applicationcontext to get the beans in the sping configuration file.

To get the bean configured in the XML, the key is to get the Org.springframework.context.ApplicationContext

The first way to get ApplicationContext:

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.FileSystemXmlApplicationContext;
ApplicationContext ApplicationContext = new Filesystemxmlapplicationcontext ("Applicationcontext.xml");

Or

Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Private ApplicationContext ApplicationContext = new Classpathxmlapplicationcontext ("Applicationcontext.xml");

This is a time-consuming way to instantiate ApplicationContext, which applies to stand-alone applications that use the spring framework, and is only recommended when a program needs to manually initialize spring through a configuration file. The main implementation class for ApplicationContext is Classpathxmlapplicationcontext and filesystemxmlapplicationcontext, which loads the configuration file by default from the Classpath. The latter loads the configuration file from the file system by default

Example:

public class Beanmanager {
private static ApplicationContext context = new Classpathxmlapplicationcontext (" Appcontext.xml ");
public static Object Getbean (String beanid) {return
Context.getbean (Beanid);
}
}

Write a servlet in Web.xml, start automatically, call Beanmanager in the Init method

Init () throws Servletexception {
Beanmanager BM = new Beanmanager ()//optional to let spring load the bean configuration when the Web application is started.
//Otherwise it will be loaded at the first call to Beanmanager, affecting one speed.
}

Use Beanmanager.getbean (String beanid) in Java code; To get the bean instance.

The second way to get ApplicationContext is to get the ApplicationContext object from the tool class provided by spring, specifically for Web engineering, and recommend it for use in Web projects. For example:

ServletContext ServletContext = Request.getsession (). Getservletcontext ();
ApplicationContext AC1 = webapplicationcontextutils. Getrequiredwebapplicationcontext (ServletContext SC)
ApplicationContext AC2 = webapplicationcontextutils. Getwebapplicationcontext (ServletContext SC)
Ac1.getBean (" Beanid ");
Ac2.getbean ("Beanid");

Get to the ApplicationContext instance object through Javax.servlet.ServletContext, which means that you must use the request, session, and so on.

This way, you cannot set the ApplicationContext object as a member variable. In each concrete method, the request, session, etc. are acquired to ServletContext to obtain the ApplicationContext instance.

Therefore, this method is only recommended for use in a Web project that can get to a ServletContext object, and you do not need to define a ApplicationContext object as a member variable.

Note: When you use Webapplicationcontextutils to get the ApplicationContext instance, The Org.springframework.web.context.ContextLoaderListener listener needs to be added to the Web.xml configuration file, otherwise the ApplicationContext object is not obtained and NULL is returned.

Configuration file: Web.xml

<!--Contextloaderlistener automatically injected applicationcontext through
webapplicationcontextutils.getwebapplicationcontext ( Request.getsession (). Getservletcontext ()) gets-->
<!--spring configuration file load location-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/web-inf/spring/ appcontext.xml,/web-inf/spring/appinterceptor.xml</param-value>
</context-param>
< Listener>
<listener-class>org.springframework.web.context.contextloaderlistener</ Listener-class>
</listener>

3. Inherit from abstract class Applicationobjectsupport

Abstract class Applicationobjectsupport provides a Getapplicationcontext () method that can be easily obtained from ApplicationContext. When spring initializes, the ApplicationContext object is injected through the Setapplicationcontext (ApplicationContext context) method of the abstract class.

4. Inherit from abstract class Webapplicationobjectsupport

Using Getwebapplicationcontext () by inheriting Org.springframework.web.context.support.WebApplicationObjectSupport Get to Org.springframework.web.context.WebApplicationContext because Web applications have more features than general applications, Webapplicationcontext extends ApplicationContext 。 Webapplicationcontext defines a constant root_web_application_ context_attribute that, when the context is started, The Webapplicationcontext instance is placed in the ServletContext property list with this as the key, so we can get the webapplicationcontext from the Web container directly from the following statement:

Webapplicationcontext WAC = (webapplicationcontext) servletcontext.getattribute (
webapplicationcontext.root_ Web_application_context_attribute);

5. Implement Interface Applicationcontextaware

Implements the Setapplicationcontext (ApplicationContext context) method of the interface and saves the ApplicationContext object. When spring initializes, the ApplicationContext object is injected by this method.

The third chapters method requires that the class be configured in the Spring configuration file:

<!--assume that Applicationcontexttool is inheriting or implementing the specific implementation class--> <bean class= of third chapters methods
Com.twovv.utils.ApplicationContextTool "></bean>

Otherwise, the applicationcontext is not obtained, and Null is returned.

The above content for you to introduce the Java spring to get the Bean method Summary, I hope you like.

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.