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);
					}
				});
	}
相关推荐
ALex_zry2 分钟前
CMake变量传递与宏定义技术详解:从问题到解决方案
开发语言·spring·cmake·条件编译
lang201509282 分钟前
彻底理解CountDownLatch
java·开发语言
he___H17 分钟前
关于Amazon S3; Status Code: 403; Error Code: 403 Forbidden问题
java·报错·minio
山沐与山20 分钟前
【MQ】MQ消息队列幂等性设计与踩坑实战
java·开发语言·数据库·rocketmq
xiaowu08034 分钟前
C# 多返回值写法
java·前端·c#
XiaoYu200238 分钟前
第7章 Prisma入门
javascript·后端
银迢迢38 分钟前
springboot的拦截器配置不拦截路径没有生效
java·后端·spring
superman超哥39 分钟前
Rust 引用的作用域与Non-Lexical Lifetimes(NLL):生命周期的精确革命
开发语言·后端·rust·生命周期·编程语言·rust引用的作用域·rust nll
阿里巴巴P8资深技术专家1 小时前
2025 年度总结:在坚持与突破中前行
java·vue.js·人工智能·年终总结·2025
独自破碎E1 小时前
Spring Bean一共有几种作用域?
java·后端·spring