国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Spring EL表示式的運(yùn)用@Value說明

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

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

在Spring開發(fā)中經(jīng)常涉及調(diào)用各種資源的情況,包含普通文件、網(wǎng)址、配置文件、系統(tǒng)環(huán)境變量等,我們可以使用Spring的表達(dá)式語言實(shí)現(xiàn)資源的注入。

Spring主要在注解@value的參數(shù)中使用表達(dá)式。

本事咧演示一下情況:

注入普通字符串注入操作系統(tǒng)屬性注入表達(dá)式運(yùn)算結(jié)果注入其他Bean的屬性注入文件內(nèi)容注入網(wǎng)址內(nèi)容注入屬性文件(注意:用的是$符號)

配置文件test.properties:

book.author=wangyunfeibook.name=spring boot

測試文件test.text:

你好!Spring boot

注入類:

@Configuration // 聲明當(dāng)前類是一個(gè)配置類,相當(dāng)于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’]}')// 注入操作系統(tǒng)屬性 private String osName; @Value('#{T(java.lang.Math).random() * 100.0 }')// 注入表達(dá)式結(jié)果 private double randomNumber; @Value('#{demoELService.another}')// 注入其他Bean屬性 private String fromAnother; @Value('classpath:test.txt')// 注入文件資源 private Resource testFile; @Value('https://www.baidu.com')// 注入網(wǎng)址資源 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(); } }

補(bǔ)充知識(shí):yml、properties獲取pom自定義變量

pom變量:

<profiles> <profile> <!-- 本地環(huán)境 --> <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設(shè)置:

<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表示式的運(yùn)用@Value說明就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 师宗县| 措美县| 金乡县| 布尔津县| 常山县| 白玉县| 崇州市| 洱源县| 历史| 厦门市| 永新县| 大冶市| 湖南省| 唐河县| 沾化县| 赣州市| 五常市| 成安县| 吴堡县| 美姑县| 五莲县| 甘谷县| 清水河县| 公安县| 呼伦贝尔市| 沅江市| 遵义县| 五河县| 沁水县| 丹东市| 苍山县| 平谷区| 正蓝旗| 西藏| 定边县| 敦化市| 叶城县| 江安县| 集贤县| 安吉县| 通道|