Spring @Value 注释用于将默认值分配给变量和方法参数。我们可以使用 @Value 注释读取 Spring 环境变量以及系统变量。
Spring @Value - 默认值
我们可以使用 @Value 注释将默认值分配给类属性。
1@Value("Default DBConfiguration")
2private String defaultName;
@Value 注释参数只能是一个字符串,但春天试图将其转换为指定的类型. 下面的代码将正常工作,并将布尔和整数值分配给变量。
1@Value("true")
2private boolean defaultBoolean;
3
4@Value("10")
5private int defaultInt;
Spring @Value - 春季环境财产
1@Value("${APP_NAME_NOT_FOUND}")
2private String defaultAppName;
如果在春季环境属性中找不到密钥,则属性值将为${APP_NAME_NOT_FOUND}
。
1@Value("${APP_NAME_NOT_FOUND:Default}")
2private String defaultAppName;
Spring @Value - 系统环境
当 Spring 环境被填充时,它会读取所有系统环境变量并将其存储为属性,因此我们也可以使用 @Value 注释来分配系统变量。
1@Value("${java.home}")
2private String javaHome;
3
4@Value("${HOME}")
5private String homeDir;
春天 - 游戏
我们还可以使用 @Value 注释的 Spring Expression Language,这样我们也可以使用 SpEL 读取 java 主系统属性。
1@Value("#{systemProperties['java.home']}")
2private String javaHome;
Spring @Value 使用方法
当一个方法找到 @Value 注释时,当所有春节配置和豆类被加载时,春节文本将召唤它。如果该方法有多个参数,那么每个参数值将从方法注释中绘制出来。
1@Value("Test")
2public void printValues(String s, String v){} //both 's' and 'v' values will be 'Test'
1@Value("Test")
2public void printValues(String s, @Value("Data") String v){}
3// s=Test, v=Data
春天 价值 例子
让我们创建一个简单的 Spring 应用程序,我们将使用 @Value 注释来读取属性并将其分配给类变量。
1<dependency>
2 <groupId>org.springframework</groupId>
3 <artifactId>spring-context</artifactId>
4 <version>5.0.6.RELEASE</version>
5</dependency>
Our final project structure will look like below image, we will look each of the components one by one. Create a component class where we will inject variable values through @Value annotation.
1package com.journaldev.spring;
2
3import org.springframework.beans.factory.annotation.Value;
4
5public class DBConnection {
6
7 @Value("${DB_DRIVER_CLASS}")
8 private String driverClass;
9 @Value("${DB_URL}")
10 private String dbURL;
11 @Value("${DB_USERNAME}")
12 private String userName;
13 @Value("${DB_PASSWORD}")
14 private char[] password;
15
16 public DBConnection() {
17 }
18
19 public void printDBConfigs() {
20 System.out.println("Driver Class = " + driverClass);
21 System.out.println("DB URL = " + dbURL);
22 System.out.println("User Name = " + userName);
23
24 // Never do below in production environment :D
25 System.out.println("Password = " + String.valueOf(password));
26 }
27}
现在我们必须创建一个 Configuration 类,并为 DBConnection 类提供 @Bean 方法。
1package com.journaldev.spring;
2
3import org.springframework.beans.factory.annotation.Value;
4import org.springframework.context.annotation.Bean;
5import org.springframework.context.annotation.Configuration;
6import org.springframework.context.annotation.PropertySource;
7
8@Configuration
9@PropertySource("classpath:db.properties")
10@PropertySource(value = "classpath:root.properties", ignoreResourceNotFound = true)
11public class DBConfiguration {
12
13 @Value("Default DBConfiguration")
14 private String defaultName;
15
16 @Value("true")
17 private boolean defaultBoolean;
18
19 @Value("10")
20 private int defaultInt;
21
22 @Value("${APP_NAME_NOT_FOUND:Default}")
23 private String defaultAppName;
24
25 // @Value("#{systemProperties['java.home']}")
26 @Value("${java.home}")
27 private String javaHome;
28
29 @Value("${HOME}")
30 private String homeDir;
31
32 @Bean
33 public DBConnection getDBConnection() {
34 DBConnection dbConnection = new DBConnection();
35 return dbConnection;
36 }
37
38 @Value("Test")
39 public void printValues(String s, @Value("another variable") String v) {
40 System.out.println("Input Argument 1 =" + s);
41 System.out.println("Input Argument 2 =" + v);
42
43 System.out.println("Home Directory = " + homeDir);
44 System.out.println("Default Configuration Name = " + defaultName);
45 System.out.println("Default App Name = " + defaultAppName);
46 System.out.println("Java Home = " + javaHome);
47 System.out.println("Boolean = " + defaultBoolean);
48 System.out.println("Int = " + defaultInt);
49
50 }
51
52}
这里是我们的主要班级,我们正在创建基于注释的春季背景。
1package com.journaldev.spring;
2
3import java.sql.SQLException;
4
5import org.springframework.context.annotation.AnnotationConfigApplicationContext;
6
7public class SpringMainClass {
8
9 public static void main(String[] args) throws SQLException {
10 AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
11 context.scan("com.journaldev.spring");
12 context.refresh();
13 System.out.println("Refreshing the spring context");
14 DBConnection dbConnection = context.getBean(DBConnection.class);
15
16 dbConnection.printDBConfigs();
17
18 // close the spring context
19 context.close();
20 }
21
22}
当您运行该类时,它将产生以下输出。
1Input Argument 1 =Test
2Input Argument 2 =another variable
3Home Directory = /Users/pankaj
4Default Configuration Name = Default DBConfiguration
5Default App Name = Default
6Java Home = /Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home
7Boolean = true
8Int = 10
9Refreshing the spring context
10Driver Class = com.mysql.jdbc.Driver
11DB URL = jdbc:mysql://localhost:3306/Test
12User Name = journaldev
13Password = journaldev
请注意,在我们的环境为用户请求做好准备之前,正在调用printValues()
配置类。