繁体   English   中英

将MongoTemplate传递给Custom Repository实现

[英]Passing MongoTemplate to Custom Repository implementation

Project配置为使用多个MongoTemplate

Mongo Ref被传递为

@EnableMongoRepositories(basePackages={"com.mypackage.one"}, mongoTemplateRef="mongoTemplateOne")

对于com.mypackage.one包中的存储库

@EnableMongoRepositories(basePackages={"com.mypackage.two"}, mongoTemplateRef="mongoTemplateTwo")

对于com.mypackage.two包中的存储库

对于标准存储库,它工作正常。 但对于我需要自定义行为的场景,我定义了myRepoCustomImpl以及我的自定义行为需求。

问题 :我需要访问类似标准存储库的MongoTemplate

例如,如果MyRepoMyRepoCustom接口扩展为

@Repository
interface MyRepo extends MongoRepository<MyEntity, String>, MyRepoCustom{}

MyRepoCustomImpl

@Service
    public class MyRepoCustomImpl implements MyRepoCustom{
        @Autowired
        @Qualifier("mongoTemplateOne")
        MongoTemplate mongoTmpl;

        @Override
        MyEntity myCustomNeedFunc(String arg){
            // MyImplemenation goes here
        }

}

如果MyRepocom.mypackage.one包中, mongoTemplateOne将被myRepo使用,所以应该有一些方法让MyRepoCustomImpl知道它也应该使用mongoTemplateOne ,每当我在mongoTemplateRefMyRepo进行更改时,就像

@EnableMongoRepositories(basePackages={"com.mypackage.one"}, mongoTemplateRef="mongoTemplateThree")

现在我需要在MyRepoCustomImpl对@Qualifier进行更改! 自定义行为有很多回购,因此它变得繁琐乏味。

问题:相反,没有任何方法可以根据它扩展到的回购自动注入或解决使用的MongoTemplate?

MongoTemplate不被暴露MongoRepository接口。 他们可能会暴露MongoTemplate @Bean的名称,这可以为您的问题提供解决方案。 但是,鉴于他们没有,我将在下面提供一个可能适合您需求的示例。

首先, mongoTemplateRef指的是要使用的@Bean名称 ,它不指定MongoTemplate的名称。

您需要提供每个MongoTemplate @Bean ,然后在@EnableMongoRepositories注释中引用它。

由于您使用的是spring-boot,因此您可以利用MongoDataAutoConfiguration类。 请看看它在这里做了什么https://github.com/spring-projects/spring-boot/blob/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data /mongo/MongoDataAutoConfiguration.java

最简单的例子就是这样。

package: com.xyz.repo(此实现依赖于MongoDataAutoConfiguration提供的配置)

@Configuration
@EnableMongoRepositories(basePackages={"com.xyz.repo"}) //mongoTemplateRef defaults to mongoTemplate
public class XyzRepoConfiguration {

}

public abstract class BaseRepo {
    @Autowired
    MongoTemplate mongoTemplate;
}

@Service
public class MyRepoCustomImpl extends BaseRepo implements MyRepoCustom {    
    @Override
    MyEntity myCustomNeedFunc(String arg){
        // access to this.mongoTemplate is present
    }
}

包: com.abc.repo

@Configuration
@EnableMongoRepositories(basePackages={"com.abc.repo"}, mongoTemplateRef=AbcRepConfiguration.TEMPLATE_NAME)
public class AbcRepoConfiguration {
    public static final String TEMPLATE_NAME = "mongoTemplateTwo";

    @Bean(name="mongoPropertiesTwo")
    @ConfigurationProperties(prefix="spring.data.mongodb2")
    public MongoProperties mongoProperties() {
        return new MongoProperties();
    }

    @Bean(name="mongoDbFactoryTwo")
    public SimpleMongoDbFactory mongoDbFactory(MongoClient mongo, @Qualifier("mongoPropertiesTwo") MongoProperties mongoProperties) throws Exception {
        String database = this.mongoProperties.getMongoClientDatabase();
        return new SimpleMongoDbFactory(mongo, database);
    }

    @Bean(name=AbcRepoConfiguration.TEMPLATE_NAME)
    public MongoTemplate mongoTemplate(@Qualifier("mongoDbFactoryTwo") MongoDbFactory mongoDbFactory, MongoConverter converter) throws UnknownHostException {
        return new MongoTemplate(mongoDbFactory, converter);
    }
}

public abstract class BaseRepo {
    @Autowired
    @Qualifier(AbcRepoConfiguration.TEMPLATE_NAME)
    MongoTemplate mongoTemplate;
}

@Service
public class MyRepoCustomImpl extends BaseRepo implements MyRepoCustom {    
    @Override
    MyEntity myCustomNeedFunc(String arg){
        // access to this.mongoTemplate is present
    }
}

com.xyz.repo将依托spring.data.mongodb属性中application.properties com.abc.repo将依托spring.data.mongodb2内性能application.properties

我以前没有使用过AbcRepoConfiguration.TEMPLATE_NAME方法,但它是在我的IDE中编译的。

如果您需要任何澄清,请告诉我。

MongoTemplate不会在您的存储库类中注入,而是在spring-data-mongodb更深入,因此您无法从存储库中获取它。 看看你会学到很多东西的代码。

