Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: Spring not using my custom converter

  1. #11
    Join Date
    Nov 2011
    Posts
    10

    Default Is anyone using base-package to register converter?

    I have a workable version for converter registion:
    Code:
    	<mongo:mapping-converter >
    		<mongo:custom-converters>
    			<mongo:converter>
    				<bean class="com.abc.Converter.mongo.DBObjectToDateTime" />
    			</mongo:converter>
    			<mongo:converter>
    				<bean class="com.abc.Converter.mongo.DateTimeToDBObject" />
    			</mongo:converter>
    		</mongo:custom-converters>
    	</mongo:mapping-converter>
    
    	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    		<constructor-arg name="mongoConverter" ref="mappingConverter" />
    	</bean>
    but if I am trying scanning style for converter registration, it doesn't work until now:
    Code:
    	<mongo:mapping-converter base-package="com.abc.*.entities">
    		<mongo:custom-converters base-package="com.abc.Converter.mongo" />
    	</mongo:mapping-converter>
    
    	<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    		<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
    		<constructor-arg name="mongoConverter" ref="mappingConverter" />
    	</bean>
    where com.abc.*.entities is the place for domain class, and the package com.abc.Converter.mongo is the place for converters like DateTimeToDBObject and DBObjectToDateTime.
    anything I missed here?

  2. #12
    Join Date
    Nov 2011
    Posts
    10

    Default Can we register converter factory?

    for example, if we want mongodb only store the id of some part of entities, the converter seems like:
    Code:
    public final class DBObjectToEntityFactory implements ConverterFactory<DBObject, BaseEntity> {
    	
    	public <T extends BaseEntity> Converter<DBObject, T> getConverter(Class<T> targetType) {
    		return new DBObjectToManagedEntityConverter<T>();
    	}
    
    	private final class DBObjectToManagedEntityConverter<T extends BaseEntity> implements Converter<DBObject, T> {
    		@Autowired CrudRepository<T, Long> repo;
    
    		public T convert(DBObject source){
    			return (T) repo.findOne((Long) source.get("id"));
    		}
    	}
    
    }
    
    public class EntityToDBObject implements Converter<BaseEntity, DBObject> {
    
    	public DBObject convert(BaseEntity source) {
    		DBObject dbo = new BasicDBObject();
    		dbo.put("id", source.getId());
    		return dbo;
    	}
    
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •