javascript
Spring Boot系列四 Spring @Value 属性注入使用总结一
@Value注入
不通過配置文件的注入屬性的情況
通過@Value將外部的值動態注入到Bean中,使用的情況有:
注入普通字符串
注入操作系統屬性
注入表達式結果
注入其他Bean屬性:注入beanInject對象的屬性another
注入文件資源
注入URL資源
詳細代碼見:
@Value("normal") private String normal; // 注入普通字符串@Value("#{systemProperties['os.name']}") private String systemPropertiesName; // 注入操作系統屬性@Value("#{ T(java.lang.Math).random() * 100.0 }") private double randomNumber; //注入表達式結果@Value("#{beanInject.another}") private String fromAnotherBean; // 注入其他Bean屬性:注入beanInject對象的屬性another,類具體定義見下面@Value("classpath:com/hry/spring/configinject/config.txt") private Resource resourceFile; // 注入文件資源@Value("http://www.baidu.com") private Resource testUrl; // 注入URL資源1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
注入其他Bean屬性:注入beanInject對象的屬性another
@Component
public class BeanInject {
@Value(“其他Bean的屬性”)
private String another;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
注入文件資源:com/hry/spring/configinject/config.txt
test configuration file
1
測試類:
@SpringBootApplication
public class Application {
}
@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)
public class ConfiginjectApplicationTest {
@Autowired
private BaseValueInject baseValueInject;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
運行測試類
normal=normal
systemPropertiesName=Windows 10
randomNumber=35.10603794922444
fromAnotherBean=其他Bean的屬性
resourceFile=test configuration file
testUrl=…百度一下,你就知道…略
1
2
3
4
5
6
通過配置文件的注入屬性的情況
通過@Value將外部配置文件的值動態注入到Bean中。配置文件主要有兩類:
application.properties。application.properties在spring boot啟動時默認加載此文件
自定義屬性文件。自定義屬性文件通過@PropertySource加載。@PropertySource可以同時加載多個文件,也可以加載單個文件。如果相同第一個屬性文件和第二屬性文件存在相同key,則最后一個屬性文件里的key啟作用。加載文件的路徑也可以配置變量,如下文的${anotherfile.configinject},此值定義在第一個屬性文件config.properties
第一個屬性文件config.properties內容如下:
${anotherfile.configinject}作為第二個屬性文件加載路徑的變量值
book.name=bookName
anotherfile.configinject=placeholder
1
2
第二個屬性文件config_placeholder.properties內容如下:
book.name.placeholder=bookNamePlaceholder
1
下面通過@Value(“${app.name}”)語法將屬性文件的值注入bean屬性值,詳細代碼見:
@Component
// 引入外部配置文件組:KaTeX parse error: Expected group after '_' at position 175: …iginject/config_?{anotherfile.configinject}.properties"})
public class ConfigurationFileInject{
@Value("${app.name}")
private String appName; // 這里的值來自application.properties,spring boot啟動時默認加載此文件
}
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
測試代碼:
Application.java同上文
@RunWith(SpringRunner.class)
@SpringBootTest(classes=Application.class)
public class ConfiginjectApplicationTest {
@Autowired
private ConfigurationFileInject configurationFileInject;
}
1
2
3
4
5
6
7
8
9
10
11
12
測試運行結果:
bookName=bookName
bookNamePlaceholder=bookNamePlaceholder
appName=appName
env=StandardEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[Inlined Test Properties,systemProperties,systemEnvironment,random,applicationConfig: [classpath:/application.properties],class path resource [com/hry/spring/configinject/config_placeholder.properties],class path resource [com/hry/spring/configinject/config.properties]]}
env=bookNamePlaceholder
總結
以上是生活随笔為你收集整理的Spring Boot系列四 Spring @Value 属性注入使用总结一的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 剑指 Offer II 022. 链表中
- 下一篇: 基于Netty+Zookeeper实现D