AbstractApplicationContext 之 prepareBeanFactory

在  AbstractApplicationContext  refresh方法中会调用prepareBeanFactory方法进行容器的特殊属性处理

protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) {
		// Tell the internal bean factory to use the context's class loader etc.
		beanFactory.setBeanClassLoader(getClassLoader());
		beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver());
//注册默认属性编辑器
		beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this));

		
ApplicationContextAwareProcessor实现了BeanPostProcessor接口对于bean实现了
EmbeddedValueResolverAware ,ResourceLoaderAware ,ApplicationEventPublisherAware ,MessageSourceAware 
ApplicationContextAware等接口就通过反射进行赋值。
		beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this));
		beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class);
		beanFactory.ignoreDependencyInterface(MessageSourceAware.class);
		beanFactory.ignoreDependencyInterface(ApplicationContextAware.class);

		// BeanFactory interface not registered as resolvable type in a plain factory.
		// MessageSource registered (and found for autowiring) as a bean.
		beanFactory.registerResolvableDependency(BeanFactory.class, beanFactory);
		beanFactory.registerResolvableDependency(ResourceLoader.class, this);
		beanFactory.registerResolvableDependency(ApplicationEventPublisher.class, this);
		beanFactory.registerResolvableDependency(ApplicationContext.class, this);

		// Detect a LoadTimeWeaver and prepare for weaving, if found.
		if (beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) {
			beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory));
			// Set a temporary ClassLoader for type matching.
			beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader()));
		}

		// Register default environment beans.
		if (!beanFactory.containsBean(SYSTEM_PROPERTIES_BEAN_NAME)) {
			Map systemProperties;
			try {
				systemProperties = System.getProperties();
			}
			catch (AccessControlException ex) {
				systemProperties = new ReadOnlySystemAttributesMap() {
					@Override
					protected String getSystemAttribute(String propertyName) {
						try {
							return System.getProperty(propertyName);
						}
						catch (AccessControlException ex) {
							if (logger.isInfoEnabled()) {
								logger.info("Not allowed to obtain system property [" + propertyName + "]: " +
										ex.getMessage());
							}
							return null;
						}
					}
				};
			}
			beanFactory.registerSingleton(SYSTEM_PROPERTIES_BEAN_NAME, systemProperties);
		}

猜你喜欢

转载自xuyunti.iteye.com/blog/2246978