Spring EL表示式的運(yùn)用@Value說(shuō)明
Spring EL表達(dá)式語(yǔ)言,支持在XML和注解中表達(dá)式,類是于JSP的EL表達(dá)式語(yǔ)言。
在Spring開(kāi)發(fā)中經(jīng)常涉及調(diào)用各種資源的情況,包含普通文件、網(wǎng)址、配置文件、系統(tǒng)環(huán)境變量等,我們可以使用Spring的表達(dá)式語(yǔ)言實(shí)現(xiàn)資源的注入。
Spring主要在注解@value的參數(shù)中使用表達(dá)式。
本事咧演示一下情況:
注入普通字符串
注入操作系統(tǒng)屬性
注入表達(dá)式運(yùn)算結(jié)果
注入其他Bean的屬性
注入文件內(nèi)容
注入網(wǎng)址內(nèi)容
注入屬性文件(注意:用的是$符號(hào))
配置文件test.properties:
book.author=wangyunfei
book.name=spring boot
測(cè)試文件test.text:
你好!Spring boot
注入類:
@Configuration // 聲明當(dāng)前類是一個(gè)配置類,相當(dāng)于Spring配置的XML文件 // 包掃描,并排除了對(duì)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}")// 注入配置文件【注意是$符號(hào)】 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; } } }
測(cè)試類:
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&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai</jdbc-url> <lcn-log-url>jdbc:mysql://127.0.0.1:3306/tx-manager?allowMultiQueries=true&useUnicode=true&characterEncoding=utf-8&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說(shuō)明就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
說(shuō)明Java的傳遞與回調(diào)機(jī)制的代碼示例分享
這篇文章主要介紹了說(shuō)明Java的傳遞與回調(diào)機(jī)制的代碼示例分享,傳遞與回調(diào)機(jī)制是Java入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09Spring AOP實(shí)現(xiàn)復(fù)雜的日志記錄操作(自定義注解)
Spring AOP實(shí)現(xiàn)復(fù)雜的日志記錄操作(自定義注解),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Spring+Quartz配置定時(shí)任務(wù)實(shí)現(xiàn)代碼
這篇文章主要介紹了Spring+Quartz配置定時(shí)任務(wù)實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04詳解Java如何判斷ResultSet結(jié)果集是否為空
ResultSet 表示 select 語(yǔ)句的查詢結(jié)果集。這篇文章主要為大家詳細(xì)介紹了Java如何判斷ResultSet結(jié)果集是否為空,感興趣的可以了解一下2023-02-02spring?boot入門之誕生背景及優(yōu)勢(shì)影響
這篇文章主要為大家描述說(shuō)明了介紹了spring?boot誕生的背景以及其產(chǎn)生的優(yōu)勢(shì)影響,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-03-03SpringBoot?AOP?@Pointcut切入點(diǎn)表達(dá)式排除某些類方式
這篇文章主要介紹了SpringBoot?AOP?@Pointcut切入點(diǎn)表達(dá)式排除某些類方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Jmeter參數(shù)化獲取序列數(shù)據(jù)實(shí)現(xiàn)過(guò)程
這篇文章主要介紹了Jmeter參數(shù)化獲取序列數(shù)據(jù)實(shí)現(xiàn)過(guò)程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07Java結(jié)合uniapp實(shí)現(xiàn)驗(yàn)證碼功能的示例詳解
UniApp 是一個(gè)基于 Vue.js 的跨平臺(tái)應(yīng)用開(kāi)發(fā)框架,允許開(kāi)發(fā)者使用統(tǒng)一的代碼庫(kù)來(lái)構(gòu)建多平臺(tái)應(yīng)用,這篇文章將給大家介紹Java結(jié)合uniapp實(shí)現(xiàn)驗(yàn)證碼功能,文中通過(guò)詳細(xì)的代碼示例講解的非常詳細(xì),需要的朋友可以參考下2024-07-07