@Value("#{}")与@Value("${}")的区别以及用法

 1 package com.ieou.capsule_basic_info.util;
 2 
 3 
 4 import org.springframework.beans.factory.annotation.Value;
 5 import org.springframework.stereotype.Component;
 6 import org.springframework.web.bind.annotation.RequestMapping;
 7 import org.springframework.web.bind.annotation.RestController;
 8 
 9 @RestController
10 @RequestMapping("/login")
11 @Component
12 public class TestController {
13 
14     /************************   @Value("#{}")   *********************************/
15 
16     //这是  @Value("#{}")
17 
18     @Value("#{1}")
19     private int number; //获取数字 1
20 
21     @Value("#{'Spring Expression Language'}") //获取字符串常量
22     private String str;
23 
24 
25     @Value("#{dataSource.jdbcUrl}") //获取bean的属性
26     private String jdbcUrl;
27 
28     /**************************   @Value("${}")  ***********************************/
29 
30     // @Value("${}") 有两种用法
31     //1.从properties配置文件中读取数据
32     @Value("${test_value}") //获取字符串常量
33     private String str1;  //str1 = hello world
34     
35     //2.为方法参数赋值
36     @Value("${test_value}")
37     public void test(String s){
38         System.out.println(s);  // s = hello world
39     }
40     
41     
42 } 

@Value("#{}")   SpEL表达式
@Value("#{}") 表示SpEl表达式通常用来获取bean的属性,或者调用bean的某个方法。当然还有可以表示常量

猜你喜欢

转载自www.cnblogs.com/wang-yaz/p/10567716.html
今日推荐