【问题标题】:MongoDB -Consider defining a bean of type 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' in your configurationMongoDB - 考虑在你的配置中定义一个 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' 类型的 bean
【发布时间】:2017-11-20 03:42:45
【问题描述】:

我的学生实体类

package com.example.entity;

import java.io.Serializable;

import javax.persistence.Id;

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

@Document(collection = "student")
public class StudentMongo implements Serializable {

    private static final long serialVersionUID = 8764013757545132519L;

    @Id
    private Long id;

    private String name;

    public String getName() {
        return name;
    }

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

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

}

我的仓库

package com.example.repository;

import javax.annotation.Resource;

import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.repository.query.MongoEntityInformation;
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;
import org.springframework.stereotype.Repository;

import com.example.entity.StudentMongo;

@Repository
public class StudentMongoRepository extends SimpleMongoRepository<StudentMongo, Long> {

    @Resource
    MongoOperations mongoOperations;


    public StudentMongoRepository(MongoEntityInformation<StudentMongo, Long> metadata, MongoOperations mongoOperations) {
        super(metadata, mongoOperations);
    }

}

我的配置类

@Configuration
@EnableMongoRepositories(basePackageClasses = {com.example.repository.StudentMongoRepository.class})
public class MongoConfiguration {

}

Spring 启动应用程序

当我尝试启动应用程序时,我得到以下应用程序

2017-11-20 09:04:48.937 ERROR 23220 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of constructor in com.example.repository.StudentMongoRepository required a bean of type 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' in your configuration.

考虑在你的配置中定义一个 'org.springframework.data.mongodb.repository.query.MongoEntityInformation' 类型的 bean 如spring框架所说,如何创建EntityInformation bean 运行我的应用程序时遇到上述问题。如何传递实体信息

建议我如何使用 SimpleMongorepository

【问题讨论】:

  • 将@Service 注解添加到您的 mongoOperations 类中。
  • @Habil 为什么要在 mongoOperations 上使用 @Service??

标签: spring mongodb spring-boot spring-data-mongodb


【解决方案1】:

首先我想说的是,请不要使用实现类而不是使用接口

声明一个接口并从MongoRepository&lt;T,ID&gt; 扩展它,spring 将提供开箱即用的实现。

@Repository
public interface StudentMongoRepository extends MongoRepository<T,ID>{

@Autowired
private MongoOperations mongoOperations;

这应该可行。

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我解决了将 Spring Data 的 MongoEntityInformationSupport 外部化。

    这样做:

    创建这三个类:

    public final class MongoEntityInformationSupport {
    
        private MongoEntityInformationSupport() {}
    
        /**
         * Factory method for creating {@link MongoEntityInformation}.
         *
         * @param entity must not be {@literal null}.
         * @param idType can be {@literal null}.
         * @return never {@literal null}.
         */
        @SuppressWarnings("unchecked")
        public static <T, ID> MongoEntityInformation<T, ID> entityInformationFor(MongoPersistentEntity<?> entity,
                                                                          @Nullable Class<?> idType) {
    
            Assert.notNull(entity, "Entity must not be null!");
    
            MappingMongoEntityInformation<T, ID> entityInformation = new MappingMongoEntityInformation<T, ID>(
                    (MongoPersistentEntity<T>) entity, (Class<ID>) idType);
    
            return ClassUtils.isAssignable(Persistable.class, entity.getType())
                    ? new PersistableMongoEntityInformation<T, ID>(entityInformation) : entityInformation;
        }
    }
    

    Ps.:把PersistableMongoEntityInformationMongoEntityInformationSupport放在同一个包里

    @RequiredArgsConstructor //<-- Lombok
    class PersistableMongoEntityInformation<T, ID> implements MongoEntityInformation<T, ID> {
    
        private final @NonNull
        MongoEntityInformation<T, ID> delegate;
    
        /*
         * (non-Javadoc)
         * @see org.springframework.data.mongodb.repository.MongoEntityInformation#getCollectionName()
         */
        @Override
        public String getCollectionName() {
            return delegate.getCollectionName();
        }
    
        /*
         * (non-Javadoc)
         * @see org.springframework.data.mongodb.repository.MongoEntityInformation#getIdAttribute()
         */
        @Override
        public String getIdAttribute() {
            return delegate.getIdAttribute();
        }
    
        /*
         * (non-Javadoc)
         * @see org.springframework.data.repository.core.EntityInformation#isNew(java.lang.Object)
         */
        @Override
        @SuppressWarnings("unchecked")
        public boolean isNew(T t) {
    
            if (t instanceof Persistable) {
                return ((Persistable<ID>) t).isNew();
            }
    
            return delegate.isNew(t);
        }
    
        /*
         * (non-Javadoc)
         * @see org.springframework.data.repository.core.EntityInformation#getId(java.lang.Object)
         */
        @Override
        @SuppressWarnings("unchecked")
        public ID getId(T t) {
    
            if (t instanceof Persistable) {
                return ((Persistable<ID>) t).getId();
            }
    
            return delegate.getId(t);
        }
    
        /*
         * (non-Javadoc)
         * @see org.springframework.data.repository.core.support.PersistentEntityInformation#getIdType()
         */
        @Override
        public Class<ID> getIdType() {
            return delegate.getIdType();
        }
    
        /*
         * (non-Javadoc)
         * @see org.springframework.data.repository.core.support.EntityMetadata#getJavaType()
         */
        @Override
        public Class<T> getJavaType() {
            return delegate.getJavaType();
        }
    }
    

    我创建这个 MongoHelper 只是为了简化

    public class MongoHelper {
        public static MongoEntityInformation getEntityInformationFor(Class clazz, Class idClazz) {
            TypeInformation typeInformation = ClassTypeInformation.from(clazz);
            MongoPersistentEntity mongoPersistentEntity = new BasicMongoPersistentEntity(typeInformation);
            return MongoEntityInformationSupport.entityInformationFor(mongoPersistentEntity, idClazz);
        }
    }
    

    终于

    您可以这样使用(示例为QuerydslMongoPredicateExecutor):

    public class TicketRepositoryCustomImpl extends QuerydslMongoPredicateExecutor<Ticket> implements TicketRepositoryCustom {
    
        //The MongoOperations will be autowired
        public TicketRepositoryCustomImpl(MongoOperations mongoOperations) {
            super(MongoHelper.getEntityInformationFor(Ticket.class, String.class), mongoOperations);
        }
    
        @Override
        public Optional<Ticket> findWithFilter(TicketFilter filter) {
            BooleanBuilder builder = new BooleanBuilder();
            //populate the builder
            return findOne(builder.getValue());
        }
    }
    

    【讨论】:

    • private final @NonNull MongoEntityInformation 委托如何;初始化?
    【解决方案3】:

    试试这个:- 不要让它自动装配,只需像这样创建一个变量。 私有的 MongoTemplate mongoTemplate;

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    【解决方案4】:
    XXService extends PagingAndSortingRepository<XX, String>{
        
    }
    ----------------------------------------------------------------
    
    @Service
    public class XXXmpl extends SimpleMongoRepository<XX, String> implements XXService {
    
        public XXImpl(MongoOperations mongoOperations) {
            super(new MongoRepositoryFactory(mongoOperations).getEntityInformation(XXX.class), mongoOperations);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-06-22
      • 1970-01-01
      • 2020-08-01
      • 2019-02-14
      • 2018-12-21
      • 2019-05-10
      • 2019-09-04
      • 2020-05-23
      • 2019-02-05
      相关资源
      最近更新 更多