Summary of Spring's methods for getting bean in code

  • 2020-05-12 02:41:04
  • OfStack

1. ContextLoader via Spring


WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext();
wac.getBean(beanID);

This approach does not rely on servlet and does not require an injection approach. One thing to note, however, is that when the Spring container is initialized at server startup, you cannot get the Spring container in this way

2. Implement interface ApplicationContextAware

Defining tool classes


public class SpringContextUtil implements ApplicationContextAware {
  private static ApplicationContext applicationContext;   //Spring Application context 

  /**
   *  implementation ApplicationContextAware The callback method of the interface to set the context 
   * @param applicationContext
   * @throws BeansException
   */
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    SpringContextUtil.applicationContext = applicationContext;
  }

  /**
   * @return ApplicationContext
   */
  public static ApplicationContext getApplicationContext() {
    return applicationContext;
  }

  /**
   *  Access to the object 
   * @param name
   * @return Object 1 Registered under the given name bean An instance of the 
   * @throws BeansException
   */
  public static Object getBean(String name) throws BeansException {
    return applicationContext.getBean(name);
  }

  /**
   *  Get type is requiredType The object of 
   *  if bean Unable to be cast, the corresponding exception will be thrown ( BeanNotOfRequiredTypeException ) 
   * @param name    bean Registered name 
   * @param requiredType  Return object type 
   * @return Object  return requiredType Type of object 
   * @throws BeansException
   */
  public static Object getBean(String name, Class requiredType) throws BeansException {
    return applicationContext.getBean(name, requiredType);
  }

  /**
   *  if BeanFactory contains 1 Matches the given name bean Define, and return true
   * @param name
   * @return boolean
   */
  public static boolean containsBean(String name) {
    return applicationContext.containsBean(name);
  }

  /**
   *  Determine which is registered with the given name bean Is defined as 1 a singleton or 1 a prototype . 
   *  If it corresponds to a given name bean The definition was not found and will be thrown 1 An exception ( NoSuchBeanDefinitionException ) 
   * @param name
   * @return boolean
   * @throws NoSuchBeanDefinitionException
   */
  public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
    return applicationContext.isSingleton(name);
  }

  /**
   * @param name
   * @return Class  The type of the registered object 
   * @throws NoSuchBeanDefinitionException
   */
  public static Class getType(String name) throws NoSuchBeanDefinitionException {
    return applicationContext.getType(name);
  }

  /**
   *  If given bean Name in bean If there are aliases in the definition, return them 
   * @param name
   * @return
   * @throws NoSuchBeanDefinitionException
   */
  public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
    return applicationContext.getAliases(name);
  }
}

3. The configuration bean


<!-- SpringContextUtil  Get by code bean -->
<bean id="SpringContextUtil " class="org.shaofan.demo.utils.SpringContextUtil"/>

conclusion

The above are several ways for Spring to obtain bean in the code. I hope the content of this article can be helpful to your study or work. If you have any questions, you can leave a message to communicate.


Related articles: