无法解析字符串值中的占位符自定义对象列表

时间:2017-04-03 03:22:40

标签: java spring-boot

我无法在弹簧控制器中读取如下所示的自定义对象列表。但是,能够通过在restcontoller中将bean自动装配到java bean中来读取/获取数据。 任何人都可以帮助我使用annonation @value来读取其他控制器的数据列表吗?

无法在字符串值“$ {royalty.testRates}”中解析占位符自定义对象列表(royalty.testRates)。

YAML文件的代码段:

testRates:
    - channelType: "WEB"
      value: "0.03"
    - channelType: "ANDROID_TAB"
      value: "0.04"
    - channelType: "ANDROID_PHONE"
      value: "0.04"

java文件的代码段: Java Bean:

@EnableAsync
@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(ignoreInvalidFields = false, prefix = "royalty")
@Component
public class RoyaltyMigration {

private List<TestRate> testRates = new ArrayList<TestRate>();

public static class TestRate {
    private String channelType;
    private String value;

    public List<TestRate> getTestRates() {
    return testRates;
    }

    public void setTestRates(List<TestRate> testRates) {
    this.testRates = testRates;
    }

    public String getChannelType() {
        return channelType;
    }

    public void setChannelType(String channelType) {
        this.channelType = channelType;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

Spring Rest Controller:

//@Autowired//Commented
//RoyaltyMigration royaltyMigration;  //Commented

@Value("${royalty.testRates}")
private List<RoyaltyMigration.TestRate> testRates;//= new ArrayList<RoyaltyMigration.TestRate>();;

public void setTestRates(List<RoyaltyMigration.TestRate> testRates) {
    this.testRates = testRates;
}
@RequestMapping(value = "/testrates", method = {RequestMethod.GET, RequestMethod.POST},
        produces = "application/json")
public ResponseEntity<RoyaltyMigration> testRates() {
    final RoyaltyMigration royaltyMigration = new RoyaltyMigration();
    royaltyMigration.setTestRates(this.testRates);
    return new ResponseEntity<>(royaltyMigration, HttpStatus.OK);
}

在此处记录跟踪 -

2017-04-02 21:38:46,366 [main] org.springframework.boot.SpringApplication ERROR Application startup failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'royaltyMigrationController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List com.royalty.controller.RoyaltyMigrationController.testRates; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'royalty.testRates' in string value "${royalty.testRates}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:760)
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:360)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:306)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174)
    at com.expedia.www.host.loyalty.Starter.main(Starter.java:23)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private java.util.List com.royalty.controller.LoyaltyMigrationController.testRates; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'loyalty.testRates' in string value "${royalty.testRates}"
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 17 more
Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'royalty.testRates' in string value "${royalty.testRates}"
    at org.springframework.util.PropertyPlaceholderHelper.parseStringValue(PropertyPlaceholderHelper.java:174)
    at org.springframework.util.PropertyPlaceholderHelper.replacePlaceholders(PropertyPlaceholderHelper.java:126)
    at org.springframework.core.env.AbstractPropertyResolver.doResolvePlaceholders(AbstractPropertyResolver.java:204)
    at org.springframework.core.env.AbstractPropertyResolver.resolveRequiredPlaceholders(AbstractPropertyResolver.java:178)
    at org.springframework.context.support.PropertySourcesPlaceholderConfigurer$2.resolveStringValue(PropertySourcesPlaceholderConfigurer.java:172)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveEmbeddedValue(AbstractBeanFactory.java:808)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1027)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
    ... 19 more

0 个答案:

没有答案