Home > Back-end >  Why @ConfigurationProperties does not automatically register as a bean if set @EnableConfigurationPr
Why @ConfigurationProperties does not automatically register as a bean if set @EnableConfigurationPr

Time:08-17

I have an example here:

@ConfigurationProperties(prefix = "hello")
public class MyProperties {
    
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
@EnableConfigurationProperties(MyProperties.class)
public class MyConfig {
}

application.properties

hello.name=zhangsan

My Unit test:

@SpringBootTest
class SpringLearnApplicationTests {

    @Autowired
    private MyProperties myProperties;

    @Test
    void contextLoads() {
        System.out.println(myProperties.getName()); 
    }
}

And then there's an anomaly:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.github.spring_learn.conf.MyProperties' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

I consulted the @EnableConfigurationProperties note:

Enable support for @ConfigurationProperties annotated beans. @ConfigurationProperties beans can be registered in the standard way (for example using @Bean methods) or, for convenience, can be specified directly on this annotation.

Doesn't it mean to quickly register @ConfigurationProperties as Spring Bean ?

Of course, I can register use @compent,@bean,@configuation... like this

@Component
@ConfigurationProperties(prefix = "hello")
public class MyProperties {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

and I don't even need it MyConfig,I think it's much simpler

So, since @EnableConfigurationProperties cannot be registered as a bean, what does it do?

CodePudding user response:

Place @Configuration on the MyConfig object:

// here you've defined everything right
@ConfigurationProperties(prefix = "hello")
public class MyProperties {
    
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

@Configuration // <----- this was missing
@EnableConfigurationProperties(MyProperties.class) 
public class MyConfig  {
   

   // ... here beans will be defined...
   @Bean
   public MySampleBean mySampleBean(MyProperties config) {
     return new MySampleBean(config.getName());
   }
}

Here I've shown an example of MySampleBean that might be aware of some configuration property (config.name) in this case

CodePudding user response:

@EnableConfigurationProperties allows you to support annotated @ConfigurationProperties classes in our application. However, it's worth noting that the Spring Boot documentation says that every project automatically includes @EnableConfigurationProperties. Therefore, @Configurationproperties support is implicitly enabled in every Spring Boot application.

If spring boot did not automatically enable ConfigurationProperties support, and you would not specify this through the @EnableConfigurationProperties annotation, then the @ConfigurationProperties annotation would simply be ignored.

The @Configuration annotation allows spring to define your class as a bean provider.

Even if your class is marked with the @EnableConfigurationProperties annotation, it cannot become a bean source, you must also add the @Configuration annotation.

  • Related