【发布时间】:2018-02-27 16:49:37
【问题描述】:
期望的结果:
如果没有找到属性键,或者值为空白默认为null。
为了在钱包和直接登录环境中工作,我需要它可以为空或可以为空。
问题:
我无法正确解析 SpEL 默认为空的表达式。
但是,根据文档,它应该适用于 Spring 3.0。
我怀疑我在 XML 或依赖项中遗漏了一些东西,但我一生都无法弄清楚它是什么。
注意:
SpEL 的替代方法是可以接受的,但我不想全局允许所有属性为空。 (不想设置ignoreUnresolvablePlaceholders=true)
尝试的修复:
- 在 bean 定义中添加了
springframework.org/schema/context。 没有影响 - 尝试将初始的
$替换为#,但这只是作为文字字符串读取。 (
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。或者更好的是只依赖<context:property-placeholder> -
PropertySourcesPlaceholderConfigurer在 Spring 3.0 中不存在 -
使用
没有区别。 -
OK-OK。我得到了它。我错了 - 同意。
-
事实证明,根本原因是 Spring 2.0 的依赖关系搞砸了。更新到 3.0.7 后,