【问题标题】:How to get SpEL (spring-el) default to null expression working in the Spring XML?如何让 SpEL (spring-el) 默认为空表达式在 Spring XML 中工作?
【发布时间】:2018-02-27 16:49:37
【问题描述】:

期望的结果:

如果没有找到属性键,或者值为空白默认为null。
为了在钱包和直接登录环境中工作,我需要它可以为空或可以为空。

问题:

我无法正确解析 SpEL 默认为空的表达式。
但是,根据文档,它应该适用于 Spring 3.0。
我怀疑我在 XML 或依赖项中遗漏了一些东西,但我一生都无法弄清楚它是什么。

注意:

SpEL 的替代方法是可以接受的,但我不想全局允许所有属性为空。 (不想设置ignoreUnresolvablePlaceholders=true)

尝试的修复:

  1. 在 bean 定义中添加了 springframework.org/schema/context。 没有影响
  2. 尝试将初始的$ 替换为#,但这只是作为文字字符串读取。 (

Spring 版本: 3.0.7.RELEASE
Java 版本: 1.6(不要评判,不是我的选择)

例外:

2018-02-27 11:13:52,982  FATAL class BatchDriver 166 - Server(): Unexpected exception
org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'dataSource' defined in class path resource [databaseContext.xml]: Could not resolve placeholder 'jdbc.credu:#{null'
    at org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.processProperties(PropertyPlaceholderConfigurer.java:268)
    at org.springframework.beans.factory.config.PropertyResourceConfigurer.postProcessBeanFactory(PropertyResourceConfigurer.java:75)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:553)
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:527)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:362)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    ...

databaseContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"
    default-lazy-init="true">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" scope="singleton">
        <property name="locations">
            <list>
                <value>classpath:/jdbc.properties</value>
                <value>classpath:/javadriver.properties</value>
            </list>
        </property>
    </bean>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
        <property name="url"><value>${jdbc.url}</value></property>
        <property name="username" value="${jdbc.credu:#{null}}"/>
        <property name="password" value="${jdbc.credp:#{null}}"/>
        ~~~ Other Settings ~~~
    </bean>

jdbc.properties:

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver

jdbc.url=jdbc:oracle:oci:/@WALLET_ALIAS
#jdbc.url=jdbc:oracle:thin:@255.255.255.255:9999/db_name
#jdbc.credu=username
#jdbc.credp=password

【问题讨论】:

  • 尝试使用PropertySourcesPlaceholderConfigurer 而不是PropertyPlaceholderConfigurer。或者更好的是只依赖&lt;context:property-placeholder&gt;
  • PropertySourcesPlaceholderConfigurer 在 Spring 3.0 中不存在
  • 使用 没有区别。
  • OK-OK。我得到了它。我错了 - 同意。
  • 事实证明,根本原因是 Spring 2.0 的依赖关系搞砸了。更新到 3.0.7 后,

标签: java spring spring-el


【解决方案1】:

SpEL 在占位符之后解析;尝试类似

#{'${jdbc.credu:xxx}' == 'xxx' ? null : '${jdbc.credu:xxx}'}

编辑

3.0.7 是非常旧的版本;最高 4.3.x 的 Spring 版本适用于 java 6。

这至少适用于 3.1.4.RELEASE:

<bean id="foo" class="com.example.Foo">
    <property name="foo" value="#{'${foo:bar}' == 'bar' ? null : '${foo:bar}'}"/>
</bean>

和

public class So49013862Application {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("foo.xml");
        System.out.println(ctx.getBean("foo"));
        ctx.close();
    }

}

和

null
Foo [foo=null]

与

public class Foo {

    private String foo;

    public void setFoo(String foo) {
        System.out.println(foo);
        this.foo = foo;
    }

    @Override
    public String toString() {
        return "Foo [foo=" + this.foo + "]";
    }

}

我没有时间用 3.0.x 测试它。

【讨论】:

  • 这对我不起作用。当我使用
  • 原来根本问题实际上是一个流氓 spring 2.0 依赖项。
  • 一个建议:去掉三元运算最后一部分的单引号。
  • 那不行,因为SpEL会尝试解析属性占位符解析的结果;它必须是文字。
猜你喜欢
  • 1970-01-01
  • 2014-01-14
  • 1970-01-01
  • 1970-01-01
  • 2020-03-03
  • 1970-01-01
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多