Spring实战之Bean定义中的SpEL表达式语言支持操作示例
作者:cakincqm 时间:2021-07-07 10:46:19
本文实例讲述了Spring实战之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()的参数时,在表达式中调用方法
配置setAxe()的参数时,在表达式中创建对象
配置调用setBooks()的参数时,在表达式中访问其他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企业应用实战》, 《疯狂Java讲义》]
我是0.06178107142599454,正在砍柴
钢斧砍柴很快!
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/chengqiuming/article/details/101157904
标签:Spring,Bean,SpEL,表达式
0
投稿
猜你喜欢
详解JAVA流程控制语句
2023-11-05 02:27:09
spring boot实战之本地jar包引用示例
2021-11-01 20:44:45
C#判断系统是32位还是64位的方法
2022-04-01 12:44:04
java中BigDecimal和0比较的示例代码
2022-07-05 04:58:18
Spring Boot 启动加载数据 CommandLineRunner的使用
2021-06-17 12:52:21
C语言程序设计50例(经典收藏)
2023-07-10 08:33:19
C# 线程同步详解
2021-12-30 04:50:03
Java基础之并发相关知识总结
2022-04-24 13:59:05
spring boot 使用profile来分区配置的操作
2022-11-27 22:55:15
android绘制圆形图片的两种方式示例
2021-10-11 13:17:28
SpringMVC拦截器创建配置及执行顺序
2023-06-06 20:41:16
OpenGL实现Bezier曲线的方法示例
2023-06-30 05:06:22
SpringBoot+Elasticsearch实现数据搜索的方法详解
2023-04-17 02:45:46
Springboot多种情况yml配置代码实例
2022-05-14 23:26:00
C#在子线程中更新窗口部件的写法
2022-04-01 09:57:50
说说在Spring中如何引用外部属性文件的方法
2023-09-18 21:47:25
Java设计模式中的命令模式
2023-11-20 04:26:46
Android使用Canvas对象实现刮刮乐效果
2021-11-27 02:53:36
JAVA 中Spring的@Async用法总结
2023-11-28 16:35:58
Java编程实现对十六进制字符串异或运算代码示例
2023-11-06 15:58:15