无法从spring启动应用程序自动装配yml配置类

时间:2018-02-02 10:40:17

标签: java spring spring-boot

我无法在@autowire申请中spring boot上课。下面是项目资源管理器快照:

enter image description here

从我的主要课程CrmDisconnectionApplication,我打电话给DisconnectionConTrigger课程。在那个课程中,我正在为@autowireYamlConfig。但是我得到了null pointer exception

下面是代码:

CrmDisconnectionApplication

package com.wpits.crm.disconnection;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.wpits.crm.disconnection.quartzJob.DisconnectionCronTrigger;

@SpringBootApplication(scanBasePackages = { "com.wpits.crm" })
public class CrmDisconnectionApplication {

    public static void main(String[] args) {
        SpringApplication.run(CrmDisconnectionApplication.class, args);
        DisconnectionCronTrigger disconnectionCronTrigger = DisconnectionCronTrigger.getInstance();
        disconnectionCronTrigger.initialize();
    }

}

DisconnectionCronTrigger

package com.wpits.crm.disconnection.quartzJob;

import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wpits.crm.disconnection.config.YamlConfig;
@Component
public class DisconnectionCronTrigger {

    @Autowired
    private YamlConfig myConfig;
    private static DisconnectionCronTrigger obj = null;

    private DisconnectionCronTrigger() {}

    public static DisconnectionCronTrigger getInstance() {
        if(obj == null) {
            obj = new DisconnectionCronTrigger();
        }
        return obj;
    }


    public void initialize() {
        System.out.println("using environment: " + myConfig.getEnvironment());
        System.out.println("name: " + myConfig.getName());
        System.out.println("servers: " + myConfig.getServers());
        System.out.println("hobies: "+myConfig.getHobies());

        JobDetail job = JobBuilder.newJob(DisconnectionJob.class).withIdentity("DisconnectionJob", "group1").build();

        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("cronTrigger", "group1").withSchedule(CronScheduleBuilder.cronSchedule("0/10 * * * * ?")).build();
        try {
        Scheduler scheduler = new StdSchedulerFactory().getScheduler();
        scheduler.start();
        scheduler.scheduleJob(job, trigger);
        }catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

YamlConfig

package com.wpits.crm.disconnection.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.*;

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties


public class YamlConfig {
    private String name;
    private String environment;
    private List<String> servers = new ArrayList<>();
    private List<String> hobies = new ArrayList<>();

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEnvironment() {
        return environment;
    }
    public void setEnvironment(String environment) {
        this.environment = environment;
    }
    public List<String> getServers() {
        return servers;
    }
    public void setServers(List<String> servers) {
        this.servers = servers;
    }
    public List<String> getHobies() {
        return hobies;
    }
    public void setHobies(List<String> hobies) {
        this.hobies = hobies;
    }    
}

我在课程null pointer exception中获得System.out.println("using environment: " + myConfig.getEnvironment());DisconnectionCronTrigger。我在哪里弄错了。请纠正我..

1 个答案:

答案 0 :(得分:2)

问题在于这一行

DisconnectionCronTrigger disconnectionCronTrigger = DisconnectionCronTrigger.getInstance();

在getInstance中,您使用new创建一个新对象。你不应该做新的,而是自动装配bean或从Spring应用程序上下文中获取它。

 public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(CrmDisconnectionApplication.class, args);
        DisconnectionCronTrigger disconnectionCronTrigger = (DisconnectionCronTrigger)context.getBean("disconnectionCronTrigger");
        disconnectionCronTrigger.initialize();
    }

如果您这样做,那么您将获得一个对象,将自动连接bean中的所有字段。如果使用new创建对象,则不会。