【问题标题】:How do I pass a spring expression language evaluation as an argument?如何将 Spring 表达式语言评估作为参数传递?
【发布时间】:2014-03-13 02:15:29
【问题描述】:

这不起作用:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:encryption="http://www.jasypt.org/schema/encryption"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                        http://www.jasypt.org/schema/encryption
                        http://www.jasypt.org/schema/encryption/jasypt-spring31-encryption-1.xsd">

    <import resource="properties/secure/jasypt-context.xml" />
    <bean name="propEnvironment" class="java.lang.String">
        <constructor-arg value="#{ systemProperties['property.environment'] ?: systemProperties['cfg.environment'] }" />
    </bean>
    <encryption:encryptable-property-placeholder 
        encryptor="configurationEncryptor"
        location="classpath:properties/#{propEnvironment}/*.properties,classpath:properties/common.properties" />

</beans>

只有 common.properties 由可加密属性占位符配置。就像另一个条目消失了(没有抛出错误或任何东西,它只是不加载其他任何东西)。我想用生产配置文件建立一个实例,但我想使用我的开发属性。如果我将其打印出来,则 spel 表达式正在正确评估。这可能吗?

当我尝试这个时,

<bean name="propEnvironment" class="java.lang.String" id="testing">
    <constructor-arg value="#{ systemProperties['property.environment'] ?: systemProperties['cfg.environment'] }" />
</bean>
<bean name="fullString" class="java.lang.String">
    <constructor-arg value="classpath:properties/#{ systemProperties['property.environment'] ?: systemProperties['cfg.environment'] }/*.properties" />
</bean>
<bean class="EchoBean" lazy-init="false">
    <constructor-arg ref="propEnvironment" />
</bean>
<bean class="EchoBean" lazy-init="false">
    <constructor-arg ref="fullString" />
</bean>
<bean class="EchoBean" lazy-init="false">
    <constructor-arg value="#{@testing.toString()}" />
</bean>
<bean class="EchoBean" lazy-init="false">
    <constructor-arg value="blah" />
</bean>

使用@语法的第三个回显不产生任何输出。

2014-02-14 10:03:41,998 [main] INFO  EchoBean - local
2014-02-14 10:03:42,000 [main] INFO  EchoBean - classpath:properties/local/*.properties
2014-02-14 10:03:42,000 [main] INFO  EchoBean - blah

【问题讨论】:

    标签: java spring properties expression spring-el


    【解决方案1】:

    试试这个格式:

    <bean class="java.net.URI" id="dbUrl">
        <constructor-arg value="#{systemEnvironment['DATABASE_URL']}"/>
    </bean>
    
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="url" value="#{ 'jdbc:postgresql://' + @dbUrl.getHost() + ':' + @dbUrl.getPort() + @dbUrl.getPath() }"/>
        <property name="username" value="#{ @dbUrl.getUserInfo().split(':')[0] }"/>
        <property name="password" value="#{ @dbUrl.getUserInfo().split(':')[1] }"/>
    </bean>
    

    如果您尝试在propEnvironment 上调用 toString() 会怎样?像这样:

    <encryption:encryptable-property-placeholder 
        encryptor="configurationEncryptor"
        location="classpath:properties/#{@propEnvironment.toString()}/*.properties,classpath:properties/common.properties" />
    

    【讨论】:

    • 我不知道。似乎 Spring 想要在评估 spring-el 之前构建属性占位符
    • @Mongo - 发布了一个更新的示例进行尝试,希望对您有所帮助。如果这不起作用,您可以尝试将“propEnvironment”字符串bean自动装配到某个组件中,看看值是否正确通过?
    • 我用这个结果更新了底部的问题。
    猜你喜欢
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-20
    • 1970-01-01
    相关资源
    最近更新 更多