yml 绑定到嵌套 java 对象时出现异常
Exception in yml binding to nested java object
在嵌套的java对象中映射yml时遇到以下异常,有没有人也遇到异常,请指教:
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'alias': Could not bind properties to Alias
(prefix=alias, ignoreInvalidFields=false, ignoreUnknownFields=true,
ignoreNestedProperties=false); nested exception is
java.lang.NullPointerException
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:352)
at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:303)
at org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder.rebind(ConfigurationPropertiesRebinder.java:100)
Caused by: java.lang.**NullPointerException**: null
申请-alias.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 + "]";
}
}
这应该有效
@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 中删除这些行 class
@Configuration
@ConfigurationProperties(prefix="alias.name")
你的 PropA class 应该是
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 + "]";
}
}
在嵌套的java对象中映射yml时遇到以下异常,有没有人也遇到异常,请指教:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'alias': Could not bind properties to Alias (prefix=alias, ignoreInvalidFields=false, ignoreUnknownFields=true, ignoreNestedProperties=false); nested exception is java.lang.NullPointerException at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:352) at org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor.postProcessBeforeInitialization(ConfigurationPropertiesBindingPostProcessor.java:303) at org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder.rebind(ConfigurationPropertiesRebinder.java:100)
Caused by: java.lang.**NullPointerException**: null
申请-alias.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 + "]";
}
}
这应该有效
@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 中删除这些行 class
@Configuration
@ConfigurationProperties(prefix="alias.name")
你的 PropA class 应该是
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 + "]";
}
}