Can't instantiate Spring bean repository

2019-08-22 23:58发布

问题:

In this task I am not allowed to use auto-wiring

so I am trying to create something similar to this: https://www.tutorialspoint.com/spring/spring_java_based_configuration.htm

My code

@Configuration
public class ApplicationConfig {

    @Bean
    public FoodService foodService() {
        return new FoodService(FoodRepository());
    }

    @Bean
    public FoodRepository foodRepository() {
        return new FoodRepository();
    }
}

Of course since FoodRepository is an interface extending MongoRepository, it gives an error that it has not been instantiated, and for sure inside the service I need to be calling the Repository to save and such. How to solve this?

回答1:

I found the answer in Spring Data MongoDB documentation

Inside ApplicationConfig.java I should have done the following:

@Configuration
@EnableMongoRepositories("com.food.repository")
public class ApplicationConfig {
@Value("${spring.data.mongodb.host}")
private String mongoHost;

@Value("${spring.data.mongodb.port}")
private int mongoPort;

@Value("${spring.data.mongodb.database}")
private String mongoDB;

@Bean
public foodService foodService() {
    MongoOperations operations = new MongoTemplate(new MongoClient(mongoHost,mongoPort), mongoDB);
    MongoRepositoryFactory factory = new MongoRepositoryFactory(operations);
    foodRepository foodRepository = factory.getRepository(foodRepository.class);
    return new foodService(foodRepository);
}


回答2:

Updated belog the line

Since you didn't tag the question with Spring Boot, I'll assume you are using vanilla Spring.

According to https://docs.spring.io/spring-data/mongodb/docs/1.2.0.RELEASE/reference/html/mongo.repositories.html you would need:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:mongo="http://www.springframework.org/schema/data/mongo"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/data/mongo
    http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

  <mongo:mongo id="mongo" />

  <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <constructor-arg ref="mongo" />
    <constructor-arg value="databaseName" />
  </bean>

  <mongo:repositories base-package="com.acme.*.repositories" />

</beans>

I would suggest to create a similar file and include @Import with a reference to the .xml file in ApplicationConfig.

If you are not adding additional method to the FoodRepository interface there is no need to implement it and thus you cannot do new FoodRepository().


Updated:

Since this is a Spring Boot app you would only need:

Add dependency to pom.xml:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

Annotate main class (or @Configuration-annotated class) with @EnableMongoRepositories

Configuration properties with:

spring.data.mongodb.host=
spring.data.mongodb.port=
spring.data.mongodb.database=