DEV Community

Discussion on: Understanding Dependency Injection by writing a DI Container - from scratch! (Part 3)

Collapse
 
tee12345 profile image
Babatunde Raimi Lawal

@martin Hausler, thanks for this post. Please how can I make this program accept configuration files (XML or text files)? Thanks for your time.

Collapse
 
martinhaeusler profile image
Martin Häusler

Hi! Sorry for the late response, somehow your "@" mention didn't work and I didn't get notified about your answer.

The XML/Text files you refer to would replace the classpath scan. Instead of scanning all classes, you'll read a predefined list of service class names from a file, typically XML. Let's simplify it a bit and use plain text, for example:

(left is interface, right is implementation)

org.example.api.MyServiceA, org.example.impl.MyServiceAImpl
org.example.api.MyServiceB, org.example.impl.MyServiceBImpl
Enter fullscreen mode Exit fullscreen mode

This would tell you that the interface "MyServiceA" is implemented by a class "MyServiceAImpl". To find the classes, you'd simply have to use Class.forName(className). Use those interface/class pairs instead of the classpath scan.

Collapse
 
tee12345 profile image
Babatunde Raimi Lawal

Thanks a lot, I'm very grateful.