The Java class gets bean in Spring in five ways

  • 2020-05-07 19:35:51
  • OfStack

There are many ways to get bean in Spring.
type 1: save the ApplicationContext object at initialization time


ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");

Note: this approach is suitable for stand-alone applications using the Spring framework, requiring the program to manually initialize Spring via a configuration file.
type 2: gets ApplicationContext object through the utility class provided by Spring


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:
1. These two methods are suitable for B/S system with Spring framework, which obtains ApplicationContext object through ServletContext object, and then obtains the required class instance through it;
2. The first method throws an exception when the fetch fails, and the second method returns null.
type 3: inherits from the abstract class ApplicationObjectSupport

Note: the getApplicationContext() method provided by the abstract class ApplicationObjectSupport can be easily obtained to the ApplicationContext instance, and then to bean in the Spring container. When Spring is initialized, the ApplicationContext object is injected through the setApplicationContext(ApplicationContext context) method of the abstract class.
type 4: inherits from the abstract class WebApplicationObjectSupport

Note: similar to the above method, get an WebApplicationContext instance by calling getWebApplicationContext();
type 5: implementation interface ApplicationContextAware

Note: implement the setApplicationContext(ApplicationContext context) method of the interface and save the ApplicationContext object. When the Spring is initialized, the ApplicationContext object is injected through this method.

After although Spring provides three methods can be implemented in ordinary class inheritance or implement the corresponding class or interface for Spring ApplicationContext object, but in the use of 1 need to pay attention to the inheritance, or the abstract class or interface of ordinary java class 1 need to Spring configuration files (i.e. application - context. xml file) in the configuration, or obtain for null ApplicationContext object.

Here's how to get bean in Spring container by implementing the interface ApplicationContextAware:
First, customize a class that implements the ApplicationContextAware interface, and implement the methods in it:


package com.ghj.tool;

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 ac = null;
 private static SpringConfigTool springConfigTool = null;

 public synchronized static SpringConfigTool init() {
 if (springConfigTool == null) {
  springConfigTool = new SpringConfigTool();
 }
 return springConfigTool;
 }

 public void setApplicationContext(ApplicationContext applicationContext)throws BeansException {
 ac = applicationContext;
 }

 public synchronized static Object getBean(String beanName) {
 return ac.getBean(beanName);
 }
}

Secondly, configuration is carried out in applicationContext.xml file:

<bean id="SpringConfigTool" class="com.ghj.tool.SpringConfigTool"/>

Finally, the corresponding bean in Spring container can be obtained through the following code:
SpringConfigTool.getBean("beanId");

Note 1: when the server starts the Spring container initialization, the Spring container cannot be obtained by:


import org.springframework.web.context.ContextLoader; 
import org.springframework.web.context.WebApplicationContext; 
 
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); 
wac.getBean(beanID);

The above is the entire content of this article, I hope to help you with your study.


Related articles: