使用注释

时间:2017-08-07 06:48:47

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

我正在尝试仅使用注释向我的spring启动应用程序中的所有存储库添加自定义行为。官方文档涉及JPA而不涉及MongoDB。

我还没有创建任何自定义方法,因为我想检查默认设置是否正常。

我的代码如下:



    src/main/java/com/test/shipper/database:
    MongoConfig.java

    src/main/java/com/test/shipper/modal:
    Shipper.java

    src/main/java/com/test/shipper/repository:
    BaseRepositoryFactoryBean.java
    BaseRepositoryImpl.java
    BaseRepository.java
    ShipperRepository.java

    src/main/java/com/test/shipper/service:
    ShipperService.java

ShipperService.java



    public class ShipperService {
        @Autowired
        private ShipperRepository repository;

        public Shipper createShipper(Shipper shipper){

            repository.deleteAll();

            Shipper shipper1 = new Shipper("123","shipper123");
            repository.insert(shipper1);

            Shipper shipper2 = new Shipper("234","shipper234");
            repository.insert(shipper2);
            System.out.println("Find All!!");
            List  shippers = repository.findAll();
            for (Shipper car: shippers) {System.out.println(car);}
            repository.delete(shipper2.getShipperId());
            shippers = repository.findAll();
            for (Shipper car: shippers) {
                System.out.println(car);
            }
            return null;
        }
    }

MongoConfig.java



    @Configuration
    @EnableMongoRepositories(basePackages = "com.test.shipper",
            repositoryFactoryBeanClass = com.test.shipper.repository.BaseRepositoryFactoryBean.class)
    public class MongoConfig extends AbstractMongoConfiguration {

        @Value("test")
        private String dbName;

        @Value("127.0.0.1")
        private String host;

        @Value("27017")
        private int port;

        @Override
        protected String getDatabaseName() {
            return dbName;
        }
        @Bean
        @Override
        public Mongo mongo() throws Exception {
            return new MongoClient(host, port);
        }
        @Bean
        public MongoTemplate mongoTemplate() throws Exception {
            return new MongoTemplate(mongo(), dbName);
        }
    }

BaseRepository.java

@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable>
        extends MongoRepository<T, ID>
{

}

BaseRepositoryImpl.java

@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable>
        extends MongoRepository<T, ID>
{

}

BaseRepositoryFactoryBean.java



    @NoRepositoryBean
    public class BaseRepositoryImpl
            extends SimpleMongoRepository implements BaseRepository
    {
        private MongoOperations mongoOperations;

        public BaseRepositoryImpl(MongoEntityInformation metadata,
                                  MongoOperations mongoOperations) {
            super(metadata, mongoOperations);
            this.mongoOperations = mongoOperations;
        }

    }

ShipperRepository.java



    public class BaseRepositoryFactoryBean, T, I extends Serializable>
            extends MongoRepositoryFactoryBean {

        public BaseRepositoryFactoryBean(Class repositoryInterface) {

            super(repositoryInterface);
        }

        @Override
        protected RepositoryFactorySupport getFactoryInstance(
                MongoOperations operations) {
            return new BaseMongoRepositoryFactory( operations );
        }

        private static class BaseMongoRepositoryFactory
                extends MongoRepositoryFactory {

            private MongoOperations mongo;
            public BaseMongoRepositoryFactory(MongoOperations mongoOperations) {
                super(mongoOperations);
                this.mongo = mongoOperations;
            }

            @SuppressWarnings("unchecked")
            protected Object getTargetRepository(RepositoryMetadata metadata) {

                TypeInformation information =  ClassTypeInformation.from((Class)metadata.getDomainType());
                MongoPersistentEntity pe = new BasicMongoPersistentEntity(information);
                MongoEntityInformation mongometa =
                        new MappingMongoEntityInformation(pe);

                return new BaseRepositoryImpl( mongometa,  mongo);
            }

            protected Class getRepositoryBaseClass(RepositoryMetadata metadata) {

                return BaseRepository.class;
            }
        }
    }

Shipper.java



    @Repository
    public interface ShipperRepository
            extends BaseRepository{
    }

当应用程序启动时,我收到以下异常。




     @Document(collection = "shipper")
        public class Shipper{

            @Id
            private String shipperId;
            private String shipperName;
            public Shipper() {
            }
            public Shipper(String shipperId, String shipperName) {
                this.shipperId = shipperId;
                this.shipperName = shipperName;
            }

            public String getShipperId() {
                return shipperId;
            }

            public void setShipperId(String shipperId) {
                this.shipperId = shipperId;
            }

            public String getShipperName() {
                return shipperName;
            }

            public void setShipperName(String shipperName) {
                this.shipperName = shipperName;
            }
        }

可能是什么问题?

0 个答案:

没有答案