1

I am not able to @autowire a class in spring boot application. below is the project explorer snapshot:

enter image description here

From my main class CrmDisconnectionApplication, I am calling DisconnectionConTrigger class. In that class I am doing @autowire for YamlConfig. But I am getting null pointer exception.

below is the code:

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;
    }    
}

I am getting null pointer exception for line System.out.println("using environment: " + myConfig.getEnvironment()); in class DisconnectionCronTrigger. Where am I getting it wrong. Please correct me..

JPG
  • 1,247
  • 5
  • 31
  • 64
  • Why do you have both `application.properties` and `application.yml` files in your project? – gWombat Feb 02 '18 at 10:52
  • because I have to add `spring.profiles.active=prod` in `application.properties`. btw, is it not necessary to add both files when using /reading `yml` fields? – JPG Feb 02 '18 at 11:15
  • No it's not. Please have a look at https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html – gWombat Feb 02 '18 at 14:52

1 Answers1

2

The problem is this line

DisconnectionCronTrigger disconnectionCronTrigger = DisconnectionCronTrigger.getInstance();

In getInstance you are creating a new object using new. You should not do new, instead Autowire the bean or get it from Spring application context.

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

If you do it like this, then you will get an object will all the fields in the bean autowired. If you create a object using new, then you wont.

pvpkiran
  • 25,582
  • 8
  • 87
  • 134