Spring IOC 源码

嗯嗯

java 复制代码
从AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); 这一行代码说起,我们用的AnnotationConfigApplicationContext这个类就继承GenericApplicationContext,而GenericApplicationContext (其注入了DefaultListableBeanFactory)继承 AbstractApplicationContext

public class AnnotationConfigApplicationContext extends GenericApplicationContext

DefaultListableBeanFactory 有一些核心方法, 比如  public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)


通过构造器注入DefaultListableBeanFactory
public class GenericApplicationContext extends AbstractApplicationContext implements BeanDefinitionRegistry {

	private final DefaultListableBeanFactory beanFactory;

	@Nullable
	private ResourceLoader resourceLoader;

	private boolean customClassLoader = false;

	private final AtomicBoolean refreshed = new AtomicBoolean();


	/**
	 * Create a new GenericApplicationContext.
	 * @see #registerBeanDefinition
	 * @see #refresh
	 */
	public GenericApplicationContext() {
		this.beanFactory = new DefaultListableBeanFactory();
	}

    @Override
	public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
			throws BeanDefinitionStoreException {

		this.beanFactory.registerBeanDefinition(beanName, beanDefinition);
	}

	@Override
	public void removeBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
		this.beanFactory.removeBeanDefinition(beanName);
	}

	@Override
	public BeanDefinition getBeanDefinition(String beanName) throws NoSuchBeanDefinitionException {
		return this.beanFactory.getBeanDefinition(beanName);
	}

   /**
	 * Register a bean from the given bean class, optionally providing explicit
	 * constructor arguments for consideration in the autowiring process.
	 * @param beanClass the class of the bean
	 * @param constructorArgs custom argument values to be fed into Spring's
	 * constructor resolution algorithm, resolving either all arguments or just
	 * specific ones, with the rest to be resolved through regular autowiring
	 * (may be {@code null} or empty)
	 * @since 5.2 (since 5.0 on the AnnotationConfigApplicationContext subclass)
	 */
	public <T> void registerBean(Class<T> beanClass, Object... constructorArgs) {
		registerBean(null, beanClass, constructorArgs);
	}

	/**
	 * Register a bean from the given bean class, optionally providing explicit
	 * constructor arguments for consideration in the autowiring process.
	 * @param beanName the name of the bean (may be {@code null})
	 * @param beanClass the class of the bean
	 * @param constructorArgs custom argument values to be fed into Spring's
	 * constructor resolution algorithm, resolving either all arguments or just
	 * specific ones, with the rest to be resolved through regular autowiring
	 * (may be {@code null} or empty)
	 * @since 5.2 (since 5.0 on the AnnotationConfigApplicationContext subclass)
	 */
	public <T> void registerBean(@Nullable String beanName, Class<T> beanClass, Object... constructorArgs) {
		registerBean(beanName, beanClass, (Supplier<T>) null,
				bd -> {
					for (Object arg : constructorArgs) {
						bd.getConstructorArgumentValues().addGenericArgumentValue(arg);
					}
				});
	}
相关推荐
特立独行的猫A5 小时前
DDS 调试助手:用仓颉写的跨厂商 DDS 抓包与监控工具(开源)
后端
码事漫谈6 小时前
OPC到底是什么:一年冒出1600万家,九成活不过2026年
后端
陆枫Larry6 小时前
从一个 Java Demo 聊聊 H2 数据库的前世今生
后端
特立独行的猫A6 小时前
仓颉版 Tauri 单文件 exe:gcc -r 一招破解 GCC-LTO 静态库链接
后端
金銀銅鐵6 小时前
带您看懂字节码指令(番外二):递归里有什么——三层 fact 帧怎么叠起来
java·jvm·后端
皮皮林5517 小时前
私活神器,腾讯低代码平台开源了,拖拽就能开发~
后端
两点王爷8 小时前
Java 面试高频:JWT、Cookie、Session、Token 鉴权详解
java·开发语言·面试
用户1129361121910 小时前
npm 都提示成功了,node_modules 为什么还占着 370M?
linux·前端·后端
悟道书生10 小时前
hashMap/hashSet/hashCode详解
java