Basically, an implementation of JavaMailSender
is exposed as part of the Spring Boot Autoconfiguration. This is noted in the documentation here:
The Spring Framework provides an easy abstraction for sending email by using the JavaMailSender interface, and Spring Boot provides auto-configuration for it as well as a starter module.
This is not the case with BCryptPasswordEncoder
.
If I had to guess, I would think that this approach was chosen because:
- Few developers would need to reimplement the
JavaMailSender
functionality for sending Emails
- Many developers choose different hashing algorithms based on speed and security needs, though the
BCryptPasswordEncoder
is recommended.
- Since various developers might opt for an alternative
PasswordEncoder
, having a BCryptPasswordEncoder
already configured would necessitate using @Primary
on the one you actually wanted to use (in the case of writing something like @Autowired PasswordEncoder encoder
), which might lead to confusion.
How do I know for sure when to expose beans explicitly via a configuration class and when not to ? How do I know which beans need to be exposed ?
Read the reference documentation for the particular bean - it should state whether it is pre-configured or not (and under which conditions, i.e. in the presence of some starter package). Barring that, you might check the implementation to see if there are any components which implement the interface of the bean you wish to inject.
UPDATE: I actually went through some of the sources and noticed that the interfaces implemented by BCryptPasswordEncoder
on one hand, and Md5PasswordEncoder
/ShaPasswordEncoder
on the other, are in different packages, the latter being deprecated. In this case it may be safe to assume that there is no autoconfiguration being done for BCryptPasswordEncoder
since:
- It could lead to a great deal of confusion to see one interface be implemented, when a completely different one in a different package is being referred to (i.e. different methods being implemented)
- The 'default values' that should be used when hashing a password would likely differ from one developer to another, which would necessitate manual configuration anyway.