服务类,可从yml文件中读取数据

时间:2019-12-19 11:54:20

标签: spring spring-boot

我有以下代码:

application.yml
security:
  section1:
    key1: "value1"
    key2: "value2"
  section2:
    key1: "value1"
    key2: "value2"
//Main class
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

服务等级

@Service
public class DemoService {

    private Map<String,Section1Properties>projects=new HashMap<String,Section1Properties>();
    @Autowired
    public DemoService(SecurityProperties security) {
        this.projects=security.myConfig1();
        projects.get("section1").getKey1();
        projects.get("section1").getKey2();
    }

}

配置类

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix = "security")
public class SecurityProperties implements Serializable{

    private static final long serialVersionUID = 1L;
    private String section1;
    private String section2;
    private Map<String,Section1Properties> sec1=new HashMap<String,Section1Properties>();
    private Map<String,Section2Properties> sec2=new HashMap<String,Section2Properties>();

    public Map<String,Section1Properties> myConfig1(){
        return sec1;
    }   
    public Map<String,Section2Properties> myConfig2(){
        return sec2;
    }
    public String getSection1() {
        return section1;
    }

    public void setSection1(String section1) {
        this.section1 = section1;
    }

    public String getSection2() {
        return section2;
    }

    public void setSection2(String section2) {
        this.section2 = section2;
    }

    public static class Section1Properties{
    private String key1;
     private String key2;
    public String getKey1() {
        return key1;
    }
    public void setKey1(String key1) {
        this.key1 = key1;
    }
    public String getKey2() {
        return key2;
    }
    public void setKey2(String key2) {
        this.key2 = key2;
    }
}

    public static class Section2Properties{
        private String key1;
         private String key2;
        public String getKey1() {
            return key1;
        }
        public void setKey1(String key1) {
            this.key1 = key1;
        }
        public String getKey2() {
            return key2;
        }
        public void setKey2(String key2) {
            this.key2 = key2;
        }
    }
}

运行此代码时,出现如下所示的错误

  

org.springframework.beans.factory.BeanCreationException:创建文件[C:\ Users \ ABC \ eclipse-workspace \ demo \ target \ classes \ com \ demo \ DemoService.class]中定义的名称为'demoService'的bean时出错:通过构造函数实例化Bean失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[com.demo.DemoService]:构造方法引发了异常;嵌套的异常是java.lang.NullPointerException       在org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:314)〜[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]       在org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:295)〜[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1358)〜[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204)〜[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)〜[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)〜[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]       在org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:323)〜[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)〜[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)〜[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)〜[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]       在org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)〜[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]       在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)〜[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]       在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)〜[spring-context-5.2.2.RELEASE.jar:5.2.2.RELEASE]       在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)〜[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]       在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)上[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]       在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]       在org.springframework.boot.SpringApplication.run(SpringApplication.java:315)[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]       在org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)上[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]       在org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)上[spring-boot-2.2.2.RELEASE.jar:2.2.2.RELEASE]       在com.demo.DemoApplication.main(DemoApplication.java:10)[classes /:na]   由以下原因导致:org.springframework.beans.BeanInstantiationException:无法实例化[com.demo.DemoService]:构造函数引发了异常;嵌套的异常是java.lang.NullPointerException       在org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:213)〜[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]       在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117)〜[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]       在org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:310)〜[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]       ...省略了19个通用框架   引起原因:java.lang.NullPointerException:null       在com.demo.DemoService。(DemoService.java:18)〜[classes /:na]       在sun.reflect.NativeConstructorAccessorImpl.newInstance0(本地方法)〜[na:1.8.0_231]       在sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)〜[na:1.8.0_231]       在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)〜[na:1.8.0_231]       在java.lang.reflect.Constructor.newInstance(Constructor.java:423)〜[na:1.8.0_231]       在org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:200)〜[spring-beans-5.2.2.RELEASE.jar:5.2.2.RELEASE]       ...省略了21个常见框架

0 个答案:

没有答案