1
votes

I am using below annotations in my config class to get the values from properties file(yml). Configuration EnableConfigurationProperties ConfigurationProperties (prefix = "notification")

I am able to get the values inside public methods without problem using the class . But I am getting 'Error Creating bean' Error when I try to assign value instance variable of the class using config class.

Below is my code. Can someone please throw some light.

This is my config class

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties (prefix = "notification")
public class NotifyYaml {

private String subscriptionId;

public String getSubscriptionId() {
    return subscriptionId;
}

public void setSubscriptionId(String subscriptionId) {
    this.subscriptionId = subscriptionId;
}

Below is the class where I am getting error during startup.

@Component
public class PubSubController {

    @Autowired
    private NotifyYaml notify;

    public PubSubController() {
        // TODO Auto-generated constructor stub
    }

String projectId = "ccc-g-pre-proj-cacdate";
    //Error in this line
    String subscriptionId = notify.getSubscriptionId();
1

1 Answers

0
votes

The @Autowired object only gets filled in after the object is created.

This means that while the object is being created, it tries to call a method from a null object.

I would suggest using something like a @PostConstruct method. (Note: you will need to include javax.annotations into your project somehow.)

String subscriptions; // remove the value for now...

@PostConstruct
private void init() {
    subscriptions = notify.getSubscriptionId(); // ...and add it back in here.
}