Java Code Examples for org.springframework.data.mongodb.core.mapping.MongoMappingContext

The following examples show how to use org.springframework.data.mongodb.core.mapping.MongoMappingContext. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: flow-platform-x   Source File: MongoConfig.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public MongoMappingContext mongoMappingContext() throws ClassNotFoundException {
    CustomizedMappingContext context = new CustomizedMappingContext();
    context.setInitialEntitySet(getInitialEntitySet());
    context.setSimpleTypeHolder(customConversions().getSimpleTypeHolder());
    context.setFieldNamingStrategy(fieldNamingStrategy());

    // add addPersistentEntity for sub types since not registered if called within same thread
    context.addEntity(SmtpConfig.class);
    context.addEntity(TextConfig.class);
    context.addEntity(AuthSecret.class);
    context.addEntity(RSASecret.class);
    context.addEntity(TokenSecret.class);
    context.addEntity(LocalUnixAgentHost.class);
    context.addEntity(SshAgentHost.class);

    return context;
}
 
Example 2
@Bean
public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory, SequenceOption sequenceOption) {
	DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);

	MongoCustomConversions conversions = new MongoCustomConversions(resolverConverter());
	MongoMappingContext mappingContext = new BHBMongoMappingContext();
	mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
	mappingContext.afterPropertiesSet();
	MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mappingContext);
	// _class 剔除
	converter.setTypeMapper(new DefaultMongoTypeMapper(null));
	converter.setCustomConversions(conversions);
	converter.afterPropertiesSet();

	// 需要自增时
	// MongoTemplate template=new DTXDMongoTemplate(mongoDbFactory,converter,
	// sequenceOption);
	MongoTemplate template = new MongoTemplate(mongoDbFactory, converter);
	return template;
}
 
Example 3
Source Project: POC   Source File: MongoConfiguration.java    License: Apache License 2.0 6 votes vote down vote up
@EventListener(ApplicationReadyEvent.class)
public void initIndicesAfterStartup() {

	log.info("Mongo InitIndicesAfterStartup init");
	var init = System.currentTimeMillis();

	var mappingContext = this.mongoTemplate.getConverter().getMappingContext();

	if (mappingContext instanceof MongoMappingContext) {
		var resolver = IndexResolver.create(mappingContext);
		mappingContext.getPersistentEntities().stream().filter(clazz -> clazz.isAnnotationPresent(Document.class))
				.forEach(o -> {
					IndexOperations indexOps = this.mongoTemplate.indexOps(o.getType());
					resolver.resolveIndexFor(o.getType()).forEach(indexOps::ensureIndex);
				});
	}

	log.info("Mongo InitIndicesAfterStartup took: {}", (System.currentTimeMillis() - init));
}
 
Example 4
@Bean
@ConditionalOnMissingBean
public MongoMappingContext mongoMappingContext(MongoCustomConversions conversions)
		throws ClassNotFoundException {
	MongoMappingContext context = new MongoMappingContext();
	context.setInitialEntitySet(new EntityScanner(this.applicationContext)
			.scan(Document.class, Persistent.class));
	Class<?> strategyClass = this.beihuMongoProperties.getFieldNamingStrategy();
	if (strategyClass != null) {
		context.setFieldNamingStrategy(
				(FieldNamingStrategy) BeanUtils.instantiateClass(strategyClass));
	}
	context.setSimpleTypeHolder(conversions.getSimpleTypeHolder());
	return context;
}
 
Example 5
@Bean
@ConditionalOnMissingBean(MongoConverter.class)
public MappingMongoConverter mappingMongoConverter(MongoDbFactory factory,
		MongoMappingContext context, MongoCustomConversions conversions) {
	DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
	MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver,
			context);
	// 去掉 _class项
	mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
	mappingConverter.setCustomConversions(conversions);
	return mappingConverter;
}
 
Example 6
Source Project: spring-fu   Source File: MongoReactiveDataInitializer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(GenericApplicationContext context) {
	MongoReactiveDataAutoConfiguration configuration = new MongoReactiveDataAutoConfiguration();
	context.registerBean(MappingMongoConverter.class, () -> configuration.mappingMongoConverter(context.getBean(MongoMappingContext.class), context.getBean(MongoCustomConversions.class)));
	context.registerBean(SimpleReactiveMongoDatabaseFactory.class, () -> configuration.reactiveMongoDatabaseFactory(this.properties, context.getBean(MongoClient.class)));
	context.registerBean(ReactiveMongoTemplate.class, () -> configuration.reactiveMongoTemplate(context.getBean(ReactiveMongoDatabaseFactory.class), context.getBean(MongoConverter.class)));
}
 
