我有以下application.yml文件,我想在控制台或其他任何地方读取和显示数据。
security:
Test :
Name:'anyname'
Username:'user1023'
Password:'pass'
Salary:'290378'
Address:'203 LA'
PhoneNo:+13457938549
DEV :
Name:'anyname1'
Username:'user1022'
Password:'bgjsl'
Salary:'290378'
Address:'203 LA'
PhoneNo:+13457938549
我的服务等级
@Service
public class EmployeeService {
//For testing,getting data
private Map<String, WebConfig.Project> projects;
@Autowired
public EncryptionService(WebConfig config) {
this.projects = config.getMyConfig();
projects.get("DEV").getName();
projects.get("DEV").getUsername();
projects.get("DEV").getPassword();
projects.get("DEV").getSalary();
projects.get("DEV").getAddress();
projects.get("DEV").getPhoneNo();
}
}
//我的配置类
@Configuration
@ConfigurationProperties(prefix = "security")
public class WebConfig implements Serializable{
public Map<String, Project> getMyConfig() {
return myConfig;
}
public static class Project{
Name
Username
Password
Salary
Address
PhoneNo
//getter and setter
}
}
并且在运行此应用程序时出现错误,所以我有两个问题 第一个是在控制台中显示数据,另一个是在创建名为EmployeeService的bean时出错。该代码有什么问题?
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.config.EmployeeService]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:213) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:310) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
... 19 common frames omitted
Caused by: java.lang.NullPointerException: null
at com.config.encrypt.EmployeeService.<init>(EmployeeService.java:15) ~[classes/:na]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_231]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_231]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_231]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_231]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:200) ~[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]
... 21 common frames omitted