繁体   English   中英

春季:如何用常量值代替构造函数参数?

[英]Spring: How do I substitute constant values for constructor-args?

我正在一个项目中,我得到了一个项目配置

项目配置文件

 <bean id="awsCredentials" class="com.amazonaws.auth.BasicAWSCredentials">
        <constructor-arg
                value="${com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator.dest.ProposalManager-accessKeyId}"/>
        <constructor-arg
                value="${com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator.dest.ProposalManager-secretAccessKey}"/>
    </bean>

我正在从此处传递此值的位置添加测试配置

测试配置文件

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator-destBucketName=bucketname
    com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator.dest.ProposalManager-accessKeyId=accesskey
    com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator.dest.ProposalManager-secretAccessKey=secretaccess key

    <bean id="spa-evaluation-factory" class="com.myorg.sparrow..business.DummySpaEvaluationFactory"/>
    <import resource="classpath:/com/myorg/sparrow/spa_adapter/project-config.xml"/>
</beans>

但这是行不通的。 我怎样才能

具有在test-config.xml定义的变量

com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator-destBucketName=bucketname
        com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator.dest.ProposalManager-accessKeyId=accesskey
        com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator.dest.ProposalManager-secretAccessKey=secretaccess key

替代project-config.xml

<bean id="awsCredentials" class="com.amazonaws.auth.BasicAWSCredentials">
            <constructor-arg
                    value="${com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator.dest.ProposalManager-accessKeyId}"/>

您可以通过这种方式进行操作,您的占位符应该得到解决。

<context:property-placeholder location="classpath*:META-INF/spring/test.properties" local-override="true" properties-ref="localProperties" ignore-resource-not-found="true"/>

<util:properties id="localProperties">
    <prop key="com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator-destBucketName">bucketname</prop>     
    <prop key="com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator.dest.ProposalManager-accessKeyId">accesskey</prop>        
    <prop key="com.myorg.sparrow.s3EnvironmentConfiguration.S3EnvironmentConfigurator.dest.ProposalManager-secretAccessKey">secretaccess key</prop>     
</util:properties>

另一种方法是将条目包含在上面的test.properties文件中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM