@Configuration

  • It is stereotype annotation to indicate bean definitions inside a class, also it marks the class as bean (since it has @Component inside it)

@ConfigurationProperties

  • Used to bind a class with an externalized property file. Very powerful and must be used to separate out bean classes with configuration entity class.
  • Point to note is that it DOES NOT register the class as Bean, hence it does not act as a bean but instead work as simple POJO.
  • To register it as bean you can do either of the following:
    • mark the class as @Configuration
    • create another class with @EnableConfigurationProperties
  • Example:
// Need to create another class to instantiate as bean
@Getter  
@Setter
@ConfigurationProperties(prefix="transportation-service")
public class TransportationServiceProperties {
	private String baseUrl;  
	private String partnerUri;
}
 
 
 
// OR
 
@NoArgsConstructor  
@Configuration // instantiate this class as a bean
@Getter  
@Setter
@ConfigurationProperties(prefix="transportation-service")
public class TransportationServiceConfiguration { // notice the name
	private String baseUrl;  
	private String partnerUri;
}

@EnableConfigurationProperties

  • It registers @ConfigurationProperties annotated class as a bean, and hence very closely related to it.
  • Example:
// Need to create another class to instantiate as bean (hence 2 classes needed)
@Getter  
@Setter
@ConfigurationProperties(prefix="transportation-service")
public class TransportationServiceProperties {
	private String baseUrl;  
	private String partnerUri;
}
 
// This instantiate it as bean
@Configuration
@EnableConfigurationProperties(TransportationServiceProperties.class)  
public class TransportationServiceConfiguration {
	@Autowired
	private final TransportationServiceProperties properties;
 
	// use properties....
}
 
 
// OR
 
 
 
@NoArgsConstructor  
@Configuration // instantiate this class as a bean (one class is enough)
@Getter  
@Setter
@ConfigurationProperties(prefix="transportation-service")
public class TransportationServiceConfiguration { // notice the name
	private String baseUrl;  
	private String partnerUri;
}