Skip to content

Instantly share code, notes, and snippets.

@oharsta
Created March 27, 2016 15:46
Show Gist options
  • Save oharsta/7946bc55239b9de2866e to your computer and use it in GitHub Desktop.
Save oharsta/7946bc55239b9de2866e to your computer and use it in GitHub Desktop.
Inject singleton instance into Java (+8) classes
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.joda.JodaModule;
import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
public interface JsonMapper {
ObjectMapper mapper = ObjectMapperWrapper.init();
class ObjectMapperWrapper {
private static com.fasterxml.jackson.databind.ObjectMapper init() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModule(new JodaModule());
objectMapper.registerModule(new AfterburnerModule());
return objectMapper;
}
}
}
public class NeedJsonMapper implements JsonMapper {
public void doSomething() {
mapper.writeValueAsString("whatever");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment