Spring @Value usage summary, the difference between # and $

origin

The usual usage is @Value("${jdbc.driverClass}"), but I see the usage of @Value("#{ab}") in the official Spring documentation.

So researched.

in conclusion

There are two types of values ​​for @Value:

① ${ property : default_value }

② #{ obj.property? : default_value }

That is to say, the first injected is the property corresponding to the external parameter, and the second is the content corresponding to the SpEL expression.

The default_value is the default value when the previous value is empty. Note the difference between the two.

example

1. Use STS to create a new Spring Boot project, and leave the dependencies blank.

2. Add the following to the application.properties file:

jdbc.driverClass=com.mysql
 jdbc.url=3306@local
 jdbc.user=admin
 jdbc.pwd=pwd

3. Create a new class ValueDemo with the following contents:

 
 1 package cn.larry.spring;
 2 
 3 import javax.annotation.PostConstruct;
 4 
 5 import org.springframework.beans.factory.annotation.Value;
 6 import org.springframework.stereotype.Component;
 7 
 8 @Component
 9 public class ValueDemo {
10     @Value("${jdbc.driverClass}")
11     private String driver;
12     
13     @PostConstruct
14     public void run(){
15         System.out.println(driver);
16     }
17 
18 }
 

4. Start the Spring Boot project and the output is as follows:

This is the usual $ {properties} , very simple. continue.

5. Create a new class AnotherObj with the following contents:


 
 1 package cn.larry.spring;
 2 
 3 import org.springframework.beans.factory.annotation.Value;
 4 import org.springframework.stereotype.Component;
 5 
 6 @Component
 7 public class AnotherObj {
 8     @Value("${jdbc.user}")
 9     private String name;
10     @Value("{jdbc.pwd}")
11     private String pwd;
12 
13     public String getName() {
14         return name;
15     }
16 
17     public void setName(String name) {
18         this.name = name;
19     }
20 
21     public String getPwd() {
22         return pwd;
23     }
24 
25     public void setPwd(String pwd) {
26         this.pwd = pwd;
27     }
28 
29 }
 

6. Modify ValueDemo as follows:

 
 1 package cn.larry.spring;
 2 
 3 import javax.annotation.PostConstruct;
 4 
 5 import org.springframework.beans.factory.annotation.Value;
 6 import org.springframework.stereotype.Component;
 7 
 8 @Component
 9 public class ValueDemo {
10     @Value("${jdbc.driverClass}")
11     private String driver;
12     
13     @Value("#{anotherObj.name}")
14     private String name;
15     
16     @PostConstruct
17     public void run(){
18         System.out.println(driver);
19         System.out.println(name);
20     }
21     
22 }
 

7. Execute the Spring Boot project again, and the results are as follows:

Shown here is the effect of # {SpEL} .

8. The interesting thing is that the two can be used in combination, such as: #{ '${}' } , pay attention to the single quotation marks, pay attention that it cannot be reversed, the test is as follows.

Add a line to the application.properties file: media=jdbc.url

The ValueDemo class is modified as follows:

 
 1 package cn.larry.spring;
 2 
 3 import javax.annotation.PostConstruct;
 4 
 5 import org.springframework.beans.factory.annotation.Value;
 6 import org.springframework.stereotype.Component;
 7 
 8 @Component
 9 public class ValueDemo {
10     @Value("${jdbc.driverClass}")
11     private String driver;
12     
13     @Value("#{anotherObj.name}")
14     private String name;
15     
16 //    @Value("${ '#{anotherObj.media}' }") //这个不支持。
17     @Value("#{ '${media}' }")
18     private String media;
19     
20     @PostConstruct
21     public void run(){
22         System.out.println(driver);
23         System.out.println(name);
24         System.out.println(media);
25     }
26     
27 }
 

9. Execute the Spring Boot project and the results are as follows:

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326443552&siteId=291194637