Spring EL表达式

本文介绍了如何使用Spring EL表达式来读取properties文件中的值,并展示了如何在spring.xml中配置<context:property-placeholder>来实现这一目标。此外,还对比了mybatis使用的OGNL表达式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

除了我们常见的JSP EL表达式,还有一种Spring EL表达式,通过EL表达式读取properties文件中的值。

原理:在spring.xml中对.properties文件进行了扫描,

<context:property-placeholder location="***.properties"/>
//location属性中如果是在类路径下,需要写上 classpath:

//例如:<context:property-placeholder location="classpath:mybatis/db.properties"/>

详述:

spring将properties文件读取后在配置文件中直接将对象的配置信息填充到bean中的变量里。

原本使用PropertyPlaceholderConfigurer类进行文件信息配置。PropertyPlaceholderConfigurer实现了BeanFactoryPostProcessor接口,能够对<bean/>中的属性值进行外在化管理。

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">    
        <list>      
            <value>userinfo.properties</value>    
        </list>  
    </property>
</bean>
<bean name="userInfo" class="test.UserInfo">  
    <property name="username" value="${db.username}"/>  
    <property name="password" value="${db.password}"/>
</bean>

现在,直接使用
<context:property-placeholder location="classpath*:resources/*.properties" />

注意:spring容器中最多只能定义一个context:property-placeholder。

 

mybatis没有使用EL表达式,而是使用了OGNL!!

详见:https://blog.youkuaiyun.com/l153097889/article/details/52464548

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值