溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Spring實戰(zhàn)之Bean定義中的SpEL表達式語言支持操作示例

發(fā)布時間:2020-09-22 16:50:08 來源:腳本之家 閱讀:161 作者:cakincqm 欄目:編程語言

本文實例講述了Spring實戰(zhàn)之Bean定義中的SpEL表達式語言支持操作。分享給大家供大家參考,具體如下:

一 配置

<?xml version="1.0" encoding="GBK"?>
<!-- 指定Spring配置文件的根元素和Schema
   導入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ù)時,在表達式中調用方法
   配置setAxe()的參數(shù)時,在表達式中創(chuàng)建對象
   配置調用setBooks()的參數(shù)時,在表達式中訪問其他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 "鋼斧砍柴很快!";
   }
}

五 測試類

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();
   }
}

六 測試結果

[《輕量級Java EE企業(yè)應用實戰(zhàn)》, 《瘋狂Java講義》]
我是0.06178107142599454,正在砍柴
鋼斧砍柴很快!

更多關于java相關內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進階教程》、《Java數(shù)據(jù)結構與算法教程》、《Java操作DOM節(jié)點技巧總結》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》

希望本文所述對大家java程序設計有所幫助。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI