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 }}
JavaDevTeam
notes
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
notes
/
Spring
/
spring-导入外部配置文件.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
52 lines (39 loc) · 1.51 KB
master
Breadcrumbs
notes
/
Spring
/
spring-导入外部配置文件.java
Copy path
Top
File metadata and controls
Code
Blame
52 lines (39 loc) · 1.51 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
42
43
44
45
46
47
48
49
50
51
52
1
,
第一种方式
,
简写完成
<
context
:
property
-
placeholder
location
=
"classpath:jdbc.properties"
/>
<
context
:
property
-
placeholder
location
=
"classpath:upload.properties"
/>
#
配置文件中出现了多个
<
context
:
property
-
placeholder
/>
所有都要加上ignore
-
unresolvable
=
"true"
,
一个不加也是不行的
2
,
第二种传统方式
<!--
使用spring自带的占位符替换功能
,
楼上是简写
-->
<
bean
class
=
"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
>
<!--
允许覆盖JVM参数
-->
<
property
name
=
"systemPropertiesModeName"
value
=
"SYSTEM_PROPERTIES_MODE_OVERRIDE"
/>
<!--
忽略未找到的文件
,
就是说如果文件不存在
,
不会报错
-->
<
property
name
=
"ignoreResourceNotFound"
value
=
"true"
/>
<
property
name
=
"locations"
>
<
list
>
<
value
>
classpath
:
jdbc
.
properties
</
value
>
<
value
>
classpath
:
upload
.
properties
</
value
>
</
list
>
</
property
>
</
bean
>
不仅仅是可以在xml中使用
'${...}'
对bean的属性进行注值
(
jdbc
)
在JAVA代码中可以使用注解直接进行注入值
*
使用
@
Value
注解
*
value属性的值
,
就是配置文件中的
'${key}'
,
@
Value
(
value
=
"${userName}"
)
private
String
userName
;
@
Value
(
value
=
"${passWord}"
)
private
String
passWord
;
*
@
Value详解
1
,
什么时候注入相应的值
>
在spring容器中所有bean完成初始化后执行
2
,
有个非常特殊的地方
>
'只会在当前spring容器中查找配置参数'
>
啥意思呢
*
这个
@
Value
注解标识的类
,
被注册到了哪个IOC容器
?
所以
,
就会在这个IOC容器中到加载的properties文件
,
再找它的值
其他IOC加载的
.
properties文件
,
一律不鸟
3
,
添加默认值
@
Value
(
"${jdbc.url:aDefaultUrl}"
)
You can’t perform that action at this time.