Skip to content

Instantly share code, notes, and snippets.

@jankronquist
Created December 7, 2011 14:58
Show Gist options
  • Save jankronquist/1443114 to your computer and use it in GitHub Desktop.
Save jankronquist/1443114 to your computer and use it in GitHub Desktop.
Autowiring Morphia entities
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import com.google.code.morphia.AbstractEntityInterceptor;
import com.google.code.morphia.Morphia;
import com.google.code.morphia.mapping.Mapper;
import com.mongodb.DBObject;
public class SpringAutowiringEntityInterceptor extends AbstractEntityInterceptor {
@Autowired
private ApplicationContext applicationContext;
@Autowired
private Morphia morphia;
@PostConstruct
public void postConstruct() {
morphia.getMapper().addInterceptor(this);
}
@Override
public void preLoad(Object ent, DBObject dbObj, Mapper mapr) {
applicationContext.getAutowireCapableBeanFactory().autowireBean(ent);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment