Is it possible to exclude a class from auto-configuration
based on the value of a property?
In my case I want to disable,
MailSenderAutoConfiguration
in some case.
For example,
say I have this property app.email.disabled=true
in my application.properties
,
I would like something like this:
@SpringBootApplication
@EnableAutoConfiguration(exclude = MailSenderAutoConfiguration.class, ifProperty="app.email.disabled")
public class Application { .... }
I know I can declare a bean of type JavaMailSender
and use @ConditionalOnProperty
,
but I already defined all email's properties in the
application.properties
and
I don't want to repeat them again in a class.
I found a solution for my case, which is not the best solution but it works. In the MailSenderAutoConfiguration
class there is this nested class :
**
* Condition to trigger the creation of a {@link JavaMailSenderImpl}. This kicks in if
* either the host or jndi name property is set.
*/
static class MailSenderCondition extends AnyNestedCondition {...}
So I just commented the spring.mail.host
property and when I'll need to send emails I'll uncomment it. It isn't the best solution because I am sure I'll forget to uncomment it one day.
This solution works in my case but for general needs (any auto-configuration) it will not work. I hope to see this feature on next Spring Boot version.