AbstractBeanDefinitionReader.loadBeanDefinitions() 1234567891011121314151617181920212223242526272829303132333435
public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException { ResourceLoader resourceLoader = getResourceLoader(); if (resourceLoader == null) { throw new BeanDefinitionStoreException( "Cannot import bean definitions from location [" + location + "]: no ResourceLoader available"); } if (resourceLoader instanceof ResourcePatternResolver) { // Resource pattern matching available. try {//根据配置文件读取相应配置 Resource[] resources = ((ResourcePatternResolver) resourceLoader).getResources(location); int loadCount = loadBeanDefinitions(resources); if (logger.isDebugEnabled()) { logger.debug("Loaded " + loadCount + " bean definitions from location pattern [" + location + "]"); } return loadCount; } catch (IOException ex) { throw new BeanDefinitionStoreException( "Could not resolve bean definition resource pattern [" + location + "]", ex); } } else { // Can only load single resources by absolute URL. Resource resource = resourceLoader.getResource(location); int loadCount = loadBeanDefinitions(resource); if (logger.isDebugEnabled()) { logger.debug("Loaded " + loadCount + " bean definitions from location [" + location + "]"); } return loadCount; } }
这个是其中一个ResourceLoader的实现 ==>PathMatchingResourcePatternResolver.getResources(String locationPattern); 1234567891011121314151617181920212223242526272829
public Resource[] getResources(String locationPattern) throws IOException { Assert.notNull(locationPattern, "Location pattern must not be null"); if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) { // a class path resource (multiple resources for same name possible) if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) { // a class path resource pattern return findPathMatchingResources(locationPattern); } else { // all class path resources with the given name return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length())); } } else { // Only look for a pattern after a prefix here // (to not get fooled by a pattern symbol in a strange prefix). int prefixEnd = locationPattern.indexOf(":") + 1; if (getPathMatcher().isPattern(locationPattern.substring(prefixEnd))) { // a file pattern return findPathMatchingResources(locationPattern); } else { // a single resource with the given name return new Resource[] {getResourceLoader().getResource(locationPattern)}; } } }
==>PathMatchingResourcePatternResolver.findPathMatchingResources(String locationPattern); 12345678910111213141516171819202122
protected Resource[] findPathMatchingResources(String locationPattern) throws IOException { String rootDirPath = determineRootDir(locationPattern); String subPattern = locationPattern.substring(rootDirPath.length()); Resource[] rootDirResources = getResources(rootDirPath);//collectionFactory初始化一个set容量为16 Set result = CollectionFactory.createLinkedSetIfPossible(16); for (int i = 0; i < rootDirResources.length; i++) { Resource rootDirResource = rootDirResources[i]; if (isJarResource(rootDirResource)) { result.addAll(doFindPathMatchingJarResources(rootDirResource, subPattern)); } else { result.addAll(doFindPathMatchingFileResources(rootDirResource, subPattern)); } } if (logger.isDebugEnabled()) { logger.debug("Resolved location pattern [" + locationPattern + "] to resources " + result); } return (Resource[]) result.toArray(new Resource[result.size()]); }
前面说到有一个刷新WebApplicationContext的操作,但是XmlWebApplicationContext 并没有实现refresh方法,而方法的实现写在 AbstractRefreshableWebApplicationContext 中 ==>AbstractRefreshableWebApplicationContext.refresh(); 1234567
public void refresh() throws BeansException { if (ObjectUtils.isEmpty(getConfigLocations())) { //设置configLocations为默认的getDefaultConfigLocations() setConfigLocations(getDefaultConfigLocations()); } super.refresh(); }
==>AbstractApplicationContext.refresh(); 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
public void refresh() throws BeansException, IllegalStateException { synchronized (this.startupShutdownMonitor) { this.startupTime = System.currentTimeMillis(); synchronized (this.activeMonitor) { this.active = true; } // Tell subclass to refresh the internal bean factory. refreshBeanFactory(); ConfigurableListableBeanFactory beanFactory = getBeanFactory(); // Tell the internal bean factory to use the context's class loader. beanFactory.setBeanClassLoader(getClassLoader()); // Populate the bean factory with context-specific resource editors. beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this)); // Configure the bean factory with context semantics. beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)); beanFactory.ignoreDependencyInterface(ResourceLoaderAware.class); beanFactory.ignoreDependencyInterface(ApplicationEventPublisherAware.class); beanFactory.ignoreDependencyInterface(MessageSourceAware.class); beanFactory.ignoreDependencyInterface(ApplicationContextAware.class); // Allows post-processing of the bean factory in context subclasses. postProcessBeanFactory(beanFactory); // Invoke factory processors registered with the context instance. for (Iterator it = getBeanFactoryPostProcessors().iterator(); it.hasNext();) { BeanFactoryPostProcessor factoryProcessor = (BeanFactoryPostProcessor) it.next(); factoryProcessor.postProcessBeanFactory(beanFactory); } if (logger.isInfoEnabled()) { if (getBeanDefinitionCount() == 0) { logger.info("No beans defined in application context [" + getDisplayName() + "]"); } else { logger.info(getBeanDefinitionCount() + " beans defined in application context [" + getDisplayName() + "]"); } } try { // Invoke factory processors registered as beans in the context. invokeBeanFactoryPostProcessors(); // Register bean processors that intercept bean creation. registerBeanPostProcessors(); // Initialize message source for this context. initMessageSource(); // Initialize event multicaster for this context. initApplicationEventMulticaster(); // Initialize other special beans in specific context subclasses. onRefresh(); // Check for listener beans and register them. registerListeners(); // Instantiate singletons this late to allow them to access the message source. beanFactory.preInstantiateSingletons(); // Last step: publish corresponding event. publishEvent(new ContextRefreshedEvent(this)); } catch (BeansException ex) { // Destroy already created singletons to avoid dangling resources. beanFactory.destroySingletons(); throw ex; } } }
分享到:
相关推荐
Hibernate 程式 <br>SessionFactory 注入 <br>HibernateTemplate <br>Hibernate 编程交易管理 <br>Hibernate 宣告式交易管理 <br><br><br>Web 层 <br> Spring 提供了 MVC Web 框架,您可以善用 IoC 容器在依赖注入上...
首先,Spring框架的核心特性是依赖注入(Dependency Injection,DI),它允许开发者在运行时通过XML配置文件或者注解来管理对象及其依赖关系,而不是在代码中硬编码这些依赖。这种设计模式有助于降低组件之间的耦合...
标题中的“Spring初探共18页.pdf.zip”表明这是一个关于Spring框架的压缩文档,共有18页PDF内容。从标签“Spring初探共18页.pdf”我们可以推测,这份资料可能是一个关于Spring框架基础入门的学习资料,适合初学者。 ...
- **ApplicationContext**:在Bean Factory基础上增加了国际化支持、资源访问、事件发布等高级功能,是开发Web应用时最常用的容器。 - **Web Context**:特定于Web应用的ApplicationContext实现,提供了处理Web环境...
4. **资源操控与Web容器集成**:Spring提供了丰富的资源操控功能,并能够将DI容器无缝宿主到Web容器中,简化Web应用的部署和管理。 5. **外在化配置与作用范围**:支持将配置参数外在化,提高应用的可配置性和可...
1. **添加Spring依赖**:在项目的pom.xml或build.gradle文件中添加Spring相关的库依赖。 2. **创建Bean**:定义项目中需要管理的对象,即Bean,这些对象可以是自定义的Java类或其他第三方库的类。 3. **配置Bean**:...
Spring容器(ApplicationContext)负责创建、装配和管理Bean。开发者可以通过XML配置文件或者基于注解的方式定义Bean及其依赖关系。此外,Spring还提供了丰富的Bean后处理器,如初始化回调、销毁回调等,使得我们...
- **Web容器集成**:如何将Spring的控制反转容器嵌入Web容器中运行。 **2.6 外在化配置参数** - **配置参数管理**:介绍如何管理和读取外部配置文件中的参数。 **2.7 Spring受管Bean的作用范围** - **Bean作用域...
- **BeanFactory与ApplicationContext**:BeanFactory是Spring的基础容器,而ApplicationContext提供了更丰富的功能,如国际化、事件传播、应用上下文等。 - **AOP框架**:支持声明式事务管理和切面编程,允许...
在项目中定义一个Java类,然后通过Spring的配置文件或注解来管理这个类的实例化过程。以下是一个简单的示例: ```java public class MyService { private String message; public void setMessage(String ...
- **Type2 设值注入**:通过setter方法来进行依赖的注入,即在Bean的配置文件中通过`<property>`标签指定属性值。 - **Type3 构造子注入**:通过构造函数参数来注入依赖,这种方式确保了Bean的不可变性,增强了...
- **将DI容器宿主到Web容器中**:讲解了如何将Spring容器集成到Web容器中,使得Spring管理的Bean可以在Web环境中使用。 - **外在化配置应用参数**:介绍了如何将应用程序的配置信息从代码中分离出来,放置在外部...
在这一章中,我们将从Spring框架的基础出发,介绍其诞生背景和主要目标。我们会讲解Spring如何简化Java应用的开发,以及它提供的依赖注入(Dependency Injection, DI)机制,这是Spring的核心特性之一。此外,我们...