Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 评估Spring中的属性';s context.xml(基本模板)_Java_Spring_Templates_Spring El_Property Injection - Fatal编程技术网

Java 评估Spring中的属性';s context.xml(基本模板)

Java 评估Spring中的属性';s context.xml(基本模板),java,spring,templates,spring-el,property-injection,Java,Spring,Templates,Spring El,Property Injection,我想知道是否有可能在Spring的xml配置文件中计算属性。我目前已经在使用propertyplaceholderconfigure注入属性。但我想要实现的是,如果某个属性为true,则注入一个值,如果为false,则注入另一个值 例如,如果我的自定义属性com.github.dpeger.jpa.validate为true,我想将persistence.context.xml中的hibernate属性hibernate.hbm2ddl.auto设置为仅验证。我知道我可以指定如下默认值: <

我想知道是否有可能在Spring的xml配置文件中计算属性。我目前已经在使用
propertyplaceholderconfigure
注入属性。但我想要实现的是,如果某个属性为true,则注入一个值,如果为false,则注入另一个值

例如,如果我的自定义属性
com.github.dpeger.jpa.validate
为true,我想将persistence.context.xml中的hibernate属性
hibernate.hbm2ddl.auto
设置为仅验证。我知道我可以指定如下默认值:

<property name="jpaProperties">
    <map>
        <entry key="hibernate.hbm2ddl.auto" value="${com.github.dpeger.jpa.validate:none}" />
        ...
    </map>
</property>
<property name="jpaProperties">
    <map>
        <entry key="hibernate.hbm2ddl.auto" value="${com.github.dpeger.jpa.validate?validate:none}" />
        ...
    </map>
</property>

...
但是否有可能以某种方式评估房产的价值,比如:

<property name="jpaProperties">
    <map>
        <entry key="hibernate.hbm2ddl.auto" value="${com.github.dpeger.jpa.validate:none}" />
        ...
    </map>
</property>
<property name="jpaProperties">
    <map>
        <entry key="hibernate.hbm2ddl.auto" value="${com.github.dpeger.jpa.validate?validate:none}" />
        ...
    </map>
</property>

...
第一个选项:

您可以使用
{}
EL表达式,并将
${}
占位符插入此表达式中:

<property name="jpaProperties">
    <map>
        <entry key="hibernate.hbm2ddl.auto" 
            value="#{${com.github.dpeger.jpa.validate}?'validate':'none'}" />
        ...
    </map>
</property>
现在,您可以通过id在EL表达式中使用此属性bean:

<property name="jpaProperties">
    <map>
        <entry key="hibernate.hbm2ddl.auto" 
            value="#{props['com.github.dpeger.jpa.validate']?'validate':'none'}" />
        ...
    </map>
</property>

...

谢谢。第一个选择实际上是我自己在给出现有答案后得出的结论。虽然我不喜欢同时使用SpEL和属性占位符的表示法,但我还是坚持使用这个选项。