3

I have a problem with spring boot when using autowired on a configuration class. I have minimized the problem by creating a small spring boot project on github.

I created the MyBean class declaring it as @Component and attempting the autowired of the MyConf class which is declared as @Configuration (and reads the property in the file myconfig.properties). In theory, everything is in the spring context, but when the application starts myConfigProp variable in MyBean is null.

Where am I wrong?

I also tried the following solutions, all not working:

  • Insert the @DependsOn in MyBean
  • Commented on the @Component and configured MyBean as @Bean of spring

The last test I did (not present on github project) was to pass MyConfigProp as a parameter in MyBean constructor, and it worked.

@Component
public class MyBean {

    String message;

    public MyBean(MyConfigProp myConfigProp) {
        this.message = myConfigProp.getMessage();
    }

}

I am somewhat confused.

3 Answers 3

2

Looks like you're not Autowiring MyConfigProp's into MyBean:

@Component
public class MyBean {

String message;

    @Autowired
    public MyBean(MyConfigProp myConfigProp) {
        this.message = myConfigProp.getMessage();
    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

We don't really need @Autowired annotation when we do construction auto-loading.
It seems the only working solution, however @Autowired make no difference. Unfortunately I still don't understand why this is the only solution
@Johna You're right, I wasn't aware this feature had been introduced. More specifically, you don't need Autowired on single parameter constructors since spring 4.3. And yet for some reason adding it fixes the problem ???
1

You need to add the @EnableConfigurationProperties(MyConfigProp.class) in your MyBean class so that it looks like:

MyBean.java

@Component
@EnableConfigurationProperties(MyConfigProp.class)
public class MyBean {

String message;

    @Autowired
    public MyBean(MyConfigProp myConfigProp) {
        this.message = myConfigProp.getMessage();
    }

}

MyConfigProp.java

@PropertySource("classpath:myconfig.properties")
@ConfigurationProperties(prefix = "config")
public class MyConfigProp {

    private String message;

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

10 Comments

@EnableConfigurationProperties(MyConfigProp.class) make no difference
Remove the @Configuration at the following line: github.com/Hatta84/Spring-Boot-Test/blob/…
Nope wrong again. If you check the code out you can test this before posting.
@MichaelW I checked the code out and tested this, the code worked with the first modification, adding the @EnableConfigurationProperties(MyConfigProp.class) as suggested above. The second suggestion was because I suspected that Spring could have issues with @Configuration as it essentially a component that produces Beans for the context and this was not producing Beans, it was binding properties.
@MichaelW you also do not need @Autowired when using constructor injection, Spring knows to Autowired in that instance ;)
|
0

I tried all the solutions and the only one that actually solve the problem is to pass MyConfigProp as a parameter in MyBean constructor, as from post, even without @Autowired.

Code update

@Component
public class MyBean {

  String message;

  public MyBean (MyConfigProp myConfigProp) {
     this.message = myConfigProp.getMessage ();
  }
}

I share the rest for knowledge. In detail, trying the proposed solutions, the result was:

  • Adding the @Component stereo-type on MyConfigProp annotation to use @Autowired in MyBean not work, even adding @ComponentScan. It still launches NullPointerException
  • The annotation @EnableConfigurationProperties seems to be useful only if @Configuration is not used on MyConfigProp and does not solve the problem

From the tests and the documentation readings, if I understand correctly, the problem is that I try to use the object in @ Autowired in theMyBean constructor and, during the creation of this, Spring has not yet instantiatedMyConfigProp, hence theNullPointerException. I updated the code by adding a solutions package with the possible solutions:

  • MyBeanWorked: Solution shown above.
  • WithoutConstructor: By moving the instruction into a method, the startup is successful and the application works.
  • WorkedWithInjectProp: Not declared as @Component but configured as @Bean. A little longer but, needing only a property, perhaps cleaner.

More details in the code. I hope I have done something pleasant.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.