在 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); }
相关推荐
- **AbstractApplicationContext.prepareBeanFactory(DefaultListableBeanFactory)** - 设置`DefaultListableBeanFactory`的`BeanExpressionResolver`为`StandardBeanExpressionResolver`:用于处理Spring表达式...
AbstractApplicationContext.refresh(){ 1.为刷新准备此上下文 prepareRefresh(){ initPropertySources();//在上下文环境中初始化任何占位符属性源 getEnvironment().validateRequiredProperties();//验证所有...
在这个过程中,AbstractApplicationContext 会调用 obtainFreshBeanFactory() 方法来获取一个新的 BeanFactory,然后调用 prepareBeanFactory() 方法来准备 BeanFactory。 其次,refresh 方法会实例化单例 Bean,这...
`refresh()`方法是AbstractApplicationContext的核心功能之一,它的主要任务是刷新IoC容器,确保每次启动或重新启动时都能得到一个新的、完整的容器实例。当调用`refresh()`时,会执行一系列的步骤来初始化和配置...
Spring框架作为Java企业级开发中广泛使用的轻量级框架之一,其核心功能之一便是通过依赖注入(DI)来管理对象间的依赖关系。Spring框架的核心是其IOC容器,而IOC容器的核心则是BeanFactory接口。BeanFactory负责实例...
一旦有了BeanFactory,`prepareBeanFactory(beanFactory)`方法开始执行,它负责配置BeanFactory以适应Spring Boot的需求。具体操作包括: 1. 注册`BeanClassLoader`,用于在创建Bean实例时提供类加载器。 2. 配置`...
自定义版本可以扩展Spring提供的`AbstractApplicationContext`,并覆盖其关键方法,如`refresh()`和`close()`,以适应特定的需求。 1. **初始化配置**: - 在`refresh()`方法中,我们需要加载XML配置文件。这可以...
在这个阶段,AbstractApplicationContext的`prepareRefresh()`方法被调用,初始化重要的属性如启动时间startupDate和状态标识closed/active,同时初始化PropertySources以加载环境属性。此外,还会检查Environment...
refresh() 方法是定义在 AbstractApplicationContext 类中的,它的主要作用是刷新 ApplicationContext 对象,准备它用于 bean 的实例化和依赖注入。下面是 refresh() 方法的源代码: ```java public void refresh()...