Example 7
Source Project: spring-fu   Source File: MongoDataInitializer.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void initialize(GenericApplicationContext context) {
	MongoDataConfiguration dataConfiguration = new MongoDataConfiguration();
	context.registerBean(MongoCustomConversions.class, dataConfiguration::mongoCustomConversions);
	context.registerBean(MongoMappingContext.class, () -> {
		try {
			return dataConfiguration.mongoMappingContext(context, properties, context.getBean(MongoCustomConversions.class));
		}
		catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	});
	context.registerBean(MongoCustomConversions.class, dataConfiguration::mongoCustomConversions);
}
 
Example 8
Source Project: flow-platform-x   Source File: MongoConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public MongoMappingContext mongoMappingContext() throws ClassNotFoundException {
    FlowMappingContext mappingContext = new FlowMappingContext();
    mappingContext.setInitialEntitySet(getInitialEntitySet());
    mappingContext.setSimpleTypeHolder(customConversions().getSimpleTypeHolder());
    mappingContext.setFieldNamingStrategy(fieldNamingStrategy());

    mappingContext.addCustomizedPersistentEntity(ClassTypeInformation.from(ExecutedCmd.class), "executed_cmd");

    return mappingContext;
}
 
Example 9
Source Project: iot-dc3   Source File: MongoConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public MongoTemplate mongoTemplate(
        MongoClientOptionProperties properties,
        MongoDbFactory factory,
        MongoMappingContext context,
        MongoCustomConversions conversions
) {
    return new MongoTemplate(mongoDbFactory(properties), mappingMongoConverter(factory, context, conversions));
}
 
Example 10
Source Project: iot-dc3   Source File: MongoConfig.java    License: Apache License 2.0 5 votes vote down vote up
public MappingMongoConverter mappingMongoConverter(
        MongoDbFactory factory,
        MongoMappingContext context,
        MongoCustomConversions conversions
) {
    DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);
    MappingMongoConverter mappingConverter = new MappingMongoConverter(dbRefResolver, context);
    mappingConverter.setCustomConversions(conversions);
    mappingConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
    return mappingConverter;
}
 
Example 11
Source Project: canal-mongo   Source File: MultipleMongoConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Primary
@Bean(name = "naiveMongoTemplate")
public MongoTemplate naiveMongoTemplate() throws Exception {
    MappingMongoConverter converter =
            new MappingMongoConverter(new DefaultDbRefResolver(naiveFactory()), new MongoMappingContext());
    converter.setTypeMapper(new DefaultMongoTypeMapper(null));
    return new MongoTemplate(naiveFactory(), converter);
}
 
Example 12
Source Project: canal-mongo   Source File: MultipleMongoConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
@Qualifier("completeMongoTemplate")
public MongoTemplate completeMongoTemplate() throws Exception {
    MappingMongoConverter converter =
            new MappingMongoConverter(new DefaultDbRefResolver(completeFactory()), new MongoMappingContext());
    converter.setTypeMapper(new DefaultMongoTypeMapper(null));
    return new MongoTemplate(completeFactory(), converter);
}
 
Example 13
@Bean(name = "mongoTemplate")
public MongoTemplate createMongoTemplate() throws UnknownHostException {
    MongoClient mongoClient = new MongoClient(host, port);
    //TODO Configure additional MongoDB mongoClient settings if needed
    MongoDbFactory factory = new SimpleMongoDbFactory(mongoClient, database, new UserCredentials(username, password));
    MappingMongoConverter converter = new MappingMongoConverter(new DefaultDbRefResolver(factory), new MongoMappingContext());
    converter.setTypeMapper(new DefaultMongoTypeMapper(null));

    return new MongoTemplate(factory, converter);
}
 
Example 14
Source Project: ZTuoExchange_framework   Source File: MongoConfig.java    License: MIT License 4 votes vote down vote up
@Override
@Bean
public MongoMappingContext mongoMappingContext() {
    MongoMappingContext mappingContext = new MongoMappingContext();
    return mappingContext;
}
 
Example 15
Source Project: ZTuoExchange_framework   Source File: MongoConfig.java    License: MIT License 4 votes vote down vote up
@Override
@Bean
public MongoMappingContext mongoMappingContext() {
    MongoMappingContext mappingContext = new MongoMappingContext();
    return mappingContext;
}
 
Example 16
@Bean
public MongoMappingContext mongoMappingContext() {
  return new MongoMappingContext();
}
 
Example 17
@Bean
public MappingMongoConverter mongoConverter(MongoMappingContext mappingContext, DbRefResolver dbRefResolver) {
  return new MappingMongoConverter(dbRefResolver, mappingContext);
}
 
Example 18
@Bean
public MongoMappingContext mongoMappingContext() {
  return new MongoMappingContext();
}
 
Example 19
@Bean
public MappingMongoConverter mongoConverter(MongoMappingContext mappingContext, DbRefResolver dbRefResolver) {
  return new MappingMongoConverter(dbRefResolver, mappingContext);
}
 
