Wolfone
Wolfone

Reputation: 1436

Spring autowire of static configuration-object fails

as a Spring newbie I try to load external configuration into a Configuration-Object which I then try to autowire.

Following is my setup broken down to a bare minimum (less fields aso): Package-structure:

app
  |--Application.java
app.configuration
  |--ConfigProperties.java

With my configuration.properties residing in:

src/main/resources/config

Application.java:

package app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import app.configuration.ConfigProperties;

@SpringBootApplication
public class Application
{
    //just to test if autowire works
    @Autowired
    public static ConfigProperties config;

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

        System.out.println(config.getId());
    }
}

ConfigProperties.java:

package app.configuration;

import javax.validation.constraints.NotBlank;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.validation.annotation.Validated;

@Configuration
@PropertySource("classpath:/config/configuration.properties")
@ConfigurationProperties
@Validated
public class ConfigProperties
{

    @NotBlank
    private String id;

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

}

This results in a NullPointerException for the line:

System.out.println(config.getId());

in the Apllication.java.

I read several posts on this topic but most imply that own instantiation prevents autowiring, which I am not doing, or that the scan can't find the class to autowire, but from what I read with my annotations and package structure that should not be the problem.

Where is/are my mistake(s)?

PS: it seems that properties are read correctly from the property file. If I test the validation, the expected error is thrown for Null-value if leave out the part where I try to autowire.

Upvotes: 0

Views: 539

Answers (3)

Shanu Gupta
Shanu Gupta

Reputation: 3817

@Autowired
public static ConfigProperties config;

You cannot autowire static fields in Spring.

//just to test if autowire works

You can write a new Junit test cases for that.

Something like:

@Autowired
public ConfigProperties config;

in your test class.

Upvotes: 1

Nikolas
Nikolas

Reputation: 44476

Since the annotation @Autowired requires getters/setters to inject the correct implementation, I don't suggest you to wire a static content. However, there is a workaround for this issue:

public static ConfigProperties config;

@Autowired
public Application(ConfigProperties config) {
    Application.config= config;
}

Alternatively, take a look on the @PostConstruct annotation.

Upvotes: 1

Jose Martinez
Jose Martinez

Reputation: 12022

Do not use a static ConfigProperties . Then use @PostConstruct to print the ID from your config object.

@Autowired
ConfigProperties config;

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

@PostConstruct 
void printId(){
    System.out.println("ID = " + config.getId());
}

Upvotes: 0

Related Questions