春季启动MongoRepository

时间:2019-05-08 13:25:35

标签: spring-boot spring-data-mongodb multiple-databases mongorepository

在这里,我在mongodb中配置了两个数据库。如本教程(link)中所述。所以基本上我覆盖了MongoDataAutoConfigurationMongoProperties的实现。

yml属性文件:

spring:
  autoconfigure:
       exclude: 
org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration

mongodb:
   primary:
     host: 127.0.0.1
     port: 27017
     database: db_admin_crm
   secondary:
     host: 127.0.0.1
     port: 27017
     database: lead_forms

MultipleMongoProperties类:

@Data
@ConfigurationProperties(prefix = "mongodb")
public class MultipleMongoProperties {

 private MongoProperties primary = new MongoProperties();
 private MongoProperties secondary = new MongoProperties();
    //getters and setters
}

MultipleMongoConfig类:

@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(MultipleMongoProperties.class)
public class MultipleMongoConfig {

@Autowired
private final MultipleMongoProperties mongoProperties;

public MultipleMongoConfig() {
    mongoProperties = null;
}


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

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

@Bean
@Primary
public MongoDbFactory primaryFactory(final MongoProperties mongo) throws Exception {
    return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
            mongo.getDatabase());
}

@Bean
public MongoDbFactory secondaryFactory(final MongoProperties mongo) throws Exception {
    return new SimpleMongoDbFactory(new MongoClient(mongo.getHost(), mongo.getPort()),
            mongo.getDatabase());
  }
}

PrimaryMongoConfig:

@Configuration
@EnableMongoRepositories(basePackages = "io.crm.service.repositories",
    mongoTemplateRef = "primaryMongoTemplate")
public class PrimaryMongoConfig{
}

SecondaryMongoConfig:

@Configuration
@EnableMongoRepositories(basePackages = "io.crm.service.repositories.report.repositories",
    mongoTemplateRef = "secondaryMongoTemplate")
public class SecondaryMongoConfig {
}

存储库类:

   @RepositoryRestResource(collectionResourceRel = "users",path = "users",excerptProjection = UserProjection.class)
   public interface UserRepository extends MongoRepository<User, String> {

 }

用户模型类:

@Id
private String id;
private String email;
private String name;
private String businessName;
private String phone;
private String address;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private Date createdTime;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
private Date updatedTime;
@Field("bookletSignUps")
@DBRef
private List<BookletSignUp> bookletSignUps;
@Field("eventSignUps")
@DBRef
private List<EventSignUp> eventSignUps;
@Field("infoSignUps")
@DBRef
private List<InfoSignUp> infoSignUps;
@Field("webinarSignUps")
@DBRef
private List<WebinarSignUp> webinarSignUps;

投影:

@Projection(name = "userExcerpt", types = User.class)
public interface UserProjection {

String getId();
String getName();
String getEmail();
String getBusinessName();
String getPhone();
String getAddress();
Date getCreatedTime();
Date getUpdatedTime();
List<BookletSignUp> getBookletSignUps();
List<EventSignUp> getEventSignUps();
List<InfoSignUp> getInfoSignUps();
List<WebinarSignUp> getWebinarSignUps();

}

但是,当我尝试向REST端点http://localhost:9090/users/发出GET请求时,我在此处获取java.lang.IllegalArgumentException: Couldn't find PersistentEntity for type class io.crm.service.models.User!异常。这里可能出什么问题了?想法将不胜感激。提前致谢。

0 个答案:

没有答案