Search code examples
springspring-securityspring-el

Can i use ${<exp_string>} instead of #{<exp_string>} in SpEL in Spring 3.0.x?


I am using SpEL in my xml configuration file. By mistake i used ${} instead of #{}, but it is working. can i replace # with $ ? Here is my code

config-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    <context:component-scan base-package="com.balancesheetreconcile" />
    <context:property-placeholder
        location="classpath:database.properties classpath:mail-configuration.properties" />
    <mvc:resources location="/resources/" mapping="/resources/**" />
    <mvc:annotation-driven />
    <tx:annotation-driven proxy-target-class="true" />
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver"
        p:prefix="/WEB-INF/views/" p:suffix=".jsp">
    </bean>
    <bean id="dateEditor"
        class="org.springframework.beans.propertyeditors.CustomDateEditor">

        <constructor-arg>
            <bean class="java.text.SimpleDateFormat">
                <constructor-arg value="dd-MM-yyyy" />
            </bean>
        </constructor-arg>
        <constructor-arg value="false" />
    </bean>
    <bean class="org.springframework.beans.factory.config.CustomEditorConfigurer">
        <property name="customEditors">
            <map>
                <entry key="java.util.Date">
                    <ref bean="dateEditor" />
                </entry>
            </map>
        </property>
    </bean>

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="${jdbc.driverClassName}" p:username="${jdbc.username}"
        p:password="${jdbc.password}" p:url="${jdbc.url}" />

    <bean id="dataSourceSessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
            </props>
        </property>
        <property name="packagesToScan">
            <list>
                <value>com.balancesheetreconcile.entities</value>
            </list>
        </property>
    </bean>
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />

    <bean id="transactionManager"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager"
        p:sessionFactory-ref="dataSourceSessionFactory" />

    <bean id="taskScheduler" class="com.balancesheetreconcile.utilities.TaskScheduler"
        init-method="scheduleTask" />

    <!-- Mail Sender bean -->

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${host}" />
        <property name="port" value="${port}" />
        <property name="username" value="${sender_username}" />
        <property name="password" value="${sender_password}" />
        <property name="javaMailProperties">
        <props>
                <prop key="${smtp_auth}">true</prop>
                <prop key="${enable_ssl_tls}">true</prop>
                <prop key="${mailing_protocol}">smtp</prop>
                <prop key="${debug}">true</prop>
            </props>
        </property>
        </bean>
</beans>

I am still confused regarding this. please help .Thanks in Advance


Solution

  • ${...} are resolved by the properties placeholder from mail-configuration.properties.

    That is entirely different to SpEL which evaluates expressions (usually not just simple properties) during context initialization.

    For example #{someBean.foo} will get the value by calling getFoo() on someBean, and

    #{SystemProperties['bar']} will use the value from the bar system property

    (java ... -Dbar=baz).