In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "the way Spring obtains Bean". In the daily operation, I believe that many people have doubts about the way Spring obtains Bean. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how Spring obtains Bean". Next, please follow the editor to study!
Save the ApplicationContext object during initialization
ApplicationContext ac = new FileSystemXmlApplicationContext ("applicationContext.xml"); ac.getBean ("userService"); # e.g.:
Get the ApplicationContext object through the utils class provided by Spring
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext (ServletContext sc); ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext (ServletContext sc); ac1.getBean ("beanId"); ac2.getBean ("beanId"); note: this method is suitable for using the Spring framework of the beanId S system, through the ServletContext object to get ApplicationContext objects. Then get the required class instance through it. The difference between the above two tools is that the former throws an exception when the fetch fails. The latter returns null.
Inherited from the abstract class ApplicationObjectSupport
Description: abstract class ApplicationObjectSupport provides getApplicationContext () method, which can easily get ApplicationContext. When Spring is initialized, the ApplicationContext object is injected through the setApplicationContext (ApplicationContext context) method of the abstract class.
Inherited from the abstract class WebApplicationObjectSupport
Description: similar to the above method. Call getWebApplicationContext () to get WebApplicationContext
Implement interface ApplicationContextAware
Description: implement the setApplicationContext (ApplicationContext context) method of the interface and save the ApplicationContext object. When Spring is initialized, the ApplicationContext object is injected through this method.
Package com.rkhd.ienterprise.isearch.util;import org.apache.commons.lang3.Validate;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.BeansException;import org.springframework.beans.factory.DisposableBean;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component;/** * saves the Spring ApplicationContext as a static variable, and you can retrieve the ApplicaitonContext at any time anywhere in any code. * * / @ Componentpublic class SpringContextHolder implements ApplicationContextAware, DisposableBean {private static ApplicationContext applicationContext = null; private static Logger logger = LoggerFactory.getLogger (SpringContextHolder.class); / * get the ApplicationContext stored in the static variable. * / public static ApplicationContext getApplicationContext () {assertContextInjected (); return applicationContext;} / * takes Bean from the static variable applicationContext and automatically transforms it to the type of the assigned object. * / @ SuppressWarnings ("unchecked") public static T getBean (String name) {assertContextInjected (); return (T) applicationContext.getBean (name);} / * * get Bean from the static variable applicationContext and automatically transform it to the type of the assigned object. * / public static T getBean (Class requiredType) {assertContextInjected (); return applicationContext.getBean (requiredType);} / * * clear that ApplicationContext in SpringContextHolder is Null. * / public static void clearHolder () {logger.debug ("clear ApplicationContext:" + applicationContext in SpringContextHolder); applicationContext = null;} / * * implement the ApplicationContextAware interface and inject Context into the static variable. * @ param applicationContext * @ throws BeansException * / @ Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {logger.debug ("inject ApplicationContext into SpringContextHolder: {}", applicationContext); if (SpringContextHolder.applicationContext! = null) {logger.warn ("ApplicationContext in SpringContextHolder is overwritten, the original ApplicationContext is:" + SpringContextHolder.applicationContext);} SpringContextHolder.applicationContext = applicationContext / / NOSONAR} / * implements the DisposableBean interface to clean up static variables when Context is turned off. * @ throws Exception * / @ Override public void destroy () throws Exception {} / * check that ApplicationContext is not empty. * / private static void assertContextInjected () {Validate.validState (applicationContext! = null, "applicaitonContext attribute is not injected, please define SpringContextHolder.") in applicationContext.xml;}}
ContextLoader provided through Spring
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext (); wac.getBean (beanID); at this point, the study on "how Spring acquires Bean" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.