Created
March 27, 2016 15:46
-
-
Save oharsta/7946bc55239b9de2866e to your computer and use it in GitHub Desktop.
Inject singleton instance into Java (+8) classes
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
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; | |
} | |
} | |
} |
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
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