简体   繁体   中英

Spring Boot - @Configuration class is null in spring component

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.

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

}

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

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 the MyBean constructor and, during the creation of this, Spring has not yet instantiated MyConfigProp , hence the NullPointerException . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM