av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Spring EL表示式的運用@Value說明

瀏覽:73日期:2023-09-08 13:01:06

Spring EL表達式語言,支持在XML和注解中表達式,類是于JSP的EL表達式語言。

在Spring開發中經常涉及調用各種資源的情況,包含普通文件、網址、配置文件、系統環境變量等,我們可以使用Spring的表達式語言實現資源的注入。

Spring主要在注解@value的參數中使用表達式。

本事咧演示一下情況:

注入普通字符串注入操作系統屬性注入表達式運算結果注入其他Bean的屬性注入文件內容注入網址內容注入屬性文件(注意:用的是$符號)

配置文件test.properties:

book.author=wangyunfeibook.name=spring boot

測試文件test.text:

你好!Spring boot

注入類:

@Configuration // 聲明當前類是一個配置類,相當于Spring配置的XML文件// 包掃描,并排除了對BeanConfig的掃描@ComponentScan(basePackages={'com.chenfeng.xiaolyuh'}, excludeFilters={@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value={BeanConfig.class, AopConfig.class})})@PropertySource('classpath:test.properties')// 指定文件地址public class ELConfig { @Value('注入普通字符串')// 注入普通字符串 private String normal; @Value('#{systemProperties[’os.name’]}')// 注入操作系統屬性 private String osName; @Value('#{T(java.lang.Math).random() * 100.0 }')// 注入表達式結果 private double randomNumber; @Value('#{demoELService.another}')// 注入其他Bean屬性 private String fromAnother; @Value('classpath:test.txt')// 注入文件資源 private Resource testFile; @Value('https://www.baidu.com')// 注入網址資源 private Resource testUrl; @Value('${book.name}')// 注入配置文件【注意是$符號】 private String bookName; @Autowired// Properties可以從Environment獲得 private Environment environment; // @Bean// public static PropertySourcesPlaceholderConfigurer propertyConfigure() {// return new PropertySourcesPlaceholderConfigurer();// } @Override public String toString() { try { return 'ELConfig [normal=' + normal + ', osName=' + osName + ', randomNumber=' + randomNumber + ', fromAnother=' + fromAnother + ', testFile=' + IOUtils.toString(testFile.getInputStream()) + ', testUrl=' + IOUtils.toString(testUrl.getInputStream()) + ', bookName=' + bookName + ', environment=' + environment.getProperty('book.name') + ']'; } catch (IOException e) { e.printStackTrace(); return null; } } }

測試類:

public class SpringELTest { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ELConfig.class); @Test public void contextTest() { ELConfig elConfig = context.getBean(ELConfig.class); System.out.println(elConfig.toString()); } @After public void closeContext() { context.close(); } }

補充知識:yml、properties獲取pom自定義變量

pom變量:

<profiles> <profile> <!-- 本地環境 --> <id>dev</id> <properties> <profiles.env>dev</profiles.env> <jdbc-url>jdbc:mysql://127.0.0.1:3306/melab?allowMultiQueries=true&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai</jdbc-url> <lcn-log-url>jdbc:mysql://127.0.0.1:3306/tx-manager?allowMultiQueries=true&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai</lcn-log-url> <jdbc-user>root</jdbc-user> <jdbc-password>123456</jdbc-password> </properties> </profile></profiles>

yml獲取pom變量:

添加依賴:

<!-- https://mvnrepository.com/artifact/org.yaml/snakeyaml --><dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>1.25</version></dependency>

獲取變量:

url: @jdbc-url@lcn-log-url: @jdbc-url@username: @jdbc-user@password: @jdbc-password@properties獲取pom變量:

build設置:

<build> <!--properties解析pom--> <pluginManagement> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <configuration> <encoding>utf-8</encoding> <useDefaultDelimiters>true</useDefaultDelimiters> </configuration> </plugin> </plugins> </pluginManagement></build>

獲取變量:

spring.datasource.url=${jdbc-url}spring.datasource.username=${jdbc-user}spring.datasource.password=${jdbc-password}

以上這篇Spring EL表示式的運用@Value說明就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 天天天久久久 | 狠狠操网站 | 九九热在线免费视频 | 精品欧美 | 亚洲精品一区中文字幕乱码 | 国产精品精品久久久 | 亚洲视频在线看 | 99久久精品免费看国产四区 | 国产精品电影网 | 欧美老妇交乱视频 | 天天狠狠 | 超碰在线播 | 亚洲精品一区二三区不卡 | 国产电影精品久久 | 精品亚洲一区二区 | 欧美一二三四成人免费视频 | 国产日韩精品视频 | 在线观看中文字幕 | 久久成人午夜 | 中国美女一级黄色片 | 在线视频a| 青青草精品视频 | 91精品国产综合久久国产大片 | 黑人巨大精品欧美一区二区免费 | 黄色片a级 | 国产精品美女久久久久久免费 | 91久久精品一区二区三区 | 久久精品中文 | 天堂综合网 | 亚洲男人天堂 | 精品亚洲一区二区 | 高清国产一区二区 | 一区二区三区电影网 | 成人黄色电影在线播放 | 老牛嫩草一区二区三区av | 成人在线看片 | 亚洲一区 | 成年视频在线观看 | 亚洲一区二区三区在线 | av一级| 一区二区三区中文 |