@@ -30,12 +30,12 @@ class Person {
3030
3131// inspect an object
3232List<Person > people = query. getResultList();
33- ObjectStringifier stringifier = new ObjectStringifier (people);
34- stringifier . setMode (Mode . PROMISCUOUS );
35- // stringifier.setBeanFactory (...);
33+ Stringify stringify = Stringify . of (people);
34+ stringify . mode (Mode . PROMISCUOUS );
35+ // stringify.beanFactory (...);
3636assert " <Person id=15, parent=<Person id=16, parent=null, "
3737 + " childs=[(↻)], account=⁂Lazy>, childs=[], "
38- + " account=⁂Lazy>" . equals(stringifier . toString());
38+ + " account=⁂Lazy>" . equals(stringify . toString());
3939```
4040
4141 ### In Quiet mode
@@ -51,11 +51,11 @@ class Person {
5151
5252// inspect an object
5353List<Person > people = query. getResultList();
54- ObjectStringifier stringifier = new ObjectStringifier (people);
55- stringifier . setMode (Mode . QUIET );
54+ Stringify stringify = Stringify . of (people);
55+ stringify . mode (Mode . QUIET );
5656assert " <Person id=15, parent=<Person id=16, parent=null, "
5757 + " childs=[(↻)], account=⁂Lazy>, childs=[], "
58- + " account=⁂Lazy>" . equals(stringifier . toString());
58+ + " account=⁂Lazy>" . equals(stringify . toString());
5959```
6060
6161## Features
@@ -83,9 +83,100 @@ Stringify Object for Java is designed to inspect complex objects that can have c
8383 * Lombok can't ** detect cycles** is object graph, which implies ` StackOverflowException ` being thrown in that case
8484 * Lombok can't detect a ** lazy loaded entities** , which leads to force loading it from JPA by invoking SQL statements. It's typical ** n+1 problem** , but with nasty consequences - your ` toString() ` method is invoking SQL without your knowledge!!
8585
86+ ## Configuration
87+
88+ Configuration is done in two ways: declarative - using Java's service loader mechanism,
89+ and programmatic.
90+
91+ ### Configuration using Service Loader
92+
93+ A ` Configurator ` interface is intended to be implemented in user code, and assigned
94+ to [ Service Loader] ( https://www.baeldung.com/java-spi ) mechanism.
95+
96+ To do that, create on your classpath, a file:
97+
98+ ` /META-INF/services/pl.wavesoftware.utils.stringify.spi.Configurator `
99+
100+ In that file, place a fully qualified class name of your class that implements
101+ ` Configurator ` interface. It should be called first time you use an Stringify to inspect
102+ an object:
103+
104+ ```
105+ # classpath:/META-INF/services/pl.wavesoftware.utils.stringify.spi.Configurator
106+ org.acmecorp.StringifyConfigurator
107+ ```
108+
109+ Then implement that class in your code:
110+
111+ ``` java
112+ package org.acmecorp ;
113+
114+ import pl.wavesoftware.utils.stringify.api.Configuration ;
115+ import pl.wavesoftware.utils.stringify.spi.Configurator ;
116+
117+ public final class StringifyConfigurator implements Configurator {
118+
119+ @Override
120+ public void configure (Configuration configuration ) {
121+ configuration. beanFactory(new SpringBeanFactory ());
122+ }
123+ }
124+ ```
125+
126+ with example Spring based BeanFactory:
127+
128+ ``` java
129+ package org.acmecorp ;
130+
131+ import org.springframework.context.event.ContextRefreshedEvent ;
132+ import org.springframework.context.ApplicationContext ;
133+ import org.springframework.context.annotation.Configuration ;
134+
135+ import pl.wavesoftware.utils.stringify.spi.BeanFactory ;
136+ import pl.wavesoftware.utils.stringify.spi.BootingAware ;
137+
138+ @Configuration
139+ class SpringBeanFactory implements BeanFactory , BootingAware {
140+ private static ApplicationContext context;
141+
142+ @EventListener (ContextRefreshedEvent . class)
143+ void onRefresh (ContextRefreshedEvent event ) {
144+ SpringBeanFactory . context = event. getApplicationContext();
145+ }
146+
147+ @Override
148+ public <T > T create (Class<T > contractClass ) {
149+ return SpringBeanFactory . context. getBean(contractClass);
150+ }
151+
152+ @Override
153+ public boolean isReady () {
154+ return SpringBeanFactory . context != null ;
155+ }
156+ }
157+ ```
158+
159+ ### Programmatic configuration
160+
161+ You can also fine tune you configuration on instance level - using methods available at
162+ ` Stringify ` interface:
163+
164+ ``` java
165+ // given
166+ BeanFactory beanFactory = createBeanFactory();
167+ Person person = createPerson();
168+
169+ // then
170+ Stringify stringifier = Stringify . of(person);
171+ stringifier
172+ .beanFactory(beanFactory)
173+ .mode(Mode . QUIET )
174+ .stringify();
175+ ```
176+
86177## Dependencies
87178
88- * Java >= 7
179+ * Java >= 8
89180 * [ EID Exceptions] ( https://github.com/wavesoftware/java-eid-exceptions ) library
90181
91182### Contributing
0 commit comments