春のソースコード解析・ノート(A)

AnnotationConfigApplicationContext context=new AnnotationConfigApplicationContext(AppConfig.class);

それはavaConfig春のスタイル+春を使用するには、注釈の方法を推奨され、現在主流の方法です

ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext();

これは道のXMLファイルであり、あなたが理解して行うことができます
図の最初のを見て
ここに画像を挿入説明
、我々はAnnotationConfigApplicationContextに入った3つの方法がある場合には、わかります

/**
	 * Create a new AnnotationConfigApplicationContext, deriving bean definitions
	 * from the given component classes and automatically refreshing the context.
	 * @param componentClasses one or more component classes — for example,
	 * {@link Configuration @Configuration} classes
	 */
	public AnnotationConfigApplicationContext(Class<?>... componentClasses) {
		this();
		register(componentClasses);
		refresh();
	}

この方法:

  • 引数なしのコンストラクタ呼び出していない、最初のスーパークラスGenericApplicationContextのコンストラクタを呼び出します
    親クラスのコンストラクタはDefaultListableBeanFactoryが内部で初期化し、たBeanFactoryに割り当てられた
    、このクラスのコンストラクタ関数を、それが読者を初期化:AnnotatedBeanDefinitionReader読み取り、スキャナClassPathBeanDefinitionScannerスキャナ
    スキャナは非常に有用ではありません、それだけで私たちは外部の手で使用している.scan他の方法を起動され、従来の方法では、使用済みのスキャナオブジェクトではありません

メソッドを登録します。

  • 入ってくるクラスレジスタは、2例があり、
    伝統的な設定クラスに渡されていない
    通常誰が背後に見るんでしょうが(入ってくる豆を@Configurationと呼ばれる伝統的なコンフィギュレーション・クラスを持っている春知っているだろう@ConfigurationはLiteの設定クラスを呼ばずにFULLコンフィギュレーションクラスは、
    私たちがここで最初のタイプを取る、ノーと呼ばれる一般的な豆で、従来の構成の@Configurationクラスコンフィギュレーションとして記述しました

リフレッシュ方法:

  • リフレッシュ

私たちはこの時間をクリックすると、プロセスに入ります、以下は抜粋です

public class AnnotationConfigApplicationContext extends GenericApplicationContext implements AnnotationConfigRegistry {

    //注解bean定义读取器,主要作用是用来读取被注解的了bean
    private final AnnotatedBeanDefinitionReader reader;

    //扫描器,它仅仅是在我们外部手动调用 .scan 等方法才有用,常规方式是不会用到scanner对象的
    private final ClassPathBeanDefinitionScanner scanner;

    /**
     * Create a new AnnotationConfigApplicationContext that needs to be populated
     * through {@link #register} calls and then manually {@linkplain #refresh refreshed}.
     */
    public AnnotationConfigApplicationContext() {
        //会隐式调用父类的构造方法,初始化DefaultListableBeanFactory

        //初始化一个Bean读取器
        this.reader = new AnnotatedBeanDefinitionReader(this);

        //初始化一个扫描器,它仅仅是在我们外部手动调用 .scan 等方法才有用,常规方式是不会用到scanner对象的
        this.scanner = new ClassPathBeanDefinitionScanner(this);
    }
}

まず、スキャナと読者は、引数なしのコンストラクタがインスタンス化されるスキャナと読者にあり、読者の種類は、スキャナ文字通りそれは「ヒットと注釈付きBean定義リーダー」であることがわかる、AnnotatedBeanDefinitionReaderですタイプは、実際には、フィールドは重要ではありませんが、それはスキャンする渡す、コンストラクタ文字列のための私達のマニュアルコール.scan方法、または呼び出しパラメータでちょうどパッケージ名出ている、ClassPathBeanDefinitionScannerなるよう私たちの伝記として、使用されますコンフィギュレーションクラスは、このスキャナオブジェクトを使用するつもりはありません。

AnnotationConfigApplicationContextクラスは継承され、それは暗黙的に親クラスのコンストラクタを呼び出します。

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();
	}
}

DefaultListableBeanFactoryダイアグラム
ここに画像を挿入説明
何かをする春AnnotatedBeanDefinitionReader初期化時に見てみましょう:

/**
	 * Create a new {@code AnnotatedBeanDefinitionReader} for the given registry.
	 * <p>If the registry is {@link EnvironmentCapable}, e.g. is an {@code ApplicationContext},
	 * the {@link Environment} will be inherited, otherwise a new
	 * {@link StandardEnvironment} will be created and used.
	 * @param registry the {@code BeanFactory} to load bean definitions into,
	 * in the form of a {@code BeanDefinitionRegistry}
	 * @see #AnnotatedBeanDefinitionReader(BeanDefinitionRegistry, Environment)
	 * @see #setEnvironment(Environment)
	 */
	public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry) {
		this(registry, getOrCreateEnvironment(registry));
	}

ここBeanDefinitionRegistryは、当然のことながら、これをクリックして、彼らは直接、他のこのような構成のメソッドを呼び出すAnnotationConfigApplicationContextの一例であり、

/**
	 * Create a new {@code AnnotatedBeanDefinitionReader} for the given registry,
	 * using the given {@link Environment}.
	 * @param registry the {@code BeanFactory} to load bean definitions into,
	 * in the form of a {@code BeanDefinitionRegistry}
	 * @param environment the {@code Environment} to use when evaluating bean definition
	 * profiles.
	 * @since 3.1
	 */
	public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry, Environment environment) {
		Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
		Assert.notNull(environment, "Environment must not be null");
		this.registry = registry;
		this.conditionEvaluator = new ConditionEvaluator(registry, environment, null);
		AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
	}

下のその方法をクリックして、彼の役割は、私がセットに内部をインスタンス化し、その後セットを返していない場合は、複数の内蔵春の豆を注入することです

/**
	 * Register all relevant annotation post processors in the given registry.
	 * @param registry the registry to operate on
	 * @param source the configuration source element (already extracted)
	 * that this registration was triggered from. May be {@code null}.
	 * @return a Set of BeanDefinitionHolders, containing all bean definitions
	 * that have actually been registered by this call
	 */
	public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
			BeanDefinitionRegistry registry, @Nullable Object source) {

		DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
		if (beanFactory != null) {
			if (!(beanFactory.getDependencyComparator() instanceof AnnotationAwareOrderComparator)) {
				beanFactory.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
			}
			if (!(beanFactory.getAutowireCandidateResolver() instanceof ContextAnnotationAutowireCandidateResolver)) {
				beanFactory.setAutowireCandidateResolver(new ContextAnnotationAutowireCandidateResolver());
			}
		}

		Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(8);

		if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

		if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

		// Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
		if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

		// Check for JPA support, and if present add the PersistenceAnnotationBeanPostProcessor.
		if (jpaPresent && !registry.containsBeanDefinition(PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition();
			try {
				def.setBeanClass(ClassUtils.forName(PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME,
						AnnotationConfigUtils.class.getClassLoader()));
			}
			catch (ClassNotFoundException ex) {
				throw new IllegalStateException(
						"Cannot load optional framework class: " + PERSISTENCE_ANNOTATION_PROCESSOR_CLASS_NAME, ex);
			}
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, PERSISTENCE_ANNOTATION_PROCESSOR_BEAN_NAME));
		}

		if (!registry.containsBeanDefinition(EVENT_LISTENER_PROCESSOR_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(EventListenerMethodProcessor.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_PROCESSOR_BEAN_NAME));
		}

		if (!registry.containsBeanDefinition(EVENT_LISTENER_FACTORY_BEAN_NAME)) {
			RootBeanDefinition def = new RootBeanDefinition(DefaultEventListenerFactory.class);
			def.setSource(source);
			beanDefs.add(registerPostProcessor(registry, def, EVENT_LISTENER_FACTORY_BEAN_NAME));
		}

		return beanDefs;
	}

以下は、ブロック図のRootBeanDefinitionされる
ここに画像を挿入説明
次のステップはBeanDefinitionある
ここに画像を挿入説明
ここに画像を挿入説明
これらのクラスは、我々は実際に動作することを春に書いたものではありませんが、BeanDefinitionが、それは自然が多く含まれ、次のことを言います

公開された25元の記事 ウォン称賛22 ビュー3634

おすすめ

転載: blog.csdn.net/weixin_42443419/article/details/104152467