因此,除非您禁用spring-boot自动配置和组件发现并自行配置它,否则不能根据repo扩展注入bean,但这比仅更改@Qualifier名称要长得多。 您的IDE调用可以轻松地帮助您,您可能会后悔禁用自动配置。

抱歉让人失望。

您可以使用以下示例。

1)

package com.johnathanmarksmith.mongodb.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Date:   6/28/13 / 10:40 AM
 * Author: Johnathan Mark Smith
 * Email:  john@johnathanmarksmith.com
 * <p/>
 * Comments:
 *  This main really does not have to be here but I just wanted to add something for the demo..
 *
 */


public class MongoDBApp {

    static final Logger logger = LoggerFactory.getLogger(MongoDBApp.class);

    public static void main(String[] args) {
        logger.info("Fongo Demo application");

        ApplicationContext context = new AnnotationConfigApplicationContext(MongoConfiguration.class);




        logger.info("Fongo Demo application");
    }
}

2)

package com.johnathanmarksmith.mongodb.example;

import com.mongodb.Mongo;
import com.mongodb.ServerAddress;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.mongodb.config.AbstractMongoConfiguration;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import java.util.ArrayList;


/**
 * Date:   5/24/13 / 8:05 AM
 * Author: Johnathan Mark Smith
 * Email:  john@johnathanmarksmith.com
 * <p/>
 * Comments:
 * <p/>
 * This is a example on how to setup a database with Spring's Java Configuration (JavaConfig) style.
 * <p/>
 * As you can see from the code below this is easy and a lot better then using the old style of XML files.
 * <p/>
 * T
 */

@Configuration
@EnableMongoRepositories
@ComponentScan(basePackageClasses = {MongoDBApp.class})
@PropertySource("classpath:application.properties")
public class MongoConfiguration extends AbstractMongoConfiguration {


    @Override
    protected String getDatabaseName() {
        return "demo";
    }



    @Override
    public Mongo mongo() throws Exception {
        /**
         *
         * this is for a single db
         */

        // return new Mongo();


        /**
         *
         * This is for a relset of db's
         */

        return new Mongo(new ArrayList<ServerAddress>() {{ add(new ServerAddress("127.0.0.1", 27017)); add(new ServerAddress("127.0.0.1", 27027)); add(new ServerAddress("127.0.0.1", 27037)); }});

    }

    @Override
    protected String getMappingBasePackage() {
        return "com.johnathanmarksmith.mongodb.example.domain";
    }

}

3)

package com.johnathanmarksmith.mongodb.example.repository;


import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.johnathanmarksmith.mongodb.example.domain.Person;


/**
 * Date:   6/26/13 / 1:22 PM
 * Author: Johnathan Mark Smith
 * Email:  john@johnathanmarksmith.com
 * <p/>
 * Comments:
 * <p/>
 * This is my Person Repository
 */


@Repository
public class PersonRepository {

    static final Logger logger = LoggerFactory.getLogger(PersonRepository.class);

    @Autowired
    MongoTemplate mongoTemplate;

    public long countUnderAge() {
        List<Person> results = null;

        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria = criteria.and("age").lte(21);

        query.addCriteria(criteria);
        //results = mongoTemplate.find(query, Person.class);
        long count = this.mongoTemplate.count(query, Person.class);

        logger.info("Total number of under age in database: {}", count);
        return count;
    }

    /**
     * This will count how many Person Objects I have
     */
    public long countAllPersons() {
        // findAll().size() approach is very inefficient, since it returns the whole documents
        // List<Person> results = mongoTemplate.findAll(Person.class);

        long total = this.mongoTemplate.count(null, Person.class);
        logger.info("Total number in database: {}", total);

        return total;
    }

    /**
     * This will install a new Person object with my
     * name and random age
     */
    public void insertPersonWithNameJohnathan(double age) {
        Person p = new Person("Johnathan", (int) age);

        mongoTemplate.insert(p);
    }

    /**
     * this will create a {@link Person} collection if the collection does not already exists
     */
    public void createPersonCollection() {
        if (!mongoTemplate.collectionExists(Person.class)) {
            mongoTemplate.createCollection(Person.class);
        }
    }

    /**
     * this will drop the {@link Person} collection if the collection does already exists
     */
    public void dropPersonCollection() {
        if (mongoTemplate.collectionExists(Person.class)) {
            mongoTemplate.dropCollection(Person.class);
        }
    }
}

4)

package com.johnathanmarksmith.mongodb.example.domain;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

/**
 * Date:   6/26/13 / 1:21 PM
 * Author: Johnathan Mark Smith
 * Email:  john@johnathanmarksmith.com
 * <p/>
 * Comments:
 * <p/>
 * This is a Person object that I am going to be using for my demo
 */


@Document
public class Person {

    @Id
    private String personId;

    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getPersonId() {
        return personId;
    }

    public void setPersonId(final String personId) {
        this.personId = personId;
    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(final int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person [id=" + personId + ", name=" + name
                + ", age=" + age + "]";
    }

}

https://github.com/JohnathanMarkSmith/spring-fongo-demo

您可以直接在服务类中注入MongoTemplate和MongoOperations。

尝试自动连接它们然后你应该很好。

更新:

没有使用适当的限定符自动装配(因为你有两个存储库),这是不可能的。 作为Custom类,所有这些都与存储库不同。 如果你只有一个存储库,那么mongotemplate的autowire就足够了,否则你必须在impl中提供限定符,因为创建了两个MongoTempalte bean。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM