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);
					}
				});
	}
相关推荐
sunwenjian8866 小时前
Java进阶——IO 流
java·开发语言·python
sinat_255487816 小时前
读者、作家 Java集合学习笔记
java·笔记·学习
墨香幽梦客6 小时前
API集成技术规范:RESTful与GraphQL在企业系统对接中的应用对比
后端·restful·graphql
皮皮林5516 小时前
如何画出一张优秀的架构图?(老鸟必备)
java
百锦再6 小时前
Java 并发编程进阶,从线程池、锁、AQS 到并发容器与性能调优全解析
java·开发语言·jvm·spring·kafka·tomcat·maven
森林猿6 小时前
java-modbus-读取-modbus4j
java·网络·python
tobias.b6 小时前
计算机基础知识-数据结构
java·数据结构·考研
reembarkation7 小时前
光标在a-select,鼠标已经移出,下拉框跟随页面滚动
java·数据库·sql
愣头不青7 小时前
617.合并二叉树
java·算法
刀法如飞7 小时前
AI编程时代,为什么35岁以上程序员会更吃香?
人工智能·后端·ai编程