0

I had a question while writing to test auto-configuration.

@Configuration
public class SimpleConfiguration {
    @Bean
    public BigDecimal aDecimal( String example ) {
        return new BigDecimal( example );
    }
}
@Configuration
public class BigDecimal {
    
}

I think the above two codes have the same result of registering BigDecimal object as bean. However, if you look at the example of auto-configuration, it is implemented with the code above. Why use the code above if the two results are the same

raboy
  • 1
  • 1
  • The second should be `@Component public class BigDecimal {}` (but `@Configuration` itself is meta-annotated with `@Component`, so it would still "work"). – knittl Aug 10 '23 at 08:10

1 Answers1

0

The first form gives you much more flexibility:

  • Default parameter values
  • Multiple bean instances of the same type
  • Wiring (private) dependencies that are not beans themselves
  • Registering classes as beans without touching those classes (you might not have access to the classes' source code, external libraries, etc.)
knittl
  • 246,190
  • 53
  • 318
  • 364