0

尝试在嵌套的java对象中映射yml时遇到以下异常,是否有人也面临异常,请建议:

org.springframework.beans.factory.BeanCreationException:创建名为“别名”的bean时出错:无法将属性绑定到别名 (前缀=别名,ignoreInvalidFields=false,ignoreUnknownFields=true,ignoreNestedProperties=false);嵌套异常是 org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:303) 的 org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:352) 的 java.lang.NullPointerException在 org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder.rebind(ConfigurationPropertiesRebinder.java:100)

Caused by: java.lang.**NullPointerException**: null

应用程序别名.yml

alias:
    name:
      -
        x: 123
      -
        x: 321 



Alias.java

    @Configuration
    @ConfigurationProperties(prefix = "alias")
    public class Alias {

        private List<PropA> name = null;
        public Alias() {}
        public List<PropA> getName() {
            return name;
        }
        public void setName(List<PropA> name) {
            this.name = name;
        }
    }

AliasResolverImpl.java

    @Component
    @EnableConfigurationProperties
    public class AliasResolverImplBack implements MapperInterface {

        @Autowired
        Alias aliases;

        @Override
        public List<Entity> resolve(List<Entity> request) throws AliasException {
            System.out.println("In impl : "+aliases.toString());
            return null;
        }
    }

ApplicationConfig.java

    @SpringBootApplication(scanBasePackages = "com.ie.naukri.services.mnj.lookupService")

    @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })

    @EnableConfigurationProperties
    public class ApplicationConfig {

        @Bean
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
        public static void main(final String[] args) {
            SpringApplication.run(ApplicationConfig.class, args);
            final ApplicationContext applicationContext = SpringApplication.run(ApplicationConfig.class, args);
            applicationContext.getBean(AliasResolverImpl.class).resolve(null);
        }
    }

PropA.java



     @Configuration
    @ConfigurationProperties(prefix="alias.name")
    public class PropA {

        private int x=0;

        public int getX() {
            return x;
        }
        public void setX(int x) {
            this.x = x;
        }
        @Override
        public String toString() {
            return "PropA [x=" + x + "]";
        }
    }
4

1 回答 1

0

这应该工作

@Configuration
@ConfigurationProperties(prefix = "alias")
public class Alias {

        private List<PropA> name = null;
        public Alias() {}
        public List<PropA> getName() {
            return name;
        }
        public void setName(List<PropA> name) {
            this.name = name;
        }
    }  

并从 PropA 类中删除这些行

@Configuration
@ConfigurationProperties(prefix="alias.name") 

你的 PropA 课程应该只是

public class PropA {

  private int x=0;

  public int getX() {
    return x;
  }
  public void setX(int x) {
    this.x = x;
  }
  @Override
  public String toString() {
    return "PropA [x=" + x + "]";
  }
}
于 2018-06-11T12:18:09.040 回答