1

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.

DwB
  • 37,124
  • 11
  • 56
  • 82
akuma8
  • 4,160
  • 5
  • 46
  • 82

4 Answers4

2

Although there is @ConditionalOnProperty annotation you don't have control over Spring Boot's MailSenderAutoConfiguration.

You could move the spring.mail properties into a separate application-mail.yaml file. It will be loaded only when Spring has an active mail profile (e.g. @ActiveProfiles("mail")). Without this mail profile the default MailSenderAutoConfiguration won't see them.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • This could not work in my case because I have many property files which are enabled dependending on the active profile. If I add a new property file my app will crash. I do not have other choice than declare my own bean. I took a quick look at the code of `MailSenderAutoConfiguration` and I full have control over the bean created by Spring Boot since its properties are based on properties defined in application.properties. – akuma8 Jul 14 '18 at 12:31
  • @akuma8 If adding a new property file for a non-active profile crashes your app you have a bug. – Karol Dowbecki Jul 14 '18 at 12:32
  • The problem will be when the profile will be active because in my app I have this : `@PropertySource( value = { "classpath:securityservice/security-${spring.profiles.active}.properties" } )` so when the `mail` profile will be active, the file `security-mail.properties` will not be found – akuma8 Jul 14 '18 at 12:35
1

I had similiar case with JmsAutoConfiguration, and what have I done was:

  1. Exclude in main SpringBoot class:
    @EnableAutoConfiguration(exclude = {ActiveMQAutoConfiguration.class,JmsAutoConfiguration.class})
  1. Create separate configuration class:

@Configuration
@ConditionalOnProperty("application.queue.enabled")
@EnableJms
@ImportAutoConfiguration({ActiveMQAutoConfiguration.class,JmsAutoConfiguration.class})
public class JmsConfiguration {
    ...

Hope it helps..

iskramac
  • 868
  • 6
  • 14
0

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.

akuma8
  • 4,160
  • 5
  • 46
  • 82
0

To disable AutoConfiguration by property you can create excluder configuration like that;

    @Configuration
    @ConditionalOnProperty(prefix = "app.email", value = "enabled", havingValue = "false")
    @EnableAutoConfiguration(exclude = {MailSenderAutoConfiguration.class})
    public class MailSenderAutoConfigurationExcluder {}