简体   繁体   English

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

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

I am working on a project where I am given a project configuration as 我正在一个项目中,我得到了一个项目配置

project-config.xml 项目配置文件

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

I am adding a test configuration from where I can pass this values 我正在从此处传递此值的位置添加测试配置

test-config.xml 测试配置文件

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

But this doesn't work. 但这是行不通的。 How can I 我怎样才能

have the variables defined in test-config.xml 具有在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

substitute for values in project-config.xml 替代project-config.xml

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

You can do it this way, your place holders should get resolved. 您可以通过这种方式进行操作,您的占位符应该得到解决。

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

Another way would be to have the entries in the test.properties file above. 另一种方法是将条目包含在上面的test.properties文件中。

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

相关问题 使用不同的构造函数参数创建2个相同类的bean并使用自动装配 - Creating 2 beans of the same class with different constructor-args and using autowiring 如何在构造函数中将字段设置为常量 - How do i set a field as constant in the constructor 如何将UniqueID作为构造函数args传递给Spring bean - How to pass UniqueID as constructor args to a spring bean Spring的构造函数args? - Constructor args in Spring? Spring p命名空间用于构造函数args? - Spring p namespace for constructor args? 如何告诉Spring实例化选定的bean,而不提供构造函数args? - How to tell Spring to instantiate selected beans without providing constructor args? 在 Spring 集成中,如何在路由器通道中使用 Util 常量 - In Spring integration, how do I use Util constant in router channel 如何从 Camel Spring 独立应用程序中的命令行参数设置属性 - How do i set properties from command line args in Camel Spring standalone app 为什么我需要一个无参数构造函数才能在 CDI 中使用带有构造函数注入的 ApplicationScoped bean? - Why do I need a no-args constructor to use ApplicationScoped beans with Constructor injection within CDI? 不确定Spring bean中构造函数args的概念 - unsure of concept of constructor args in spring beans
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM