如果未指定属性,是否可以将空字符串或空字符串作为将在spring文件中使用的默认值?目前我拥有的最好的是
<constructor-arg index="1" value="@{data.url:""}"/>
""在代码中解析
我正在使用带有弹簧轮廓的弹簧3.1来装载豆子.在我的应用程序上下文文件中,我加载了以下属性:
<context:property-placeholder order="1"  location="classpath*:META-INF/spring/*_${spring.profiles.active}.properties" ignore-unresolvable="true"/>
然后我使用属性值来加载数据源bean
<property name="driverClassName" value="${database.driverClassName}"/>
它工作正常.当我添加几个属性占位符以便可以加载某些数据库表的属性时,问题就开始了.
这使用加载的属性引用
<bean id="configFactoryBean"
class="org.springmodules.commons.configuration.CommonsConfigurationFactoryBean">
   <constructor-arg ref="globalSystemConfiguration"/>
</bean>
要添加到详细信息,这configFactoryBean将使用datasource从数据库加载属性.
当我这样做时,我有以下例外:
java.lang.ClassNotFoundException: ${database.driverClassName}
我的分析是它试图datasource在从第一个上下文属性占位符解析属性之前加载.我可能错了.或者弹簧轮廓变量未正确解析.
任何人都可以帮我解决这个问题.
谢谢Akki
我想使用SPeL,并且需要从配置源评估参数。问题在于名称/键是动态的。所以我依靠一个参数来解决另一个。我基本上需要检查一个布尔参数。
示例:部分键/前缀:app.name全键:$ {app.name} .feature.isEnabled
因此,在SPeL中,我尝试如下操作:
#{'${app.name}.feature.isEnabled' != null && !'${app.name}.feature.isEnabled'}
但这可以编译但不起作用。
如果app.name = my-app,则上面的内容解析为字符串文字:my-app.feature.isEnabled
文字本身本身就可以,但是我实际上需要此键的值。
如果我尝试用另一个表达式包装,它将无法编译:
#{${'${app.name}.feature.isEnabled'} != null && !${'${app.name}.feature.isEnabled'}}
我尝试了上述方法的不同变体,但无法采用正确的公式。
这可能吗?
我正在寻找如何在 XML 中使用 Java 默认属性值,而无需在应用程序 YML 或其他任何内容中指定。
这是我的 java 配置,默认情况下我想使用这个 URL 值,直到从 YML 文件提供它。
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "test.sample")
public @Data class SampleProperties {
   private String serverurl ="test.example.com";
}
当我尝试在 XML 中使用时
<property name="serverURL" value="${test.sample.serverurl}" />
投掷
IllegalArgumentException : Could not resolve placeholder 'test.sample.serverurl' in value "${test.sample.serverurl}"