spring context:component-scan
<context:component-scan base-package="com.zhuguang.jack" <!-- 扫描的基本包路径 -->
annotation-config="true" <!-- 是否激活属性注入注解 -->
name-generator="org.springframework.context.annotation.AnnotationBeanNameGenerator" <!-- Bean的ID策略生成器 -->
resource-pattern="**/*.class" <!-- 对资源进行筛选的正则表达式,这边是个大的范畴,具体细分在include-filter与exclude-filter中进行 -->
scope-resolver="org.springframework.context.annotation.AnnotationScopeMetadataResolver" <!-- scope解析器 ,与scoped-proxy只能同时配置一个 -->
scoped-proxy="no" <!-- scope代理,与scope-resolver只能同时配置一个 -->
use-default-filters="false" <!-- 是否使用默认的过滤器,默认值true -->
>
<!-- 注意:若使用include-filter去定制扫描内容,要在use-default-filters="false"的情况下,不然会“失效”,被默认的过滤机制所覆盖 -->
<!-- annotation是对注解进行扫描 ,有这个注解的才会扫描实例化-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Component"/>
<!-- assignable是对类或接口进行扫描 ,实现这个接口的才会扫描实例化-->
<context:include-filter type="assignable" expression="com.wjx.betalot.performer.Performer"/>
<context:include-filter type="assignable" expression="com.wjx.betalot.performer.impl.Sonnet"/>
<context:exclude-filter type="regex" expression=".controller.*"/>
<!-- 注意:在use-default-filters="false"的情况下,exclude-filter是针对include-filter里的内容进行排除,
有Controller注解的排除, -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="assignable" expression="com.wjx.betalot.performer.impl.RainPoem"/>
<context:exclude-filter type="regex" expression=".service.*"/>
</context:component-scan>
public BeanDefinition parse(Element element, ParserContext parserContext) {
String[] basePackages = StringUtils.tokenizeToStringArray(element.getAttribute(BASE_PACKAGE_ATTRIBUTE),
ConfigurableApplicationContext.CONFIG_LOCATION_DELIMITERS); //[com.zhuguang.jack]
ClassPathBeanDefinitionScanner scanner = configureScanner(parserContext, element); //扫描器
Set<BeanDefinitionHolder> beanDefinitions = scanner.doScan(basePackages);
registerComponents(parserContext.getReaderContext(), beanDefinitions, element);
return null;
}
protected ClassPathBeanDefinitionScanner configureScanner(ParserContext parserContext, Element element) {
XmlReaderContext readerContext = parserContext.getReaderContext(); //XmlReaderContext@704deff2
boolean useDefaultFilters = true;
if (element.hasAttribute(USE_DEFAULT_FILTERS_ATTRIBUTE)) {
useDefaultFilters = Boolean.valueOf(element.getAttribute(USE_DEFAULT_FILTERS_ATTRIBUTE)); //默认过滤器
}
ClassPathBeanDefinitionScanner scanner = createScanner(readerContext, useDefaultFilters); //扫描器
return scanner;
}
protected Set<BeanDefinitionHolder> doScan(String... basePackages) {
for (String basePackage : basePackages) {
Set<BeanDefinition> candidates = findCandidateComponents(basePackage); // [[AnnotationServiceImpl];], [CommonController]; ]],
for (BeanDefinition candidate : candidates) {
ScopeMetadata scopeMetadata = this.scopeMetadataResolver.resolveScopeMetadata(candidate);
candidate.setScope(scopeMetadata.getScopeName());
String beanName = this.beanNameGenerator.generateBeanName(candidate, this.registry);
if (candidate instanceof AbstractBeanDefinition) {
postProcessBeanDefinition((AbstractBeanDefinition) candidate, beanName); //往bean定义里面设置属性,setLazyInit,setAutowireMode,setDependencyCheck,setInitMethodName,setEnforceInitMethod,setDestroyMethodName,setEnforceDestroyMethod都设置默认值,
}
if (candidate instanceof AnnotatedBeanDefinition) {
AnnotationConfigUtils.processCommonDefinitionAnnotations((AnnotatedBeanDefinition) candidate); //往bean定义里面设置属性,是否有Lazy,Primary,DependsOn,Role,Description注解,如果有就设置到bean定义中去。
}
if (checkCandidate(beanName, candidate)) {
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(candidate, beanName); //把BeanDefinition 变成BeanDefinitionHolder,
definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry); //
beanDefinitions.add(definitionHolder);
registerBeanDefinition(definitionHolder, this.registry); //注册到spring容器中,
}
}
}
return beanDefinitions;
}
public Set<BeanDefinition> findCandidateComponents(String basePackage) {
try {
String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX +
resolveBasePackage(basePackage) + "/" + this.resourcePattern; //classpath*:com/zhuguang/jack/**/*.class找到所有的。class文件,
Resource[] resources = this.resourcePatternResolver.getResources(packageSearchPath); //[file [E:AnnotationServiceImpl.class], file [E:CommonController.class], file [E:CommonMapper.class], file [E:ConsultConfigArea.class], file [E:ConsultContent.class], file [E:MyService.class], file [E:Test2.class], file [E:User.class]]
for (Resource resource : resources) {
if (isCandidateComponent(metadataReader) && resource.isReadable()) { //没有条件注解,就进来,没有注解的不进来。有@Component注解的才进来,
try {
MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource); //AnnotationMetadata里面封装了这个类的一些基本信息,类里面有哪些方法,方法上面有哪些注解,类上面有哪些注解,
if (isCandidateComponent(metadataReader)) {//!(this.isInterface || this.isAbstract) && (this.enclosingClassName == null || this.independentInnerClass); 不是接口不是抽象类就进来,CommonMapper是一个接口不进来,
ScannedGenericBeanDefinition sbd = new ScannedGenericBeanDefinition(metadataReader);
sbd.setResource(resource);
sbd.setSource(resource);
if (isCandidateComponent(sbd)) {
candidates.add(sbd);
}
else {
}
}
}
}
}
}
return candidates;
}
public MetadataReader getMetadataReader(Resource resource) throws IOException {
return new SimpleMetadataReader(resource, this.resourceLoader.getClassLoader());
}
SimpleMetadataReader(Resource resource, ClassLoader classLoader) throws IOException {
InputStream is = new BufferedInputStream(resource.getInputStream());//文件流,
ClassReader classReader;
classReader = new ClassReader(is);
AnnotationMetadataReadingVisitor visitor = new AnnotationMetadataReadingVisitor(classLoader);
classReader.accept(visitor, ClassReader.SKIP_DEBUG);
this.annotationMetadata = visitor;
this.classMetadata = visitor;
this.resource = resource;
}
protected boolean isCandidateComponent(MetadataReader metadataReader) throws IOException { //metadataReader是类的元数据信息,
for (TypeFilter tf : this.excludeFilters) {
if (tf.match(metadataReader, this.metadataReaderFactory)) {
return false;
}
}
for (TypeFilter tf : this.includeFilters) {//interface org.springframework.stereotype.Component,interface javax.inject.Named。有@Component注解的才进来,
if (tf.match(metadataReader, this.metadataReaderFactory)) {
return isConditionMatch(metadataReader);
}
}
return false;
}
public Resource[] getResources(String locationPattern) throws IOException {
if (locationPattern.startsWith(CLASSPATH_ALL_URL_PREFIX)) {
if (getPathMatcher().isPattern(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()))) {
return findPathMatchingResources(locationPattern);
}
}
}
protected Resource[] findPathMatchingResources(String locationPattern) throws IOException {
String rootDirPath = determineRootDir(locationPattern); //classpath*:com/zhuguang/jack/
String subPattern = locationPattern.substring(rootDirPath.length()); //**/*.class
Resource[] rootDirResources = getResources(rootDirPath); //返回[URL [file:/E:/Code/AAAAA/AAAAA/2019326/2/spring-source-web-master-aop/target/classes/com/zhuguang/jack/]]
Set<Resource> result = new LinkedHashSet<Resource>();
for (Resource rootDirResource : rootDirResources) {
rootDirResource = resolveRootDirResource(rootDirResource); //返回[URL [file:/E:/Code/AAAAA/AAAAA/2019326/2/spring-source-web-master-aop/target/classes/com/zhuguang/jack/]]
if (isJarResource(rootDirResource)) {
result.addAll(doFindPathMatchingJarResources(rootDirResource, subPattern));
}
else if (rootDirResource.getURL().getProtocol().startsWith(ResourceUtils.URL_PROTOCOL_VFS)) {
result.addAll(VfsResourceMatchingDelegate.findMatchingResources(rootDirResource, subPattern, getPathMatcher()));
}
else {
result.addAll(doFindPathMatchingFileResources(rootDirResource, subPattern));
}
}
return result.toArray(new Resource[result.size()]); //[file [E:AnnotationServiceImpl.class], file [E:CommonController.class], file [E:CommonMapper.class], file [E:ConsultConfigArea.class], file [E:ConsultContent.class], file [E:MyService.class], file [E:Test2.class], file [E:User.class]]
}
public Resource[] getResources(String locationPattern) throws IOException {
else {
return findAllClassPathResources(locationPattern.substring(CLASSPATH_ALL_URL_PREFIX.length()));
}
}
}
protected Resource[] findAllClassPathResources(String location) throws IOException {
Enumeration<URL> resourceUrls = getClassLoader().getResources(path); //AppClassLoader,返回[sun.misc.CompoundEnumeration@b83a9be, java.net.URLClassLoader$3@2609b277]
Set<Resource> result = new LinkedHashSet<Resource>();
while (resourceUrls.hasMoreElements()) {
URL url = resourceUrls.nextElement();
result.add(convertClassLoaderURL(url));
}
return result.toArray(new Resource[result.size()]); //返回[URL [file:/E:/Code/AAAAA/AAAAA/2019326/2/spring-source-web-master-aop/target/classes/com/zhuguang/jack/]]
}
public Enumeration<URL> getResources(String name) throws IOException {
@SuppressWarnings("unchecked")
Enumeration<URL>[] tmp = (Enumeration<URL>[]) new Enumeration<?>[];
if (parent != null) {
tmp[] = parent.getResources(name); //ExtClassLoader
} else {
tmp[] = getBootstrapResources(name);
}
tmp[] = findResources(name);
return new CompoundEnumeration<>(tmp);
}
private static Enumeration<URL> getBootstrapResources(String name) {
final Enumeration<Resource> e = getBootstrapClassPath().getResources(name); //sun.misc.URLClassPath@28d18df5
return new Enumeration<URL> () {
public URL nextElement() {
return e.nextElement().getURL();
}
public boolean hasMoreElements() {
return e.hasMoreElements();
}
}
}
spring context:component-scan的更多相关文章
- [Spring Boot] Use Component Scan to scan for Bean
Component Scan is important concept when we want to create Bean. Currently we know what, for the cla ...
- Spring注解Component原理源码解析
在实际开发中,我们经常使用Spring的@Component.@Service.@Repository以及 @Controller等注解来实现bean托管给Spring容器管理.Spring是怎么样实 ...
- Spring context:component-scan中使用context:include-filter和context:exclude-filter
Spring context:component-scan中使用context:include-filter和context:exclude-filter XML: <?xml version= ...
- Spring context:component-scan代替context:annotation-config
Spring context:component-scan代替context:annotation-config XML: <?xml version="1.0" encod ...
- Spring <context:annotation-config> 与<context-component-scan> 的作用
<context:annotation-config> 与<context-component-scan> 的作用 <context:annotation-config& ...
- Spring <context:component-scan>标签属性 use-default-filters 以及子标签 include-filter使用说明
Spring <context:component-scan>标签作用有很多,最基本就是 开启包扫描,可以使用@Component.@Service.@Component等注解: 今天要作 ...
- Spring 注解@Component,@Service,@Controller,@Repository
Spring 注解@Component,@Service,@Controller,@RepositorySpring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释, ...
- [转载]vs2012中使用Spring.NET报错:Spring.Context.Support.ContextRegistry 的类型初始值设定项引发异常
学习使用Spring.NET中的时候,写了一个Demo,在运行时报了一个错误:Spring.Context.Support.ContextRegistry 的类型初始值设定项引发异常. 重新整理思绪, ...
- 使用web.xml方式加载Spring时,获取Spring context的两种方式
使用web.xml方式加载Spring时,获取Spring context的两种方式: 1.servlet方式加载时: [web.xml] <servlet> <servlet-na ...
- 【报错】spring整合activeMQ,pom.xml文件缺架包,启动报错:Caused by: java.lang.ClassNotFoundException: org.apache.xbean.spring.context.v2.XBeanNamespaceHandler
spring版本:4.3.13 ActiveMq版本:5.15 ======================================================== spring整合act ...
随机推荐
- 19-视图集ViewSet和路由Routers
一.视图集ViewSet 使用视图集ViewSet,可以将一系列逻辑相关的动作放到一个类中: 1.list()提供一组数据 2.retrieve()提供单个数据 3.create()创建数据 4.up ...
- 02-Git远程仓库Github
1.Git远程仓库 (Gitgub网站作为远程代码仓库时的操作和本地代码仓库一样的,只是仓库位置不同而已) 需要准备的东西: 1.准备Git源代码仓库https://github.com/ 2.准备李 ...
- redis集群之Cluster
RedisCluster 是 Redis 的亲儿子,它是 Redis 作者自己提供的 Redis 集群化方案. 相对于 Codis 的不同,它是去中心化的,如图所示,该集群有三个 Redis 节点组成 ...
- 【趣学程序】Linux上安装Tengine(Nginx)
linux 安装tengine tengine是什么 tengine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.Tengine的性能和稳 ...
- Redisson实现分布式锁(1)---原理
Redisson实现分布式锁(1)---原理 有关Redisson作为实现分布式锁,总的分3大模块来讲. 1.Redisson实现分布式锁原理 2.Redisson实现分布式锁的源码解析 3.Redi ...
- Java的23种设计模式,详细讲解(三)
本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...
- Oracle 中Number的长度定义
Number可以通过如下格式来指定:Field_NAME Number(precision ,scale),其中precision指Number可以存储的最大数字长度(不包括左右两边的0),scale ...
- webUploader上传大视频文件相关web.config配置
在webuploader上传大文件时必须配置一下,不然请求后台处理程序时,会请求超时.出现404! <system.web> <httpRuntime maxRequestLengt ...
- LayoutSubviews的调用
1.当view被添加到另一个view上时调用 2.布局子控件时调用 3.屏幕旋转的时候调用 4.当view的尺寸大小改变的时候调用
- 重温《NoSQL精粹》
前言 在网上有关Redis相关文章满天飞的时候,它是什么,用于解决什么问题,有哪些相类似的技术,与传统的关系型数据库有哪些差别,什么时候使用?这个时候我决定重温一下<NoSQL精粹>. 也 ...