Spring實(shí)戰(zhàn)之Bean定義中的SpEL表達(dá)式語(yǔ)言支持操作示例
本文實(shí)例講述了Spring實(shí)戰(zhàn)之Bean定義中的SpEL表達(dá)式語(yǔ)言支持操作。分享給大家供大家參考,具體如下:
一 配置
<?xml version="1.0" encoding="GBK"?> <!-- 指定Spring配置文件的根元素和Schema 導(dǎo)入p:命名空間和util:命名空間的元素 --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd"> <!-- 使用util.properties加載指定資源文件 --> <util:properties id="confTest" location="classpath:test_zh_CN.properties"/> <!-- 配置setName()的參數(shù)時(shí),在表達(dá)式中調(diào)用方法 配置setAxe()的參數(shù)時(shí),在表達(dá)式中創(chuàng)建對(duì)象 配置調(diào)用setBooks()的參數(shù)時(shí),在表達(dá)式中訪問(wèn)其他Bean的屬性 --> <bean id="author" class="org.crazyit.app.service.impl.Author" p:name="#{T(java.lang.Math).random()}" p:axe="#{new org.crazyit.app.service.impl.SteelAxe()}" p:books="#{ {confTest.a , confTest.b} }"/> </beans>
二 資源文件
a=\u300a\u8f7b\u91cf\u7ea7Java EE\u4f01\u4e1a\u5e94\u7528\u5b9e\u6218\u300b b=\u300a\u75af\u72c2Java\u8bb2\u4e49\u300b
三 接口
Axe
package org.crazyit.app.service; public interface Axe { String chop(); }
Person
package org.crazyit.app.service; import java.util.*; public interface Person { public void useAxe(); public List<String> getBooks(); public String getName(); }
四 Bean
Author
package org.crazyit.app.service.impl; import java.util.*; import org.crazyit.app.service.*; public class Author implements Person { private Integer id; private String name; private List<String> books; private Axe axe; // id的setter和getter方法 public void setId(Integer id) { this.id = id; } public Integer getId() { return this.id; } // name的setter和getter方法 public void setName(String name) { this.name = name; } public String getName() { return this.name; } // books的setter和getter方法 public void setBooks(List<String> books) { this.books = books; } public List<String> getBooks() { return this.books; } // axe的setter方法 public void setAxe(Axe axe) { this.axe = axe; } public void useAxe() { System.out.println("我是" + name + ",正在砍柴\n" + axe.chop()); } }
SteelAxe
package org.crazyit.app.service.impl; import org.crazyit.app.service.*; public class SteelAxe implements Axe { public String chop() { return "鋼斧砍柴很快!"; } }
五 測(cè)試類
package lee; import org.springframework.context.*; import org.springframework.context.support.*; import java.util.*; import org.crazyit.app.service.*; public class SpELTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); Person author = ctx.getBean("author" , Person.class); System.out.println(author.getBooks()); author.useAxe(); } }
六 測(cè)試結(jié)果
[《輕量級(jí)Java EE企業(yè)應(yīng)用實(shí)戰(zhàn)》, 《瘋狂Java講義》]
我是0.06178107142599454,正在砍柴
鋼斧砍柴很快!
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Spring中@Value使用詳解及SPEL表達(dá)式
- 詳解Spring中Spel表達(dá)式和el表達(dá)式的區(qū)別
- SpringDataElasticsearch與SpEL表達(dá)式實(shí)現(xiàn)ES動(dòng)態(tài)索引
- Spring AOP如何在注解上使用SPEL表達(dá)式注入對(duì)象
- spring之SpEL表達(dá)式詳解
- 使用Springboot自定義注解,支持SPEL表達(dá)式
- 基于spring?@Cacheable?注解的spel表達(dá)式解析執(zhí)行邏輯
- Spring?Cache抽象-使用SpEL表達(dá)式解析
- Spring組件開(kāi)發(fā)模式支持SPEL表達(dá)式
- Spring spel表達(dá)式使用方法示例
- Spring中SpEL表達(dá)式的使用全解
相關(guān)文章
Hibernate迫切連接和普通連接的區(qū)別實(shí)例詳解
這篇文章主要介紹了Hibernate迫切連接和普通連接的區(qū)別實(shí)例詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12Java中關(guān)于泛型、包裝類及ArrayList的詳細(xì)教程
泛型可以在類或方法中預(yù)支地使用未知的類型。這篇文章主要介紹了Java中關(guān)于泛型、包裝類及ArrayList的詳細(xì)教程,需要的朋友可以參考下2021-12-12Java實(shí)現(xiàn)簡(jiǎn)單棋盤(pán)存檔和讀取功能
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單棋盤(pán)存檔和讀取功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09SpringBoot集成MyBatisPlus+MySQL的實(shí)現(xiàn)
MybatisPlus是國(guó)產(chǎn)的第三方插件, 它封裝了許多常用的CURDapi,免去了我們寫(xiě)mapper.xml的重復(fù)勞動(dòng),本文主要介紹了SpringBoot集成MyBatisPlus+MySQL的實(shí)現(xiàn),感興趣的可以了解一下2023-10-10springBoot項(xiàng)目中的static和templates文件夾的使用
本文主要介紹了springBoot項(xiàng)目中的static和templates文件夾的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07Java多線程 Guarded Suspension設(shè)計(jì)模式
這篇文章主要介紹了Java多線程 Guarded Suspension設(shè)計(jì)模式,Guarded Suspension意為保護(hù)暫停,其核心思想是僅當(dāng)服務(wù)進(jìn)程準(zhǔn)備好時(shí),才提供服務(wù),文章圍繞Java多線程 Guarded Suspension展開(kāi)內(nèi)容,需要的朋友可以參考一下2021-10-10SpringBoot中@Transiactional注解沒(méi)有效果的解決
這篇文章主要介紹了SpringBoot中@Transiactional注解沒(méi)有效果的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08Java Swing組件單選框JRadioButton用法示例
這篇文章主要介紹了Java Swing組件單選框JRadioButton用法,結(jié)合具體實(shí)例形式分析了Swing單選框JRadioButton的使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2017-11-11