国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

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

瀏覽:54日期: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
相關文章:
主站蜘蛛池模板: 一区二区三区高清视频在线观看 | 韩国porno xxxx | 久久一本色系列综合色 | 久久中文字幕免费视频 | 亚洲一级毛片免费看 | 黄色一级毛片 | 欧美成人精品 | 国产精品成人免费视频不卡 | 国产图片亚洲精品一区 | 天堂在线视频网站 | 俄罗斯小屁孩cao大人免费 | 亚洲另类激情综合偷自拍 | 国产亚洲人成a在线v网站 | 国产微拍精品福利视频 | 在线毛片观看 | 一级毛片在线免费视频 | 激情6月丁香婷婷色综合 | 九草网| 夜色精品国产一区二区 | 欧美特级视频 | 亚洲高清视频免费 | 亚洲一区二区三区视频 | 国产一区曰韩二区欧美三区 | 午夜美女网站 | 成人黄色一级视频 | 久久久久国产一级毛片高清片 | 久草资源免费 | 国内高清久久久久久久久 | 日本一级看片免费播放 | 在线观看国产一级强片 | 国产成人一区二区三区视频免费 | 国内精品小视频在线 | 777444欧美影视| 日本一级毛片片在线播放 | 操12p | 奇米第四狠狠777高清秒播 | a毛片免费全部播放毛 | 在线欧美精品一区二区三区 | 在线视频久久 | 国产欧美一区二区三区免费 | 成人三级精品视频在线观看 |