Hand-written spring instantiation bean source code, implementing the Object getBean(String beanId) method through the reflection mechanism

The handwritten spring instantiation bean source code only implements the Object getBean(String beanId) method. That is to achieve: ApplicationContext applicationContext = new ClassPathXmlApplicationContext(“xxx”); Object o = applicationContext.getBean(“xxx”); //Define ApplicationContext interface: public interface ApplicationContext {<!– –> Object getBean(String name); } // ClassPathXmlApplicationContext class /** * Parse xml files, read tags, and instantiate objects * @param springPath […]

Spring source code series: dependency injection (1) getBean

In the Spring source code series: BeanFactory creation article, we talked about the BeanFactory container, which provides the injection implementation interface. Its specific implementation also needs to be seen from AbstractBeanFactory and DefaultListableBeanFactory. Today, let’s take a look at the getBean method in the AbstractBeanFactory class. 1. getBean method getBean provides four overloaded methods, as […]

Obtain all implementation classes and execution methods under an interface through applicationContext.getBeansOfType or obtain specific implementation class objects and implementation class methods

Prerequisites: The current class must first obtain the applicationContext object of spring (the environment needs to be a springframework framework), and then execute applicationContext.getBeansOfType when the project starts to obtain all the implementation classes under our target interface, and then perform our subsequent operations. Let’s start using it, first create an entity class for the […]

[Solved] SpringBoot – Get the getBean method and solve the problem of ApplicationContext null pointer

1. Reason 1: lack of ApplicationContext tool class package com.performancetest.common.utils; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import java.util.Map; /** * Spring Context tool class */ @Component public class SpringContextUtils implements ApplicationContextAware { public static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtils.applicationContext = applicationContext; } public static Object getBean(String name) […]