Java class to obtain the bean of the Spring container

Source: Internet
Author: User

Java class to obtain the bean of the Spring container

There are many ways to obtain beans in Spring. Here we will summarize:

First: Save the ApplicationContext object during initialization

ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");ac.getBean("beanId");
Note: This method is applicable to standalone applications using the Spring framework. You need to manually initialize Spring through the configuration file.
Method 2: 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");
Note:
1. These two methods are suitable for the B/S system using the Spring framework. The ApplicationContext object is obtained through the ServletContext object, and then the desired class instance is obtained through it;
2. The first method throws an exception when the acquisition fails, and the second method returns null.

Third: inherit from the abstract class ApplicationObjectSupport

Note: You can use the getApplicationContext () method provided by the abstract class ApplicationObjectSupport to conveniently obtain the ApplicationContext instance and then obtain the bean in the Spring container. During Spring initialization, The ApplicationContext object is injected through the setApplicationContext (ApplicationContext context) method of this abstract class.

Type 4: inherit from the abstract class WebApplicationObjectSupport

Note: similar to the preceding method, call getWebApplicationContext () to obtain the WebApplicationContext instance;

Fifth: implement the ApplicationContextAware Interface

Note: implement the setApplicationContext (ApplicationContext context) method of this interface and save the ApplicationContext object. During Spring initialization, The ApplicationContext object is injected using this method.

Although Spring provides the last three methods to inherit from common classes or implement corresponding classes or interfaces to obtain Spring ApplicationContext objects, however, when using java classes that inherit or implement these abstract classes or interfaces, you must configure them in the Spring configuration file (that is, the application-context.xml file, otherwise, the obtained ApplicationContext object will be null.

The following shows how to obtain the bean in the Spring container by implementing the ApplicationContextAware interface:

First, define a class that implements the ApplicationContextAware interface, and implement the following methods:

 

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);}}
Configure the applicationContext. xml file:
 
Finally, you can get the corresponding bean in the Spring container through the following code:
SpringConfigTool.getBean("beanId");
Note that the Spring container cannot be obtained using the following methods when the server starts Spring container initialization:
import org.springframework.web.context.ContextLoader; import org.springframework.web.context.WebApplicationContext;  WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); wac.getBean(beanID);

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.