当前位置: 首页 > 知识库问答 >
问题:

如何使SpEL(spring-el)默认为null表达式在Spring xml中工作?

史超英
2023-03-14

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

    null

Spring版本:3.0.7.发布
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)
    ...
<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.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

共有1个答案

鲍宁
2023-03-14

SpEL在占位符之后解析;试试像这样的事

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

编辑

3.0.7是非常旧版本;Spring版本直到4.3.x都可以使用Java6。

<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测试它。

 类似资料:
  • 主要内容:示例,SpEL对Bean定义的支持,SpEL中的运算符,SpEL中的变量Spring Expression Language(简称 SpEL)是一种功能强大的表达式语言,支持运行时查询和操作对象图 。表达式语言一般是用最简单的形式完成最主要的工作,以此减少工作量。 Java 有许多可用的表达式语言,例如 JSP EL,OGNL,MVEL 和 JBoss EL,SpEL 语法类似于 JSP EL,功能类似于 Struts2 中的 OGNL,能在运行时构建复杂表达式、存取

  • 主要内容:EL表达式的语法,EL算术运算符,EL比较运算符,EL逻辑运算符,EL其它运算符,EL运算符优先级,EL保留字,禁用EL表达式,EL内置对象之前的 JSP 页面中,我们经常使用 JSP 表达式来输出变量或者页面之间传递的参数,大大降低了页面的可读性。 为了简化 JSP 页面,JSP 2.0 新增了 EL(Expression Language)表达式语言。EL 提供了更为简洁、方便的形式来访问变量和参数,不仅可以简化 JSP 页面代码,还会使开发者的逻辑变得更加清晰 。 EL表达式的语

  • 问题 如何在groovy Spring Boot项目中定义Spring表达式语言(SpEL)?(每个Spring调度器crontab@调度注释) Spring Boot Groovy和Spring EL调度器 根据来自web的文档,我正在groovy spring boot应用程序(2.2.x)中使用spring scheduler 根据这篇文章:任务调度使用cron表达式从属性文件 试过这个 我

  • 问题内容: 与何时使用相比,我有点困惑。Spring的文档仅使用,但是有许多使用示例。此外,当我开始使用SpEL时,我被告知要使用它,并且效果很好。 对于那些感到困惑的人来说,我将如何使用它 和一些属性文件: 我的问题是: 有什么区别或相同? 一个版本被弃用了,所以我应该使用另一个版本吗? 问题答案: 是属性占位符语法。它只能用于取消引用属性。 是SpEL语法,它的功能和复杂性要高得多。它也可以处

  • 我试图设置可变投掷SpEL在Spring启动应用程序: 应该来自应用程序-{profile}。财产。但是字段始终为空,即使已存在。 带有的代码可以正常工作,但包含空字符串。 我的问题是,如果使用SpEL的属性不存在,如何将null设置为variable。 upd:值来自特定于配置文件的属性文件