如何正确配置SimpleMongoRepository?

时间:2019-01-02 09:43:38

标签: java spring mongodb

我最近正在使用MongoDB,并且存在这样的问题。我需要配置SimpleMongoRepository,我以本主题为例(以上一篇文章为例)Spring Data Mongo Repository:: Common shared method across all Repo issue

配置:

@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(MultipleMongoProperties.class)
@EnableMongoRepositories(basePackages = "productcatalog.repository")
public class MultipleMongoConfig {

    private final MultipleMongoProperties mongoProperties;

    @Bean(name = "mongoTemplate")
    public MongoTemplate mongoTemplate() {
        return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary()));
    }

    @Primary
    @Bean(name = "primaryMongoTemplate")
    public MongoTemplate primaryMongoTemplate() {
        return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary()));
    }

    @Bean(name = "secondaryMongoTemplate")
    public MongoTemplate secondaryMongoTemplate() {
        return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary()));
    }

    @Bean("primary")
    public SimpleMongoRepository<ProductModel, String> getPrimaryProductRepository() {
        TypeInformation<ProductModel> typeInformation = ClassTypeInformation.from(ProductModel.class);
        MongoPersistentEntity<ProductModel> mongoPersistentEntity = new BasicMongoPersistentEntity<>(typeInformation);
        return new SimpleMongoRepository<>(entityInformationFor(mongoPersistentEntity, String.class), primaryMongoTemplate());
    }

    @Bean("secondary")
    public SimpleMongoRepository<ProductModel, String> getSecondaryProductRepository() {
        TypeInformation<ProductModel> typeInformation = ClassTypeInformation.from(ProductModel.class);
        MongoPersistentEntity<ProductModel> mongoPersistentEntity = new BasicMongoPersistentEntity<>(typeInformation);
        return new SimpleMongoRepository<>(entityInformationFor(mongoPersistentEntity, String.class), secondaryMongoTemplate());
    }

    @Bean("productRepository")
    public MongoRepository<ProductModel, String> getRepository() {
        return new MongoRepositoryImpl<>(getPrimaryProductRepository(), getSecondaryProductRepository());
    }

    @Bean
    @Primary
    public MongoDbFactory primaryFactory(final MongoProperties mongo) {
        return getMongoDbFactory(mongo);
    }

    @Bean
    public MongoDbFactory secondaryFactory(final MongoProperties mongo) {
        return getMongoDbFactory(mongo);
    }

    public <T, ID extends Serializable> MongoEntityInformation<T, ID> entityInformationFor(MongoPersistentEntity<T> entity,
                                                                                           Class<ID> idType) {
        return new MappingMongoEntityInformation<>(entity, idType);
    }

    private SimpleMongoDbFactory getMongoDbFactory(MongoProperties mongo) {
        MongoCredential mongoCredential = MongoCredential.createCredential(mongo.getUsername(), mongo.getAuthenticationDatabase(), mongo.getPassword());
        List<MongoCredential> mongoCredentialList = Collections.singletonList(mongoCredential);
        ServerAddress serverAddress = new ServerAddress(mongo.getHost(), mongo.getPort());
        MongoClient mongoClient = new MongoClient(serverAddress, mongoCredentialList);
        return new SimpleMongoDbFactory(mongoClient,
                mongo.getDatabase());
    }
}

我的模特:

@Data
@AllArgsConstructor
@NoArgsConstructor
@Document(collection = "products")
public class ProductModel {

    @Id
    private String id;

    @Field(value = "name")
    private String name;
}

但是我有一个问题,在我的SimpleMongoRepository配置中,在此行中

MongoPersistentEntity<ProductModel> mongoPersistentEntity = new BasicMongoPersistentEntity<>(typeInformation);

idProperty配置为null,然后在使用该方法的后续方法中获得NPE。如何解决这个问题?

java.lang.NullPointerException: null
at org.springframework.data.mongodb.repository.support.MappingMongoEntityInformation.getIdAttribute(MappingMongoEntityInformation.java:101) ~[spring-data-mongodb-1.10.9.RELEASE.jar:na]
at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.findAll(SimpleMongoRepository.java:211) ~[spring-data-mongodb-1.10.9.RELEASE.jar:na]
at productcatalog.repository.MongoRepositoryImpl.findAll(MongoRepositoryImpl.java:97) ~[classes/:na]
at productcatalog.service.impl.ProductServiceImpl.getProductsByIds(ProductServiceImpl.java:36) ~[classes/:na]
at productcatalog.controller.ProductCatalogController.getProductsByIds(ProductCatalogController.java:90) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_181]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_181]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_181]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_181]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:635) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:742) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) ~[tomcat-embed-websocket-8.5.23.jar:8.5.23]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:108) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:803) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:868) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1459) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) [tomcat-embed-core-8.5.23.jar:8.5.23]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_181]
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) [tomcat-embed-core-8.5.23.jar:8.5.23]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]

