spring xml properties split with comma for list
在注入spring bean 属性值的时候常常会用到list, 一般使用方式例如以下:
<bean id="testBean" class="com.mytest.TestBean">
<property name="myList">
<list>
<value> 1 </value>
<value> 2 </value>
</list>
</properties>
</bean>
可是假设将这个list的值通过el表达式从properties文件里获取,怎样做到,properties中存储的是键值对的数据。是不能正确存储列表数据的。仅仅能使用字符串切割成数组、列表的形式。至于字符串的切割能够自己来切割也能够使用现有的spEL。
1. properties 中的配置(逗号切割的字符串)
2. bean的list属性
3. spring xml bean 注冊 属性值
4. bean属性的setter 获取到了我们想要的结果
-------------------------------------------------
先不要急着用,其它spring还提供了更好用的InlineList, 即以大括号计算, 如 "{'a','b','c'}" , 所以还能够这样配置
xx.properties
imageDomains={'ezhe.com','gozap.com','chouti.com'}
spring-service.xml
<bean id="hostsService" class="com.gozap.ezhe.service.impl.HostsServiceImpl"> <property name="imageDomains" value="#{${imageDomains}}" /> </bean>
-------------------------------------------------
org.springframework.expression.spel.standard.TokenKind
// ordered by priority - operands first LITERAL_INT, LITERAL_LONG, LITERAL_HEXINT, LITERAL_HEXLONG, LITERAL_STRING, LITERAL_REAL, LITERAL_REAL_FLOAT, LPAREN("("), RPAREN(")"), COMMA(","), IDENTIFIER, COLON(":"),HASH("#"),RSQUARE("]"), LSQUARE("["), LCURLY("{"),RCURLY("}"), DOT("."), PLUS("+"), STAR("*"), MINUS("-"), SELECT_FIRST("^["), SELECT_LAST("$["), QMARK("?"), PROJECT("!["), DIV("/"), GE(">="), GT(">"), LE("<="), LT("<"), EQ("=="), NE("!="), MOD("%"), NOT("!"), ASSIGN("="), INSTANCEOF("instanceof"), MATCHES("matches"), BETWEEN("between"), SELECT("?["), POWER("^"), ELVIS("?:"), SAFE_NAVI("?."), BEAN_REF("@"), SYMBOLIC_OR("||"), SYMBOLIC_AND("&&"), INC("++"), DEC("--")
这里能够看到有个BEAN_REF("@") 这个就是 spEL引用bean
<bean id="hostsService" class="com.gozap.ezhe.service.impl.HostsServiceImpl"> <property name="imageDomains" value="#{${imageDomains}}" /> <property name="daos" value="#{ {@demoDao, @adminDao}}" /> </bean>
--------------------------------------------------------------------------------------------------------------------------------------
以下简要分析一下 spEL 解析
整体思路就是: 使用字符串切割 将 目标对象(target)、运算符、函数、參数等,假设是函数在getValue时使用反射机制运行,并获取结果
相关文章
- Spring学习笔记(十八)——spring日志框架的配置和使用
- Spring AOP面向切面编程
- Spring Cloud 微服务项目实战 -
- Spring AOP 的实现机制
- spring boot activiti7常见问题「建议收藏」
- List<类型1>转成List<类型2>的LIst类型转换工具类
- Spring Security OAuth2.0实现
- Spring Boot中使用MongoDB数据库的方法
- list java中List对象通用排序算法详解编程语言
- spring的AOP(五)—-Spring AOP 编程(AspectJ )详解编程语言
- 重磅:Spring Boot 2.0 正式发布!详解编程语言
- Spring Boot(十三):spring boot小技巧详解编程语言
- Spring boot+vue前后端开发学习笔记2详解编程语言
- Spring整合Redis简单实现高效缓存(spring集成redis)
- List头文件助力Linux内核开发(list.hlinux)
- Spring Boot 2.x :通过 spring-boot-starter-hbase 集成 HBase
- Redis如何清空List:技巧分享(redis清空list)
- 利用Redis List对象提升系统性能(redis list对象)
- Oracle数据库操作利用入参List实现批量处理(oracle入参list)
- 使用Redis轻松获取List数据(获取redis中list)
- Redis灵活的List储存功能(redis能储存list)