osigge
osigge

Reputation: 228

How does autowiring of custom spring data (mongo) repositories configured with java config work?

I use Spring Data Mongo with repositories. In my xml-configuration everything works fine. Now I want to use Java configuration instead of xml-configuration. This is my java configuration for the repositories:

@Configuration
@EnableMongoRepositories
public class DefaultMongoDbFactoryConfig extends AbstractMongoConfiguration{

    @Value("${db.name}") private String dbName;

    @Inject
    private Mongo mongo;

    @Bean
    protected MongoDbFactory defaultMongoDbFactory() throws Exception {
        return new SimpleMongoDbFactory(mongo, dbName);
    }

    @Bean
    protected MongoTemplate defaultMongoTemplate() throws Exception {
        return new MongoTemplate(defaultMongoDbFactory());
    }

    @Override
    protected String getDatabaseName() {
        return dbName;
    }

    @Override
    public Mongo mongo() throws Exception {
        return mongo;
    }

    @Override
    protected String getMappingBasePackage() {
        return "foo.bar.repository";
    }

    @Override
    public MongoTemplate mongoTemplate() throws Exception {
        return defaultMongoTemplate();
    }


}

I have a repository with a custom implementation:
Interface: MyRepository (extends the custom interface)
CustomInterface: MyRepositoryCustom
Implementation: MyRepositoryImpl

I have another configuration file with the component-scan annotation:

@Configuration
@ComponentScan(basePackages = {"foo.bar"})
@Import(DefaultMongoDbFactoryConfig.class)
public class AppConfig {

}

Now spring does not seem to autowire the custom implementation on startup. I get "No qualifying bean of type". Is this not supported in java config or am I missing something?

EDIT: I'm using spring data mongo 1.3.1 and spring 3.2.4

Upvotes: 1

Views: 3542

Answers (1)

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83161

In which package does DefaultMongoDbFactoryConfig reside? If you don't explicitly state a package in the @EnableMongoRepositories annotation, we only scan the package of the annotated configuration class. If this is in foo.bar.config and you have your repos in foo.bar.repositories no repository interfaces will be found.

Upvotes: 5

Related Questions