Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
blissSeven
toBeTopJavaer
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
toBeTopJavaer
/
basics
/
java-basic
/
annotation-in-spring.md
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
42 lines (20 loc) · 1.57 KB
master
Breadcrumbs
toBeTopJavaer
/
basics
/
java-basic
/
annotation-in-spring.md
Copy path
Top
File metadata and controls
Code
Blame
42 lines (20 loc) · 1.57 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
@
Configuration把一个类作为一个IoC容器
,
它的某个方法头上如果注册了
@
Bean
,
就会作为这个Spring容器中的Bean
。
@
Scope注解
作用域
@
Lazy
(
true
)
表示延迟初始化
@
Service用于标注业务层组件
、
@
Controller用于标注控制层组件
@
Repository用于标注数据访问组件
,
即DAO组件
。
@
Component泛指组件
,
当组件不好归类的时候
,
我们可以使用这个注解进行标注
。
@
Scope用于指定scope作用域的
(
用在类上
)
@
PostConstruct用于指定初始化方法
(
用在方法上
)
@
PreDestory用于指定销毁方法
(
用在方法上
)
@
DependsOn
:
定义Bean初始化及销毁时的顺序
@
Primary
:
自动装配时当出现多个Bean候选者时
,
被注解为
@
Primary的Bean将作为首选者
,
否则将抛出异常
@
Autowired
默认按类型装配
,
如果我们想使用按名称装配
,
可以结合
@
Qualifier注解一起使用
。
如下
:
@
Autowired
@
Qualifier
(
"personDaoBean"
)
存在多个实例配合使用
@
Resource默认按名称装配
,
当找不到与名称匹配的bean才会按类型装配
。
@
PostConstruct
初始化注解
@
PreDestroy
摧毁注解
默认
单例
启动就加载
###
Spring中的这几个注解有什么区别
:
@
Component
、
@
Repository
、
@
Service
、
@
Controller
1.
@
Component指的是组件
,
@
Controller
,
@
Repository和
@
Service
注解都被
@
Component修饰
,
用于代码中区分表现层
,
持久层和业务层的组件
,
代码中组件不好归类时可以使用
@
Component来标注
2.
当前版本只有区分的作用
,
未来版本可能会添加更丰富的功能
You can’t perform that action at this time.