惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
S
Security Affairs
阮一峰的网络日志
阮一峰的网络日志
L
LangChain Blog
Microsoft Azure Blog
Microsoft Azure Blog
雷峰网
雷峰网
Recent Announcements
Recent Announcements
WordPress大学
WordPress大学
The GitHub Blog
The GitHub Blog
博客园_首页
The Cloudflare Blog
M
MIT News - Artificial intelligence
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
S
SegmentFault 最新的问题
P
Proofpoint News Feed
Y
Y Combinator Blog
Jina AI
Jina AI
博客园 - 聂微东
A
About on SuperTechFans
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
G
Google Developers Blog
云风的 BLOG
云风的 BLOG
F
Full Disclosure
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
T
Tailwind CSS Blog
J
Java Code Geeks
Vercel News
Vercel News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
小众软件
小众软件
酷 壳 – CoolShell
酷 壳 – CoolShell
T
The Blog of Author Tim Ferriss
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
博客园 - 三生石上(FineUI控件)
W
WeLiveSecurity
PCI Perspectives
PCI Perspectives
Attack and Defense Labs
Attack and Defense Labs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
宝玉的分享
宝玉的分享
IT之家
IT之家
Hacker News: Ask HN
Hacker News: Ask HN
The Register - Security
The Register - Security
T
The Exploit Database - CXSecurity.com
T
Threat Research - Cisco Blogs

克洛洛日记

一年回本计划 | 克洛洛日记 股票期权的Theta | 克洛洛日记 股票期权的Gamma | 克洛洛日记 股票期权的Delta | 克洛洛日记 KDJ指标 | 克洛洛日记 布林线 | 克洛洛日记 MACD指标 | 克洛洛日记 RSI指标 | 克洛洛日记 时间协议 | 克洛洛日记 免费申请 JetBrains 全产品 License | 克洛洛日记 Aloha 一个Hexo主题 | 克洛洛日记 泰国之旅 [三] | 克洛洛日记 泰国之旅 [二] | 克洛洛日记 泰国之旅 [一] | 克洛洛日记 Nginx使用SSL证书配置https | 克洛洛日记 i18next | 克洛洛日记 错误排查:非ROOT用户使用密钥进行ssh登录提示输入密码 | 克洛洛日记 使用Github、Travis-CI和Coding.net自动部署博客[三] | 克洛洛日记 使用EditorConfig | 克洛洛日记 JavaScript执行window.print()打印内容为空白 | 克洛洛日记 使用Github、Travis-CI和Coding.net自动部署博客[二] | 克洛洛日记 寓食记 | 克洛洛日记 使用Github、Travis-CI和Coding.net自动部署博客[一] | 克洛洛日记 书单 | 克洛洛日记 WebJars使用示例 | 克洛洛日记 WebJars介绍 | 克洛洛日记 Maven打jar包内容出错 | 克洛洛日记 数据分布式模型 | 克洛洛日记 Wicket的HelloWorld | 克洛洛日记 Java运行时内存结构 | 克洛洛日记 前端优化法则 | 克洛洛日记
聊聊Spring的stereotype注解 | 克洛洛日记
海拉鲁球果 <hyrul · 2019-10-23 · via 克洛洛日记

· 技术 / Java

我们在使用Spring的时候,使用最多的注解是什么?我想除了@Bean就是@Component了吧?@Component位于spring-context库(5.1.8版本,文中未特别说明的情况下默认此版本)的org.springframework.stereotype包路径下面:

Spring Stereotype 1

会发现还有我们熟悉的@Controller,@Service,@Repository,这不就是Spring MVC里经常用到的么?

什么是 Stereotype

Stereotype是一种J2EE特性,以下来自网友:

Stereotype特性最早出现在J2EE6中(忘记是哪个JSR提出的了),
可以理解为围绕着“元数据”功能而发展出来的一种设计模式,
虽然我很难说清楚他属于23个设计模式中的哪一个,
但是这确实已经是一种约定俗成的做法,
只要看到Stereotype就应该像看到“Factory——工厂模式”、“Adapter——适配器模式”、“Facade——外观模式”一样,一眼就知道他的作用。

简单的说Stereotype特性就是用注解来告诉框架某个类是属于系统功能中的哪一层。

Stereotype 特性如何被使用

在Stereotype模式下,Spring为Controller-Service-Dao的分层模型分别提供了@Controller、@Service、@Repository注解。

我们按照Stereotype的模式为对应的类标记3个注解,然后在引入MVC、ORM、JPA相关的框架之后这些注解会告诉框架对应的类扮演着什么样的功能角色,框架就能很清晰的根据注解提供相关的功能服务。

例如引入spring-webmvc库后,类如果被@Controller注解标记,框架就知道他们都是处理前端请求的,框架就会为他提供RequestMapping之类的功能。