Summary of Spring fetch bean methods in Java

  • 2020-04-01 04:36:30
  • OfStack

Spring is a lightweight inversion of control (IoC) and aspect-oriented (AOP) container framework. How do you get spring-configured beans in your program?

Bean plant (com) springframework. Beans. Factory. The BeanFactory) is the core of the Spring framework interface, it provides a senior IoC configuration mechanism. The BeanFactory made it possible to management of different types of Java objects, the application context (com) springframework) context. The ApplicationContext) based on the BeanFactory, provides more geared to the needs of the application of the function, it provides the international support and system framework events, easier to create practical application. We generally call the BeanFactory the IoC container and the ApplicationContext the ApplicationContext. But sometimes for ease of writing, we also call ApplicationContext the Spring container.

For both purposes, we can make a simple distinction: the BeanFactory is the infrastructure of the Spring framework, oriented toward Spring itself; ApplicationContext is for developers using the Spring framework, and in almost all applications we use the ApplicationContext directly instead of the underlying BeanFactory.

There is one important difference between the initialization of ApplicationContext and the BeanFactory: the BeanFactory does not instantiate the Bean when it initializes the container and does not instantiate the target Bean until the first time a Bean is accessed. ApplicationContext, on the other hand, instantiates all singleton beans when the ApplicationContext is initialized. So the initialization time of ApplicationContext is a little bit longer than that of the BeanFactory

This article does not cover automatic injection via @resource, @autowired, and only beans in the Sping configuration file are obtained through the ApplicationContext.

To get the XML configuration Bean, the key is to obtain org. Springframework. Context. The ApplicationContext

The first method 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");

Instantiating the applicationContext in this way is time-consuming and is suitable for stand-alone applications using the Spring framework, and is recommended only in cases where the program needs to manually initialize Spring through a configuration file. ApplicationContext ClassPathXmlApplicationContext and FileSystemXmlApplicationContext, are the main implementation class of the former is the default from the classpath loading configuration file, the latter from the file system to load the configuration file 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, launch it automatically, and call the BeanManager in your init method


init() throws ServletException {
BeanManager bm = new BeanManager();//Optionally, you want spring to load the bean configuration when the web application starts.
//Otherwise it will be loaded on the first call to the BeanManager, affecting the speed of the first call.
}

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 through the spring-provided tool class, a method customized for web projects and recommended for web projects. Such as:


ServletContext servletContext = request.getSession().getServletContext();
ApplicationContext ac1 = WebApplicationContextUtils .getRequiredWebApplicationContext(ServletContext sc)
ApplicationContext ac2 = WebApplicationContextUtils .getWebApplicationContext(ServletContext sc)
ac1.getBean("beanId");
ac2.getBean("beanId");

Through javax.mail. Servlet. ServletContext get ApplicationContext instance objects, this means that you must use to request, session and so on.

This way, you cannot set the ApplicationContext object as a member variable. You need to get the ServletContext through request, session, and so on in each specific method and then get the ApplicationContext instance.

Therefore, this method is recommended only in Web projects where the ServletContext object can be obtained, and there is no need to define the ApplicationContext object as a member variable.

Note: when using WebApplicationContextUtils to obtain ApplicationContext instance, need on the web. The XML configuration files to add org. Springframework. Web. Context. The ContextLoaderListener listener, otherwise can't obtain ApplicationContext object, returns Null.

Configuration file: web.xml


<!--ContextLoaderListener Automatic injection  applicationContext Through the 
WebApplicationContextUtils.getWebApplicationContext(request.getSession().getServletContext()) To obtain  -->
<!--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 the abstract class ApplicationObjectSupport

The abstract ApplicationObjectSupport class provides the getApplicationContext() method, which can be easily obtained to the ApplicationContext. When Spring initializes, the ApplicationContext object is injected through the setApplicationContext(ApplicationContext) method of the abstract class.

4. WebApplicationObjectSupport inherited from the abstract class

Through inheritance org. Springframework. Web. Context. Support. Use WebApplicationObjectSupport getWebApplicationContext () get to org. Springframework. Web. Context. WebApplicationContext because web application has more features than the average, So the WebApplicationContext extends the ApplicationContext. The WebApplicationContext defines a constant ROOT_WEB_APPLICATION_ CONTEXT_ATTRIBUTE. When the context starts, the WebApplicationContext instance is placed in the property list of ServletContext with this key, so we can directly obtain the WebApplicationContext from the Web container by the following statement:


WebApplicationContext wac = (WebApplicationContext)servletContext.getAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

5. Implement interface ApplicationContextAware

Implement the setApplicationContext(ApplicationContext) method for the interface and save the ApplicationContext object. When Spring initializes, the ApplicationContext object is injected using this method.

The third, fourth, and fifth methods all require configuring the class in a Spring configuration file:


<!-- Assume that ApplicationContextTool Concrete implementation classes that inherit or implement the third, fourth, and fifth methods -->
<bean class="com.twovv.utils.ApplicationContextTool"></bean>

Otherwise you will not get the ApplicationContext and return Null.

The above content introduced the Java Spring bean method summary, hope you like it.


Related articles: