spring注解

AnnotationConfigApplicationContext

Java配置方式

@Test(expected = BeanCreationException.class)
public void testFbkMethod() {
	new AnnotationConfigApplicationContext(TestConfig1.class);
}

bean的注册

public class SimpleBeanDefinitionRegistry extends SimpleAliasRegistry implements BeanDefinitionRegistry {

	/** Map of bean definition objects, keyed by bean name. */
	private final Map<String, BeanDefinition> beanDefinitionMap = new ConcurrentHashMap<>(64);

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

		Assert.hasText(beanName, "'beanName' must not be empty");
		Assert.notNull(beanDefinition, "BeanDefinition must not be null");
		this.beanDefinitionMap.put(beanName, beanDefinition);
	}
}

spring多播器,事件监听

AnnotationConfigApplicationContext.class

public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
	this();
	register(annotatedClasses);
	refresh();
}

AbstractApplicationContext.refresh()

注册事件广播器

// Initialize event multicaster for this context.
initApplicationEventMulticaster();

ApplicationEventMulticaster.multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType)

通知观察者,进行事件播放

Interface to be implemented by objects that can manage a number of
{@link ApplicationListener} objects, and publish events to them.

@Override
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
	ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
	Executor executor = getTaskExecutor();
	for (ApplicationListener<?> listener : getApplicationListeners(event, type)) {
		if (executor != null) {
			executor.execute(() -> invokeListener(listener, event));
		}
		else {
			invokeListener(listener, event);
		}
	}
}
原文地址:https://www.cnblogs.com/zhuxiang1633/p/15074258.html