spring鍩轰簬@Value缁戝畾灞濨ean鎬уけ
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring鍩轰簬@Value缁戝畾灞濨ean鎬уけ相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/xsd' title='xsd'>xsd str admin 閫昏緫 tco function 姝e垯琛ㄨ揪寮忥細 ota class
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 30 31 32 33 34
| package com.laowang.annotationBase; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.ImportResource; import org.springframework.stereotype.Repository; ("dao") @ImportResource("classpath*:jdbc.properties") public class { @Value("${url}") private String url; @Value("${name}") private String name; @Value("${password}") private String password; public void save(){ System.out.println("jsr saving..."); System.out.println("url: "+url); System.out.println("name: "+name); System.out.println("password: "+password); } public void init(){ System.out.println("init jsrDAO Bean ..."); } public void destroy(){ System.out.println("destroy jsrDAO Bean ..."); } }
|
璧勬簮鏂囦欢鍐呭
url=http://localhost:3306/database
name=root
password=root
JavaConfig绫伙紝Bean鐨勮閰嶅伐浣?/strong>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.laowang.annotationBase; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; import org.springframework.test.context.ContextConfiguration; @Configuration public class JavaConfig { @Bean(name="dao", initMethod="init", destroyMethod="destroy") public JsrDAO getJseDAO(){ return new JsrDAO(); } }
|
娴嬭瘯绫?/strong>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| package com.laowang.annotationBase; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=JavaConfig.class) public class TestA { @Autowired private JsrDAO jsDao; @Test public void testDAO(){ jsDao.save(); }
|
缁撴灉鑾峰緱鏁版嵁鏄剧ず涓?/p>
init jsrDAO Bean ...
jsr saving...
url: ${url}
name: ${name}
password: ${password}
destroy jsrDAO Bean ...
闂鍑虹幇鍦ㄧ敤SpEL鍗犱綅绗︾粦瀹歜ean鐨勪笁涓睘鎬ф病鏈夋垚鍔燂紝姝g‘鐨勭粨鏋滃簲璇ユ槸杩欐牱鐨?/p>
init jsrDAO Bean ...
jsr saving...
url: http://localhost:3306/database
name: root
password: root
destroy jsrDAO Bean ...
闂鍑虹幇鍦ㄥ摢閲屽憿锛熸寜鐞嗚搴旇涓嶄細閿欑殑銆備簬鏄櫨搴︺€佽胺姝屾悳绱紝閮芥病瑙e喅锛屾渶鍚庡幓浜唖pring瀹樻柟鏂囨。鐪嬪埌姝g‘浣跨敤鏂瑰紡
鎵€浠ュ彧闇€瑕佷慨鏀笿avaConfig鏂囦欢灏卞彲浠ヤ簡锛屾坊鍔犱竴琛屾敞瑙o紝鍙樻垚濡備笅锛?/p>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package com.laowang.annotationBase; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration @ImportResource("classpath*:spring-annotationBase.xml") public class JavaConfig { @Bean(name="dao", initMethod="init", destroyMethod="destroy") public JsrDAO getJseDAO(){ return new JsrDAO(); } @Bean public JsrService getJsrService(){ JsrService service =new JsrService(); service.setJsrDAO(getJseDAO()); return service; } }
|
鍚屾椂鍐嶆坊鍔犱竴涓搴旂殑xml閰嶇疆鏂囦欢锛屾寚瀹氬睘鎬ч厤缃枃浠剁殑浣嶇疆锛屾鏃禞srDAo涓睘鎬ф枃浠朵綅缃殑鎸囧畾涔熷彲浠ヤ笉鐢ㄤ簡銆?br/>涓嬮潰鏄搴旂殑xml鏂囦欢
1 2 3 4 5 6 7 8 9 10 11 12
| <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:/properties/jdbc.properties"/> </beans>
|
1
| env.getProperty("jdbc.name");
|
鍏蜂綋浣跨敤瑕佸弬鑰冩枃妗?/p>
瑙f瀽灞炴€у崰浣嶇涓€鑸娇鐢⊿pEL璇█
SpEL璇█鐗规€у涓嬶細
- 浣跨敤bean鐨処D鏉ュ紩鐢╞ean
- 璋冪敤鏂规硶鍜岃闂璞$殑灞炴€?/li>
- 瀵瑰€艰繘琛岀畻鏈€佸叧绯诲拰閫昏緫杩愮畻
- 姝e垯琛ㄨ揪寮忓尮閰?/li>
- 闆嗗悎鎿嶄綔
璇?/em>
鈥?{鈥鈥?======琛ㄨ揪寮?br/>鈥?{鈥鈥?======鍗犱綅绗?/p>
瀛楅潰甯搁噺
鈥?{1}鈥?======璁$畻缁撴灉涓?
寮曠敤bean銆佸睘鎬у拰鏂规硶
鈥?{bean}鈥?======寮曠敤bean
鈥?{bean.attr}鈥?======璁$畻寰楀埌锛堝紩鐢級bean鐨勫睘鎬?br/>鈥?{systemProperties[鈥榡dbc.name鈥橾}鈥?======閫氳繃systemProperties瀵硅薄寮曠敤绯荤粺灞炴€э紙涓汉鐞嗚В涓哄紩鐢ㄥ舰鍚屽姞杞介厤缃枃浠朵腑鐨勫睘鎬х殑鍊硷級
鈥?{bean.method()}鈥?======璋冪敤bean鐨勬柟娉?br/>鈥?{bean.method()?.toUpperCase()}鈥?======鏂规硶杩斿洖涓嶄负绌烘椂缁х画璋冪敤toUpperCase()锛屽惁鍒欎笉璋冪敤
鍦ㄨ〃杈惧紡涓娇鐢ㄧ被鍨?/strong>
鈥?{T(java.lang.Math).random}鈥?======鍙兘璋冪敤绫荤殑闈欐€佹柟娉曞拰甯搁噺
SpEL 杩愮畻绗?/strong>
涓嶈浜?/em>
璁$畻姝e垯琛ㄨ揪寮忥細鏂囨湰锛坢atches锛夋鍒欒〃杈惧紡
鈥?{admin.email matches 鈥榌a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.com鈥檥鈥?======閭欢楠岃瘉
璁$畻闆嗗悎
鈥?{bean.list[2].attr}鈥?======鑾峰彇bean涓粨鍚坙ist绗?涓厓绱犵殑attr灞炴€?/p>
SpEL 鍏朵粬杩愮畻绗?/em>
- 锛??[]锛夊闆嗗悎杩涜杩囨护銆俒]閲岄潰鏄〃杈惧紡
鈥?{bean.list.?[attr eq 鈥榣aowang鈥橾}鈥?/li>
- .^[] 闆嗗悎涓煡璇㈢涓€涓尮閰嶉」
- .$[] 闆嗗悎涓煡璇㈡渶鍚庝竴涓尮閰嶉」
以上是关于spring鍩轰簬@Value缁戝畾灞濨ean鎬уけ的主要内容,如果未能解决你的问题,请参考以下文章
鍒嗗竷寮忎簨鍔′箣濡備綍鍩轰簬RocketMQ鐨勪簨鍔℃秷鎭壒鎬у疄鐜板垎甯冨紡绯荤粺鐨勬渶缁堜竴鑷存€э紵
绗崄绡囷細缁戝畾鏂规硶涓庨潪缁戝畾鏂规硶
docker缁戝畾鎸傝浇鐩綍
tomcat缁戝畾涓や釜https鍩熷悕
姣曚笟璁捐涔?--- 鍩轰簬Spring Boot鐨勫湪绾块煶涔恮eb绯荤粺
Windows Presentation Foundation(WPF)涓殑鏁版嵁缁戝畾锛堜娇鐢╔mlDataProvider浣滄帶浠剁粦瀹氾級