55import com .draagon .meta .loader .simple .SimpleLoader ;
66import com .draagon .meta .registry .MetaDataLoaderRegistry ;
77import com .draagon .meta .object .MetaObject ;
8+ import com .draagon .meta .util .MetaDataUtil ;
89
910import org .springframework .beans .factory .annotation .Autowired ;
1011import org .springframework .boot .CommandLineRunner ;
2728@ SpringBootApplication
2829public class SpringMetaObjectsExample implements CommandLineRunner {
2930
30- // Option 1 : Convenient service wrapper (recommended)
31+ // COMPLEX PATTERN : Convenient service wrapper for multi-loader scenarios (recommended)
3132 @ Autowired
3233 private MetaDataService metaDataService ;
33-
34- // Option 2: Backward compatible loader injection
34+
35+ // SIMPLE PATTERN: Direct loader injection for single-loader scenarios
3536 @ Autowired
3637 private MetaDataLoader primaryMetaDataLoader ;
37-
38- // Option 3 : Full registry access for advanced operations
38+
39+ // COMPLEX PATTERN : Full registry access for advanced multi-loader operations
3940 @ Autowired
4041 private MetaDataLoaderRegistry metaDataLoaderRegistry ;
4142
@@ -54,61 +55,48 @@ public static void main(String[] args) {
5455 * Simple test of Spring integration without Spring Boot complexity
5556 */
5657 private static void testSpringIntegrationManually () throws Exception {
57- System .out .println ("\n 1. Manual Spring integration test..." );
58-
59- // Test direct Spring class usage
60- com .draagon .meta .spring .MetaDataService service = null ;
61- com .draagon .meta .spring .MetaDataAutoConfiguration config = null ;
62-
58+ System .out .println ("\n 1. Spring class loading verification..." );
59+
60+ // Test Spring class availability
6361 try {
64- // Test that Spring classes can be loaded
65- config = new com .draagon .meta .spring .MetaDataAutoConfiguration ();
66- System .out .println (" MetaDataAutoConfiguration class loaded: SUCCESS" );
67-
68- // Test MetaDataService class loading
69- Class <?> serviceClass = Class .forName ("com.draagon.meta.spring.MetaDataService" );
70- System .out .println (" MetaDataService class loaded: SUCCESS" );
71-
72- // Test MetaDataLoaderConfiguration class loading
73- Class <?> configClass = Class .forName ("com.draagon.meta.spring.MetaDataLoaderConfiguration" );
74- System .out .println (" MetaDataLoaderConfiguration class loaded: SUCCESS" );
75-
62+ Class .forName ("com.draagon.meta.spring.MetaDataAutoConfiguration" );
63+ Class .forName ("com.draagon.meta.spring.MetaDataService" );
64+ Class .forName ("com.draagon.meta.spring.MetaDataLoaderConfiguration" );
65+ System .out .println (" Spring integration classes: SUCCESS" );
7666 } catch (Exception e ) {
7767 System .out .println (" Spring class loading failed: " + e .getMessage ());
7868 }
79-
80- // Test basic metadata functionality (same as other examples)
69+
70+ // Test basic metadata functionality using simple pattern
8171 System .out .println ("\n 2. Basic MetaObjects functionality..." );
82-
83- com . draagon . meta . loader . simple . SimpleLoader loader = new com . draagon . meta . loader . simple . SimpleLoader ( "spring-test" );
84-
85- // Load from classpath (same approach as other examples)
72+
73+ // Simple pattern: Create one loader for single-loader scenario
74+ SimpleLoader loader = new SimpleLoader ( "spring-test" );
75+
8676 java .net .URL resourceUrl = SpringMetaObjectsExample .class .getResource ("/metadata/examples-metadata.json" );
8777 if (resourceUrl == null ) {
88- throw new RuntimeException ("Could not find metadata resource: /metadata/examples-metadata.json " );
78+ throw new RuntimeException ("Could not find metadata resource" );
8979 }
90-
91- // Create temporary file and copy resource content
80+
9281 java .nio .file .Path tempFile = java .nio .file .Files .createTempFile ("examples-metadata" , ".json" );
9382 try (java .io .InputStream is = resourceUrl .openStream ()) {
9483 java .nio .file .Files .copy (is , tempFile , java .nio .file .StandardCopyOption .REPLACE_EXISTING );
9584 }
96-
97- java .net .URI metadataUri = tempFile .toUri ();
98- loader .setSourceURIs (java .util .Arrays .asList (metadataUri ));
85+
86+ loader .setSourceURIs (java .util .Arrays .asList (tempFile .toUri ()));
9987 loader .init ();
100-
88+
10189 System .out .println (" Loaded " + loader .getChildren ().size () + " metadata items" );
102-
103- // Test MetaObject access
90+
91+ // Simple pattern: Direct loader access instead of registry
10492 try {
105- com .draagon .meta .object . MetaObject userMeta = loader . getMetaObjectByName ( "com_example_model::User" );
93+ MetaObject userMeta = com .draagon .meta .util . MetaDataUtil . findMetaObjectByName ( loader , "com_example_model::User" );
10694 System .out .println (" Found User MetaObject: " + userMeta .getName ());
10795 System .out .println (" User has " + userMeta .getMetaFields ().size () + " fields" );
10896 } catch (Exception e ) {
10997 System .out .println (" MetaObject lookup failed: " + e .getMessage ());
11098 }
111-
99+
112100 System .out .println ("\n === Manual Spring integration test completed ===" );
113101 }
114102
@@ -155,38 +143,54 @@ private void demonstrateSpringIntegration() {
155143 boolean hasProduct = metaDataService .metaObjectExists ("com_example_model::Product" );
156144 System .out .println (" User exists: " + hasUser + ", Product exists: " + hasProduct );
157145
158- // 3. Backward compatible loader access
159- System .out .println ("\n 3. Backward compatible loader access..." );
146+ // 3. SIMPLE PATTERN: Direct loader access for single-loader scenarios
147+ System .out .println ("\n 3. Simple pattern - direct loader access..." );
160148 System .out .println (" Primary loader name: " + primaryMetaDataLoader .getName ());
161- System .out .println (" Primary loader objects: " +
149+ System .out .println (" Primary loader objects: " +
162150 primaryMetaDataLoader .getChildren (MetaObject .class ).size ());
151+
152+ // Demonstrate simple pattern utility methods
153+ try {
154+ MetaObject directUser = MetaDataUtil .findMetaObjectByName (primaryMetaDataLoader , "com_example_model::User" );
155+ System .out .println (" Direct loader lookup: " + directUser .getName ());
156+ } catch (Exception e ) {
157+ System .out .println (" Direct lookup failed: " + e .getMessage ());
158+ }
163159
164- // 4. Advanced registry operations
165- System .out .println ("\n 4. Advanced registry operations..." );
166- System .out .println (" Total registered loaders: " +
160+ // 4. COMPLEX PATTERN: Advanced registry operations for multi-loader scenarios
161+ System .out .println ("\n 4. Complex pattern - registry operations..." );
162+ System .out .println (" Total registered loaders: " +
167163 metaDataLoaderRegistry .getDataLoaders ().size ());
168-
164+
169165 for (MetaDataLoader loader : metaDataLoaderRegistry .getDataLoaders ()) {
170- System .out .println (" - Loader: " + loader .getName () +
166+ System .out .println (" - Loader: " + loader .getName () +
171167 " (" + loader .getChildren ().size () + " children)" );
172168 }
169+
170+ // Demonstrate complex pattern utility methods
171+ try {
172+ MetaObject registryUser = MetaDataUtil .findMetaObjectByName ("com_example_model::User" , this );
173+ System .out .println (" Registry utility lookup: " + registryUser .getName ());
174+ } catch (Exception e ) {
175+ System .out .println (" Registry utility lookup failed: " + e .getMessage ());
176+ }
173177
174- // 5. Demonstrate service convenience methods
175- System .out .println ("\n 5. Service convenience methods..." );
176-
178+ // 5. COMPLEX PATTERN: Service convenience methods (best for most Spring applications)
179+ System .out .println ("\n 5. Service convenience methods (recommended for most Spring apps) ..." );
180+
177181 try {
178- // Direct lookup
182+ // Service wrapper with Optional support
179183 MetaObject user = metaDataService .findMetaObjectByName ("com_example_model::User" );
180- System .out .println (" Direct lookup successful: " + user .getName ());
181-
184+ System .out .println (" Service lookup successful: " + user .getName ());
185+
182186 // Field details
183187 user .getMetaFields ().forEach (field -> {
184- System .out .println (" Field: " + field .getName () +
188+ System .out .println (" Field: " + field .getName () +
185189 " (" + field .getSubType () + ")" );
186190 });
187-
191+
188192 } catch (Exception e ) {
189- System .out .println (" MetaObject lookup failed: " + e .getMessage ());
193+ System .out .println (" Service lookup failed: " + e .getMessage ());
190194 }
191195
192196 System .out .println ("\n === Spring Example completed ===" );
0 commit comments