ngandriau
ngandriau

Reputation: 350

How to use a mongoTemplate with a qualifier in a MongoRepository?

We have an in house configuration mechanism which create spring bean outside of the regular xml file or java Configuration class.

Inside this configuration, we have a mongoTemplate bean which is created with a specific qualifier "appMongoTemplate"

We can @autowired this mongoTemplate in our services like any regular spring bean:

@Autowired
@Qualifier("appMongoTemplate")
protected MongoTemplate mongoTemplate;

Now I am trying to use a MongoRepository. I just declare a regular interface:

public interface MyRepository extends MongoRepository<MyDocument, String>

But when my application start, I have an exception during the repository creation, because it cannot find the mongoTemplate bean. I guess I need to specify the name of the mongoTemplate to use, but I don't know how.

Thanks

Upvotes: 0

Views: 1900

Answers (1)

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83081

Both the XML namespace and the @EnableMongoRepositories annotation have an attribute to explicitly wire the MongoTemplate bean to be used for the repositories:

<mongo:repositories mongo-template-ref="appMongoTemplate" />

or

@EnableMongoRepositories(mongoTemplateRef = "appMongoTemplate")

Upvotes: 2

Related Questions