[英]How to get SpEL (spring-el) default to null expression working in the Spring XML?
如果找不到属性键,或者值为空白,则默认为null
。
我需要将此字段设置为可以为空或可以为空,以便在钱包和直接登录环境中均可使用。
我无法正确解析SpEL的默认为空表达式。
但是,根据文档,它应该适用于Spring 3.0。
我怀疑我缺少XML或依赖项中的某些内容,但是无法终生弄清楚它是什么。
SpEL的替代方法是可以接受的,但是我不想全局允许所有属性为null。 (不想设置ignoreUnresolvablePlaceholders=true
)
springframework.org/schema/context
。 没有效果 #
替换初始的$
,但这只是作为文字字符串读取。 ( 春季版本: 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
SpEL在占位符之后解析; 尝试类似
#{'${jdbc.credu:xxx}' == 'xxx' ? null : '${jdbc.credu:xxx}'}
编辑
3.0.7是非常老的版本; 春季版本最高为4.3.x,可与Java 6配合使用。
至少在3.1.4版本中可以正常工作。
<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进行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.