news 2026/6/6 18:22:22

(23)声明Bean的注解

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
(23)声明Bean的注解

负责声明Bean的注解,常见的包括四个:

  • @Component
  • @Controller
  • @Service
  • @Repository

源码如下:

packagecom.powernode.annotation;importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;@Target(value={ElementType.TYPE})@Retention(value=RetentionPolicy.RUNTIME)public@interfaceComponent{Stringvalue();}
packageorg.springframework.stereotype;importjava.lang.annotation.Documented;importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;importorg.springframework.core.annotation.AliasFor;@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic@interfaceController{@AliasFor(annotation=Component.class)Stringvalue()default"";}
packageorg.springframework.stereotype;importjava.lang.annotation.Documented;importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;importorg.springframework.core.annotation.AliasFor;@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic@interfaceService{@AliasFor(annotation=Component.class)Stringvalue()default"";}
packageorg.springframework.stereotype;importjava.lang.annotation.Documented;importjava.lang.annotation.ElementType;importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;importorg.springframework.core.annotation.AliasFor;@Target({ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documented@Componentpublic@interfaceRepository{@AliasFor(annotation=Component.class)Stringvalue()default"";}

通过源码可以看到,@Controller、@Service、@Repository这三个注解都是@Component注解的别名。
也就是说:这四个注解的功能都一样。用哪个都可以。
只是为了增强程序的可读性,建议:

  • 控制器类上使用:Controller
  • service类上使用:Service
  • dao类上使用:Repository

他们都是只有一个value属性。value属性用来指定bean的id,也就是bean的名字。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/5 9:29:16

2025年项目管理系统全新排名出炉:聚焦私有部署、信创适配与全流程闭环

在2025年,企业对项目管理系统的核心诉求已从基础的任务协同升级为“安全可控、合规适配、链路贯通”的综合能力考核。私有部署带来的数据主权保障、信创适配契合的国产化发展浪潮,以及全流程闭环实现的管理效率跃迁,成为衡量系统竞争力的三大…

作者头像 李华
网站建设 2026/6/5 16:24:57

ROS2核心概念之动作

这些应用功能的实现中,另外一种ROS通信机制也会被常常用到——那就是动作。从这个名字上就可以很好理解这个概念的含义,这种通信机制的目的就是便于对机器人某一完整行为的流程进行管理。 回到顶部 一、通信模型 举个例子,比如我们想让机器…

作者头像 李华
网站建设 2026/6/2 0:50:08

解密Prompt系列66. 视觉Token爆炸→DeepSeek-OCR光学压缩

着 DeepSeek-OCR这篇论文,本章我们来回顾下多模态大模型(VLM)的核心技术演进。很多人认为:图像Token的信息密度和效率远不如文本。但 DeepSeek-OCR的核心价值,就是用实践证明了这是一个伪命题。它通过一套巧妙的串行视…

作者头像 李华
网站建设 2026/6/5 16:11:15

Typst列表符号终极指南:从异常诊断到完美渲染

Typst列表符号终极指南:从异常诊断到完美渲染 【免费下载链接】typst A new markup-based typesetting system that is powerful and easy to learn. 项目地址: https://gitcode.com/GitHub_Trending/ty/typst 在使用Typst进行文档排版时,列表符号…

作者头像 李华
网站建设 2026/6/6 1:49:37

NetBox 自动化导入资产 - IP地址

简介本文章主要讲解使用orb-agent 扫描网络收集IP信息,通过Diode 摄取到NetBox。这两个工具都是NetBox官方的自动化发现产品,下面是示意图。------------------| orb-agent ||------------------|| 网络扫描/资产发现 |------------------|| grpc 通过NM…

作者头像 李华