如何将 Spring 表达式语言评估作为参数传递



这不起作用:

<?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

试试这个格式:

<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" />

最新更新