Example 20
@Setup
public void setUp() throws Exception {

	client = MongoClients.create();

	this.mappingContext = new MongoMappingContext();
	this.mappingContext.setInitialEntitySet(Collections.singleton(Customer.class));
	this.mappingContext.afterPropertiesSet();

	DbRefResolver dbRefResolver = new DefaultDbRefResolver(new SimpleMongoClientDatabaseFactory(client, DB_NAME));

	this.converter = new MappingMongoConverter(dbRefResolver, mappingContext);
	this.converter.setCustomConversions(new MongoCustomConversions(Collections.emptyList()));
	this.converter.afterPropertiesSet();

	// just a flat document
	this.documentWith2Properties = new Document("firstname", "Dave").append("lastname", "Matthews");

	// document with a nested one
	Document address = new Document("zipCode", "ABCDE").append("city", "Some Place");
	this.documentWith2PropertiesAnd1Nested = new Document("firstname", "Dave").//
			append("lastname", "Matthews").//
			append("address", address);

	// object equivalent of documentWith2PropertiesAnd1Nested
	this.objectWith2PropertiesAnd1Nested = new Customer("Dave", "Matthews", new Address("zipCode", "City"));

	// a bit more challenging object with list & map conversion.
	objectWithFlatAndComplexPropertiesPlusListAndMap = new SlightlyMoreComplexObject();
	objectWithFlatAndComplexPropertiesPlusListAndMap.id = UUID.randomUUID().toString();
	objectWithFlatAndComplexPropertiesPlusListAndMap.addressList = Arrays.asList(new Address("zip-1", "city-1"),
			new Address("zip-2", "city-2"));
	objectWithFlatAndComplexPropertiesPlusListAndMap.customer = objectWith2PropertiesAnd1Nested;
	objectWithFlatAndComplexPropertiesPlusListAndMap.customerMap = new LinkedHashMap<>();
	objectWithFlatAndComplexPropertiesPlusListAndMap.customerMap.put("dave", objectWith2PropertiesAnd1Nested);
	objectWithFlatAndComplexPropertiesPlusListAndMap.customerMap.put("deborah",
			new Customer("Deborah Anne", "Dyer", new Address("?", "london")));
	objectWithFlatAndComplexPropertiesPlusListAndMap.customerMap.put("eddie",
			new Customer("Eddie", "Vedder", new Address("??", "Seattle")));
	objectWithFlatAndComplexPropertiesPlusListAndMap.intOne = Integer.MIN_VALUE;
	objectWithFlatAndComplexPropertiesPlusListAndMap.intTwo = Integer.MAX_VALUE;
	objectWithFlatAndComplexPropertiesPlusListAndMap.location = new Point(-33.865143, 151.209900);
	objectWithFlatAndComplexPropertiesPlusListAndMap.renamedField = "supercalifragilisticexpialidocious";
	objectWithFlatAndComplexPropertiesPlusListAndMap.stringOne = "¯\\_(ツ)_/¯";
	objectWithFlatAndComplexPropertiesPlusListAndMap.stringTwo = " (╯°□°)╯︵ ┻━┻";

	// JSON equivalent of objectWithFlatAndComplexPropertiesPlusListAndMap
	documentWithFlatAndComplexPropertiesPlusListAndMap = Document.parse(
			"{ \"_id\" : \"517f6aee-e9e0-44f0-88ed-f3694a019f27\", \"intOne\" : -2147483648, \"intTwo\" : 2147483647, \"stringOne\" : \"¯\\\\_(ツ)_/¯\", \"stringTwo\" : \" (╯°□°)╯︵ ┻━┻\", \"explicit-field-name\" : \"supercalifragilisticexpialidocious\", \"location\" : { \"x\" : -33.865143, \"y\" : 151.2099 }, \"objectWith2PropertiesAnd1Nested\" : { \"firstname\" : \"Dave\", \"lastname\" : \"Matthews\", \"address\" : { \"zipCode\" : \"zipCode\", \"city\" : \"City\" } }, \"addressList\" : [{ \"zipCode\" : \"zip-1\", \"city\" : \"city-1\" }, { \"zipCode\" : \"zip-2\", \"city\" : \"city-2\" }], \"customerMap\" : { \"dave\" : { \"firstname\" : \"Dave\", \"lastname\" : \"Matthews\", \"address\" : { \"zipCode\" : \"zipCode\", \"city\" : \"City\" } }, \"deborah\" : { \"firstname\" : \"Deborah Anne\", \"lastname\" : \"Dyer\", \"address\" : { \"zipCode\" : \"?\", \"city\" : \"london\" } }, \"eddie\" : { \"firstname\" : \"Eddie\", \"lastname\" : \"Vedder\", \"address\" : { \"zipCode\" : \"??\", \"city\" : \"Seattle\" } } }, \"_class\" : \"org.springframework.data.mongodb.core.convert.MappingMongoConverterBenchmark$SlightlyMoreComplexObject\" }");

}