spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

Embedded MongoDB always starts in Eclipse

Open christian-laakmann opened this issue 10 years ago • 14 comments

HI all,

we have an issue with the integrated MongoDB support in Spring Boot 1.3 on Eclipse/Gradle.

de.flapdoodle.embed.mongo ist configured as a test-dependency in Gradle. It is picked up as a normal dependency by Spring STS / Eclipse, as Eclipse has no support for test/compile/runtime-style dependencies and all configurations are flattened and included in the default classpath.

EmbeddedMongoAutoConfiguration always configures and starts an embedded instance, if the class MongoStarter is available. In Eclipse, that class is always available and embedded MongoDB starts up.

As we already have a local MongoDB running, this results in binding conflicts for the configured MongoDB port and the application does not start.

We've worked around this by removing the dependency from build.gradle for now. Is there another way to prevent embedded MongoDB from starting?

In the long run, we'd like to have a configuration option to en/disable the feature.

christian-laakmann avatar Dec 18 '15 14:12 christian-laakmann

Looks like spring.mongodb.embedded.enabled is missing. What do you think @wilkinsona ?

@christian-laakmann you can exclude the auto-configuration for now (org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration) either via the annotation or the property, check the doc for more details.

snicoll avatar Dec 18 '15 14:12 snicoll

I'm not sure about the ever-increasing set of *.enabled properties that we have. When it's a single auto-configuration class (as it is in this case), I think we need to consider drawing the line and relying on an exclusion of the auto-configuration class instead.

wilkinsona avatar Dec 18 '15 14:12 wilkinsona

Hey, I am facing the same problem. There is now way to disable the EmbeddedMongoAutoConfiguration via properties or? Do I really need to exclude it manually? I used the following annotations, but it still does starts up the embedded MongoDB from eclipse.

@SpringBootApplication(exclude = {RabbitAutoConfiguration.class, EmbeddedMongoAutoConfiguration.class, EmbeddedMongoProperties.class}) @EnableWebMvc @EnableDiscoveryClient @EnableHystrix @EnableHystrixDashboard public class Application {

fritzrichter avatar Jun 20 '16 19:06 fritzrichter

You can use the properties files: spring.autoconfigure.exclude=full.path.to.Configuration

See the link @snicoll posted above: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#using-boot-disabling-specific-auto-configuration

christian-laakmann avatar Jun 20 '16 19:06 christian-laakmann

Thanks for the quick reply. That still does not work. I added the following line to my application.properties:

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration

It still finds the lib on the classpath and starts up the embedded Mongo.

fritzrichter avatar Jun 20 '16 19:06 fritzrichter

Ok found the problem... I had another class configuration under /src/test/java which was including the autoconfig again, and actually the class was in the classpath as well... So I added explicitly the @Profile annotation to that config.

fritzrichter avatar Jun 20 '16 19:06 fritzrichter

Looks like excluding the auto-config is the way. We aim at adding a DataMongoTest in a future release that would help this scenario as well.

snicoll avatar Jan 13 '17 19:01 snicoll

@snicoll how can one exclude the EmbeddedMongoAutoConfiguration with the DataMongoTest annotation as to optionally disable starting up the embedded mongo and use another instance.

Including the following in the application.properties does not work: spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration

Nor does adding: @EnableAutoConfiguration(exclude={EmbeddedMongoAutoConfiguration.class, EmbeddedMongoProperties.class})

tomredwards avatar Feb 22 '17 18:02 tomredwards

@tomredwards The various @Data…Test classes selectively import auto-configuration rather than enabling it wholesale. That means that exclusions are handled slightly differently using @ImportAutoConfiguration:

@ImportAutoConfiguration(exclude = EmbeddedMongoAutoConfiguration.class)

wilkinsona avatar Feb 22 '17 18:02 wilkinsona

@wilkinsona Thank you for the quick reply! I now see in the documentation at: https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-testing-spring-boot-applications-testing-autoconfigured-tests that it explains what you just described, to include https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-testing-spring-boot-applications-testing-autoconfigured-mongo-test which gives an example of how to turn off the embedded MongoDB auto-configuration.

I am new to spring-boot, and wanted to see if you could point me to documentation or an example of how to selectively enable or disable the set of auto-configuration classes when using a @...Test annotation either via a @Profile or externalizing a property much along the lines of spring.autoconfigure.exclude (with the goal of being able to to enable or disable starting the embedded MongoD). Thanks.

tomredwards avatar Feb 22 '17 18:02 tomredwards

@tomredwards I don't think that's possible at the moment, unfortunately. The import of auto-configuration is handled by ImportAutoConfigurationImportSelector and, unlike AutoConfigurationImportSelector, it only looks at its annotation's attributes to determine its exclusions.

If you'd like to see @ImportAutoConfiguration support the use of a property then please open a new issue and we'll see what we can do.

wilkinsona avatar Feb 22 '17 19:02 wilkinsona

Just to clear this topic for reference (and that won't address that request), most test slice have a excludeAutoConfiguration attribute so you can exclude the embedded mongodb directly on the annotation: @DataMongoTest(excludeAutoConfiguration = EmbeddedMongoAutoConfiguration.class)

snicoll avatar Feb 23 '17 09:02 snicoll

I don't find where Spring Boot load this Mongo classes because I don't use it for my project and i don't have any autoconfigure include except when use @SpringBootApplication that load in background @EnableAutoConfiguration, but in my case this works:

@EnableAutoConfiguration(exclude={EmbeddedMongoAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})

Because only with "EmbeddedMongoAutoConfiguration.class" doesn't work.

ferquinz avatar May 23 '17 11:05 ferquinz

@ferquinz commenting on a closed issue isn't a great way to get help. Please ask on stackoverflow or join us on Gitter.

snicoll avatar May 23 '17 12:05 snicoll