Spring Bean的包掃描的實現方法
我們知道,Spring可以通過包掃描將使用@Component注解定義的Bean定義到容器中。今天就來探究下他實現的原理。
首先,找到@Component注解的處理類注解的定義,一般都需要配套的對注解的處理才能完成注解所代表的功能。所以我們通過@Component注解的用到的地方,來查找可能的處理邏輯;我們先進入Spring的項目,在IDEA里面用Ctrl和鼠標左鍵點擊Component注解的名稱,IDEA會顯示出使用到這個類的位置,我們從彈出的列表中找到一個名稱像的類,去看類上面的注釋說明,如圖:
我們點進類中,可以看到第一行就說了這個類是為了從classpath里面找到定義的Bean:
一般Spring的類都是經過設計的,職責清晰。所以一般都是有簡單直接的接口暴露,我們打開類的公開API可以看到有個很直接的方法就叫做掃描,看看注釋說“從指定的包中掃描Bean”,那就是它了。
然后,我們為了確認,實現確實是通過這個方法,可以啟動程序,打個斷點看看是否經過這里(但是這這里,沒有調用scan()方法,而是更深一層的doScan方法,也確實費解)。
我們進入doScan() 方法看看實現:
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {Assert.notEmpty(basePackages, 'At least one base package must be specified');Set<BeanDefinitionHolder> beanDefinitions = new LinkedHashSet<>();// 可以指定多個basePackage,這里就對每個都處理for (String basePackage : basePackages) { // 這個方法是真正的查找候選Bean的地方Set<BeanDefinition> candidates = findCandidateComponents(basePackage);// 對于每個查找出的候選Bean,進行處理for (BeanDefinition candidate : candidates) { // 解析@Scope的元數據ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);candidate.setScope(scopeMetadata.getScopeName());// 為候選的Bean生成一個名稱String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);// 應用后置處理器if (candidate instanceof AbstractBeanDefinition) {postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName);}// // 處理一些其它通用的注解的元數據if (candidate instanceof AnnotatedBeanDefinition) {AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate);}// 校驗通過后,注冊到 BeanFactoryif (checkCandidate(beanName, candidate)) {BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName);definitionHolder =AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);beanDefinitions.add(definitionHolder);registerBeanDefinition(definitionHolder, this.registry);}}}return beanDefinitions;}
從方法中我們可以明顯的看到,核心代碼還在findCandidateComponents方法里面,我們進入這個方法后再通過調試一直找到核心代碼scanCandidateComponents。如下圖,第一處是找到指定包路徑所代表的classpath中的資源對象, 但是這里只是找到了包下面有什么,但是還不知道包下面的類是不是一個候選的Bean(可以看到將DTO類也掃描到了)。如下:
正常思路,拿到了有哪些資源就該進一步去篩選,看看這些資源有哪些是真正的Bean的定義類。
現在我們還不清楚的是,Spring通過什么方式知道一個類是否是真正的Bean的。我們繼續調試,到上圖的430行debug進去看看,可以走到org.springframework.core.type.classreading.SimpleMetadataReader這個類的構造器中,如下:
SimpleMetadataReader(Resource resource, @Nullable ClassLoader classLoader) throws IOException { // 通過流讀取資源的內容,現在這個資源可以認為是我們的類InputStream is = new BufferedInputStream(resource.getInputStream());ClassReader classReader;try { // 這個Reader的構造器中就將流讀取完畢了classReader = new ClassReader(is);}catch (IllegalArgumentException ex) { // 通過這個異常的信息,可以推測出,其實這里是通過ASM讀取Class文件的定義了throw new NestedIOException('ASM ClassReader failed to parse class file - ' +'probably due to a new Java class file version that isn’t supported yet: ' + resource, ex);}finally {is.close();} // 這里根據命名可以推測是訪問者模式來暴露注解的元數據AnnotationMetadataReadingVisitor visitor = new AnnotationMetadataReadingVisitor(classLoader);// 這個accpect方法也是訪問者模式中的典型方法,在這里面,是數據的解析邏輯classReader.accept(visitor, ClassReader.SKIP_DEBUG);this.annotationMetadata = visitor;// (since AnnotationMetadataReadingVisitor extends ClassMetadataReadingVisitor)this.classMetadata = visitor;this.resource = resource;}
我們在進入classReader.accept方法,這里面可以看到reader對于Class文件的的按字節解析。
例如,下面讀取的類聲明,類注解都是包掃描需要的類元數據:
拿到這些元數據之后,就按照包掃描的過濾器就過濾出真正需要的類,作為候選的Bean
獲取到元數據之后,就可以按部就班對Bean進行注冊、初始化等一系列邏輯啦~
總結 包掃描是通過讀取包對應的類路徑下的class文件后,對class文件進行解析元數據的方式,確定了Bean的定義的; 本地IDEA的啟動方式可能和Jar包方式尋找資源的方式略有不同,但是思路是一致的,都是按照第一點查找;到此這篇關于Spring Bean的包掃描的實現方法的文章就介紹到這了,更多相關Spring Bean掃描包內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
