Home  >  Article  >  Java  >  5 ways for Java classes to obtain beans in Spring

5 ways for Java classes to obtain beans in Spring

高洛峰
高洛峰Original
2017-01-23 11:00:241716browse

There are many ways to obtain beans in Spring. To summarize again:
The first way: Save the ApplicationContext object during initialization

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

Note: This method is suitable for independent applications using the Spring framework. Program requires the program to manually initialize Spring through the configuration file.
Second: Obtain the ApplicationContext object through the tool 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 systems using the Spring framework. Obtain the ApplicationContext through the ServletContext object. Object, and then obtain the required class instance through it;
2. The first method throws an exception when the acquisition fails, and the second method returns null.
Third type: Inherited from the abstract class ApplicationObjectSupport

Description: The ApplicationContext instance can be easily obtained through the getApplicationContext() method provided by the abstract class ApplicationObjectSupport, and then the beans in the Spring container can be obtained. When Spring is initialized, the ApplicationContext object will be injected through the setApplicationContext(ApplicationContext context) method of the abstract class.
Fourth method: Inherited from the abstract class WebApplicationObjectSupport

Description: Similar to the above method, obtain the WebApplicationContext instance by calling getWebApplicationContext();
Fifth method: Implement the interface ApplicationContextAware

Description: Implement the setApplicationContext(ApplicationContext context) method of this interface and save the ApplicationContext object. When Spring is initialized, the ApplicationContext object will be injected through this method.

Although Spring provides the last three methods to inherit or implement the corresponding class or interface in ordinary classes to obtain Spring's ApplicationContext object, you must pay attention to inheriting or implementing these abstract classes or interfaces when using them. The ordinary java class of the interface must be configured in the Spring configuration file (that is, the application-context.xml file), otherwise the obtained ApplicationContext object will be null.

The following demonstrates how to obtain beans in the Spring container by implementing the interface ApplicationContextAware:
First customize a class that implements the ApplicationContextAware interface and implement the methods inside:

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);
 }
}


Next, configure it in the applicationContext.xml file:

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

Finally, you can get the corresponding bean in the Spring container through the following code:

SpringConfigTool.getBean("beanId");

Note that in When the server starts the Spring container initialization, the Spring container cannot be obtained through the following method:

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 it will be helpful to everyone's learning.

For more related articles on 5 ways for Java classes to obtain beans in Spring, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn