您的位置 首页 > 德语词汇

controller是什么意思 Controller

大家好,今天来为大家分享controller是什么意思的一些知识点,和Controller的问题解析,大家要是都明白,那么可以忽略,如果不太清楚的话可以看看本篇文章,相信很大概率可以解决您的问题,接下来我们就一起来看看吧!

1、今天主要聊聊@Controller、@Service和@Component这三个注解的关系和区别。网上很多人对这三个注解进行了详细的解释,但是仅仅局限于理论,个人对于没有经过自己验证的结果总是持怀疑态度,所有花时间研究了一下,也对这三个注解理解的更加透彻。(ps:网上好多回答不一定正确,所以只能自己花时间验证)

2、@Target(ElementType.TYPE)\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\n@Indexed\npublic@interfaceComponent{\n\tStringvalue()default"";\n}

@Target({ElementType.TYPE})\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\n@Component//关键注解\npublic@interfaceController{\n\t@AliasFor(annotation=Component.class)\n\tStringvalue()default"";\n}

@Target({ElementType.TYPE})\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\n@Component//关键注解\npublic@interfaceService{\n\t@AliasFor(annotation=Component.class)\n\tStringvalue()default"";\n}注解扫描

首先说说这三个注解的关系,从源码中可以看出,@Controller和@Service都派生于@Component,所以三者的使用方式基本没什么差别。(ps:既然这么设计,那一定是有区别的)。

controller是什么意思 Controller

3、在平时的开发中,我们通常在控制层采用注解@Controller,在业务层采用注解@Service。spring在启动时,有一个非常核心的类ConfigurationClassPostProcessor会对类路径下的所以类进行扫描,将符合条件的bean扫描出来添加到beanDefinitionMap集合中,方便接下来的实例化。具体的扫描过程比较复杂,仅仅贴出核心判断逻辑代码。

4、org.springframework.core.type.filter.AnnotationTypeFilter

5、protectedbooleanmatchSelf(MetadataReadermetadataReader){\n\tAnnotationMetadatametadata=metadataReader.getAnnotationMetadata();\n\treturnmetadata.hasAnnotation(this.annotationType.getName())||\n\t\t\t(this.considerMetaAnnotations&&metadata.hasMetaAnnotation(this.annotationType.getName()));\n}

代码解释:

6、(1)this.annotationType.getName():获取的是注解@Component的全路径名org.springframework.stereotype.Component。

7、(2)metadata.hasAnnotation(this.annotationType.getName()):判断当前的类是否直接采用注解@Component。

8、(3)metadata.hasMetaAnnotation(this.annotationType.getName()):如果当前的类没有直接采用@Component,而是采用了类组合注解@Controller,判断组合注解@Controller中是否包含@Component。

9、至此,所有添加了注解@Controller、@Service和@Component都被spring扫描出来了。(ps:这就说明了其实在扫描的时候spring其实将这三个注解都按照@Component进行扫描的)

10、如果不使用springMVC时,三者使用其实是没有什么差别的,但如果使用了springMVC,@Controller就被赋予了特殊的含义。

11、spring会遍历上面扫描出来的所有bean,过滤出那些添加了注解@Controller的bean,将Controller中所有添加了注解@RequestMapping的方法解析出来封装成RequestMappingInfo存储到RequestMappingHandlerMapping中的mappingRegistry。后续请求到达时,会从mappingRegistry中查找能够处理该请求的方法。

12、org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping

13、protectedbooleanisHandler(Class<?>beanType){\n\t//判断扫描出来的bean是否包含注解@Controller,\n\t//如果包含,springMVC会将其封装为RequestMappingInfo\n\treturn(AnnotatedElementUtils.hasAnnotation(beanType,Controller.class)||\n\t\t\tAnnotatedElementUtils.hasAnnotation(beanType,RequestMapping.class));\n}

privateRequestMappingInfocreateRequestMappingInfo(AnnotatedElementelement){\n\t//判断传递进来的方法是否包含@RequestMapping,\n\t//如果包含,就将其封装成RequestMappingInfo\n\tRequestMappingrequestMapping=AnnotatedElementUtils.findMergedAnnotation(element,RequestMapping.class);\n\tRequestCondition<?>condition=(elementinstanceofClass?\n\t\t\tgetCustomTypeCondition((Class<?>)element):getCustomMethodCondition((Method)element));\n\treturn(requestMapping!=null?createRequestMappingInfo(requestMapping,condition):null);\n}@Service分析

目前@Service本人没有找到其特殊之处,可能spring官方后续会添加特殊的操作吧。

14、该注解是万能的注解,通常加在配置类上。

15、实际上有一个注解本文没有具体讲解,它就是@Repository,由于本人没有亲自验证,所以就没有进行分析,怕误导大家。有具体分析并验证过的网友,大家可以一起探讨。

16、如果有小伙伴也想验证,可以将断点打在我在文中贴出来的三段核心代码处,在spring启动的时候可以进行调试。具体的代码调用逻辑,可能会在以后的文章中进行分析。

关于controller是什么意思到此分享完毕,希望能帮助到您。

本站涵盖的内容、图片、视频等数据,部分未能与原作者取得联系。若涉及版权问题,请及时通知我们并提供相关证明材料,我们将及时予以删除!谢谢大家的理解与支持!

Copyright © 2023