我有存储库:

public class MongoRepositoryImpl<T, K extends Serializable> implements MongoRepository<T, K> {

    private SimpleMongoRepository<T, K> primaryRepository;

    private SimpleMongoRepository<T, K> secondaryRepository;

    private SimpleMongoRepository<T, K> actualSource;

    private ReadWriteLock lock = new ReentrantReadWriteLock();

    private Lock writeLock = lock.writeLock();
    private Lock readLock = lock.readLock();

    private static final Integer DEFAULT_SIZE = 10;

    public MongoRepositoryImpl(SimpleMongoRepository<T, K> primaryRepository, SimpleMongoRepository<T, K> secondaryRepository) {
        this.primaryRepository = primaryRepository;
        this.secondaryRepository = secondaryRepository;
        actualSource = primaryRepository;
    }

 @Override
    public T findOne(K id) {
        T findResult;
        try {
            readLock.lock();
            findResult = actualSource.findOne(id);
        } finally {
            readLock.unlock();
        }
        return findResult;
    }


    @Override
    public List<T> findAll() {
        List<T> result;
        try {
            readLock.lock();
            result = actualSource.findAll();
        } finally {
            readLock.unlock();
        }
        return result;
    }

    @Override
    public Iterable<T> findAll(Iterable<K> ids) {
        Iterable<T> result;
        try {
            readLock.lock();
            result = actualSource.findAll(ids);
        } finally {
            readLock.unlock();
        }
        return result;
    }

除findAll(可迭代id)外,所有方法均有效,当我使用此方法时,我得到了NPE,因为idProperty = null;我该如何解决?

2 个答案:

答案 0 :(得分:1)

我认为您需要org.springframework.data.mongodb.repository.support.MongoRepositoryFactory的专门实例。

在配置类中设置工厂:

@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(MultipleMongoProperties.class)
@EnableMongoRepositories(basePackages = "productcatalog.repository", repositoryFactoryBeanClass = MultipleMongoRepositoryFactoryBean.class)
public class MultipleMongoConfig {
    /* ...code... */
} 

然后,在您的MultipleMongoRepositoryFactoryBean中:

public class MultipleMongoRepositoryFactoryBean<R extends MongoRepository<T, I>, T, I extends Serializable> extends MongoRepositoryFactoryBean<R, T, I> {

    protected RepositoryFactorySupport getFactoryInstance(MongoOperations operations) {
         return new MultipleMongoRepositoryFactory<T, I>(operations);
    }
}

然后根据元数据在MongoRepositoryFactory内创建自己的存储库。

public class MultipleMongoRepositoryFactory<T, ID extends Serializable> extends MongoRepositoryFactory {

    private final MongoOperations mongoOperations;

    public MultipleMongoRepositoryFactory(MongoOperations mongoOperations) {
        super(mongoOperations);
    }

    @Override
    protected Object getTargetRepository(RepositoryMetadata metadata) {
        /* Add personalyzed stuff here */
        return super.getTargetRepository(metadata);
    }

}

请注意,MongoRepositoryFactory::getEntityInformation(EntityMetadata)有一个返回MongoEntityInformation的方法,该方法实际上是从您的配置中引发异常。因此,如果您真的需要手动配置存储库,则可以尝试注入MongoRepositoryFactory

请注意,仅当您需要在存储库实现中实现特定内容时,才需要这样做。如果只使用默认接口,那么Spring Data不会提供任何类似的东西。

编辑:您可以在Spring Data的文档中查看更多信息。 https://docs.spring.io/spring-data/mongodb/docs/1.6.4.RELEASE/reference/html/#repositories.custom-implementations。这是从1.6.4版本中检索到的,这是我遇到与您自己相同的问题的版本。对于当前版本,请参阅https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#repositories.custom-implementations

答案 1 :(得分:0)

我也面临同样的挑战,不幸的是找不到合适的示例来实现此目的。

从堆栈跟踪中可以看到,SimpleMongoRepository在内部拥有MongoEntityInformation,在3种情况下,它显式搜索Id属性:findAllById()existsById()和{ {1}}最终呼叫deleteById()。因此,您必须确保它熟悉您的实体及其ID属性。

这是您的操作方式:

entityInformation.getIdAttribute()

...与“中学”相同。

希望有帮助。