Ответ 1
Лучше использовать Spring EL таким образом
<property name="password" value="${email.password:#{null}}"/>
он проверяет, указан ли email.password
, и устанавливает его в null
(not "null"
String) иначе
Я хочу определить значение свойства по умолчанию в файле конфигурации Spring XML. Я хочу, чтобы это значение по умолчанию было null
.
Что-то вроде этого:
...
<ctx:property-placeholder location="file://${configuration.location}"
ignore-unresolvable="true" order="2"
properties-ref="defaultConfiguration"/>
<util:properties id="defaultConfiguration">
<prop key="email.username" >
<null />
</prop>
<prop key="email.password">
<null />
</prop>
</util:properties>
...
Это не работает. Возможно ли даже определить значения null
по умолчанию для свойств в конфигурации Spring XML?
Лучше использовать Spring EL таким образом
<property name="password" value="${email.password:#{null}}"/>
он проверяет, указан ли email.password
, и устанавливает его в null
(not "null"
String) иначе
Вы можете попробовать использовать Spring EL.
<prop key="email.username">#{null}</prop>
посмотрите PropertyPlaceholderConfigurer # setNullValue (String)
В нем указано, что:
По умолчанию такое нулевое значение не задано. Это означает, что невозможно выразить null как значение свойства, если вы явно не указали соответствующее значение
Итак, просто определите строку "null", чтобы отобразить нулевое значение в PropertyPlaceholderConfigurer:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="nullValue" value="null"/>
<property name="location" value="testing.properties"/>
</bean>
Теперь вы можете использовать его в своих файлах свойств:
db.connectionCustomizerClass=null
db.idleConnectionTestPeriod=21600
Похоже, вы можете сделать следующее:
@Value("${some.value:null}")
private String someValue;
и
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfig() {
PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setNullValue("null");
return propertySourcesPlaceholderConfigurer;
}