@Value("#{}")與@Value("${}")
開發過程中,有些配置資訊是根據環境的不同而有不同的值。這個時候,我們需要獲取配置檔案中的值或者spring容器中的值,可以通過@value註解獲取相關的值。
@Value(“#{}”)
通過@value獲取springcontext容器中的值的資訊。
@Value(“#{}”) 表示SpEl表示式通常用來獲取bean的屬性,或者呼叫bean的某個方法。當然還有可以表示常量
如果我們想通過@value獲取spring容器中的值(包括bean和bean的屬性值),我們可以通過@value("#{bean名稱}")或者@value("#{bean名稱.屬性名}",該屬性要有setter方法)
@Value(“#{}”) SpEL表示式(
@Value(“#{}”) 表示SpEl表示式通常用來獲取bean的屬性,或者呼叫bean的某個方法。當然還有可以表示常量
@RestController @RequestMapping("/login") @Component public class LoginController { @Value("#{1}") private int number; //獲取數字 1 @Value("#{'Spring Expression Language'}") //獲取字串常量 private String str; @Value("#{dataSource.url}") //獲取bean的屬性 private String jdbcUrl; @Autowired private DataSourceTransactionManager transactionManager; @RequestMapping("login") public String login(String name,String password) throws FileNotFoundException{ System.out.println(number); System.out.println(str); System.out.println(jdbcUrl); return "login"; } } 12345678910111213141516171819202122232425
當bean通過@Value(#{“”}) 獲取其他bean的屬性,或者呼叫其他bean的方法時,只要該bean (Beab_A)能夠訪問到被呼叫的bean(Beab_B),即要麼Beab_A 和Beab_B在同一個容器中,或者Beab_B所在容器是Beab_A所在容器的父容器。
(拿我上面貼出來的程式碼為例在springMvc專案中,dataSource這個bean一般是在springContext.xml檔案中申明的,而loginController這個bean一般是在springMvc.xml檔案中申明的,雖然這兩個bean loginController和dataSource不在一個容器,但是loginController所在容器繼承了dataSource所在的容器,所以在loginController這個bean中通過@Value(“#{dataSource.url}”)能夠獲取到dataSource的url屬性)。
@Value(“${}”)
通過@value獲取properties檔案中的值的資訊。
用 @Value(“${xxxx}”)註解從配置檔案讀取值的用法
如果我們想通過@value獲取xx.properties配置檔案中的某個key-value對的值,可以通過@value("${key}")獲取其中的value值的資訊
https://blog.csdn.net/zengdeqing2012/article/details/50736119
https://blog.csdn.net/jiangyu1013/article/details/56285984
1.用法
從配置properties檔案中讀取init.password 的值。
@Value("${init.password}")
private String initPwd;
2 . 在spring的配置檔案中載入配置檔案dbconfig.properties :
<!-- 載入配置檔案 -->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="fileEncoding" value="UTF-8"/>
<property name="locations">
<list>
<value>classpath:dbconfig.properties</value>
</list>
</property>
</bean>
或這樣載入
<context:property-placeholder location="classpath:dbconfig.properties" />
或這樣載入
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="location">
<value>dbconfig.properties</value>
</property>
</bean>
3 . dbconfig.properties 檔案:
#MD5
password.algorithmName=md5
password.hashIterations=2
#initpwd
init.password=admin
通過@Value(“${}”) 可以獲取對應屬性檔案中定義的屬性值。
假如我有一個sys.properties檔案 裡面規定了一組值: web.view.prefix =/WEB-INF/views/
在springMvc.xml檔案中引入下面的程式碼可以在該容器內通過@Value(“web.view.prefix")
獲取這個字串。
<!-- 載入配置屬性檔案 -->
<context:property-placeholder
ignore-unresolvable="true" location="classpath:sys.properties" />
然後再controller檔案中通過下面程式碼即可獲取/WEB-INF/views/
這個字串
@Value("${web.view.prefix}")
private String prefix;
需要指出的是,如果只在springMvc.xml引入下面程式碼,只能在springMvc.xml檔案中掃描或者註冊的bean中才能通過@Value("web.view.prefix")
獲取這個字串。其他未在springMvc.xml掃描和定義的bean必須在相應的xml檔案中引入下面程式碼才能使用@Value(“${}”)
表示式