Created
April 5, 2012 15:58
-
-
Save tburch/2312133 to your computer and use it in GitHub Desktop.
ObjectifyFactory FactoryBean with automatic Entity registration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<bean id="objectifyFactoryFactoryBean" class="com.tristanburch.objectify.ObjectifyFactoryFactoryBean"> | |
<property name="basePackage" value="com.tristanburch.model" /> | |
</bean> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tristanburch.objectify; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.beans.factory.FactoryBean; | |
import org.springframework.beans.factory.InitializingBean; | |
import org.springframework.beans.factory.config.BeanDefinition; | |
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; | |
import org.springframework.core.type.filter.AnnotationTypeFilter; | |
import com.googlecode.objectify.ObjectifyFactory; | |
import com.googlecode.objectify.ObjectifyService; | |
import com.googlecode.objectify.annotation.Entity; | |
import com.googlecode.objectify.annotation.EntitySubclass; | |
import com.googlecode.objectify.impl.translate.opt.BigDecimalLongTranslatorFactory; | |
import com.googlecode.objectify.impl.translate.opt.joda.JodaTimeTranslators; | |
public class ObjectifyFactoryFactoryBean implements FactoryBean<ObjectifyFactory>, InitializingBean { | |
private static final Logger log = LoggerFactory.getLogger(ObjectifyFactoryFactoryBean.class); | |
private ObjectifyFactory objectifyFactory; | |
private String basePackage; | |
@Override | |
public ObjectifyFactory getObject() throws Exception { | |
return objectifyFactory; | |
} | |
@Override | |
public Class<?> getObjectType() { | |
return ObjectifyFactory.class; | |
} | |
@Override | |
public boolean isSingleton() { | |
return true; | |
} | |
@Override | |
public void afterPropertiesSet() throws Exception { | |
ObjectifyFactory objectifyFactory = ObjectifyService.factory(); | |
JodaTimeTranslators.add(objectifyFactory); | |
objectifyFactory.getTranslators().add(new BigDecimalLongTranslatorFactory()); | |
log.info("Finding classes with @Entity or @EntitySubclass annotations in {} to register on ObjectifyFactory", basePackage); | |
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false); | |
scanner.addIncludeFilter(new AnnotationTypeFilter(Entity.class)); | |
scanner.addIncludeFilter(new AnnotationTypeFilter(EntitySubclass.class)); | |
for (BeanDefinition bd : scanner.findCandidateComponents(basePackage)) { | |
log.info("Registering class {}", bd.getBeanClassName()); | |
objectifyFactory.register(Class.forName(bd.getBeanClassName())); | |
} | |
this.objectifyFactory = objectifyFactory; | |
} | |
public void setBasePackage(String basePackage) { | |
this.basePackage = basePackage; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tristanburch.objectify; | |
import com.googlecode.objectify.ObjectifyFactory; | |
import com.googlecode.objectify.ObjectifyService; | |
import com.googlecode.objectify.impl.translate.opt.BigDecimalLongTranslatorFactory; | |
import com.googlecode.objectify.impl.translate.opt.joda.JodaTimeTranslators; | |
public class ObjectifyFactoryService { | |
public static ObjectifyFactory createInstance() { | |
ObjectifyFactory objectifyFactory = ObjectifyService.factory(); | |
JodaTimeTranslators.add(objectifyFactory); | |
objectifyFactory.getTranslators().add(new BigDecimalLongTranslatorFactory()); | |
return objectifyFactory; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment