Dependency Injection via Annotations
HOW SPRING DOES IT – SORT OF 
About Presenter
•I am Jerry Kurian. Founder and Maker in Chief at Learnym Technologies
(www.Learnym.com)
•I have worked with Java for almost 20 years.
• Developed distributed systems at Huawei Technologies
• Lead developer on EJB container for our in-house app server
•Post Huawei, worked on multiple products using Java and non-Java techs
•Have kept in touch with the evolving eco-system and now code a lot with
Java, Scala, Angular 4 and anything that seems like the right tool for the job
•At Learnym, helping businesses define their products clearly and then help
develop a scalable solution. We also do workshops on interesting topics
10/23/2017 LEARNYM CONFIDENTIAL 2
Why do we need dependency
Injection
•Modular architecture is scalable
•Modularization has been the aim since long and various technologies
and architectures have come and gone
• EJB as a Component
• JNDI for Lookup
•Most of the componentization solutions suffer from being overly
complex and too hard to maintain !!
•Injection methods make code much more simpler, leaving the
frameworks to do the heavy lifting
10/23/2017 LEARNYM CONFIDENTIAL 3
What’s the challenge?
•Modularization requires that two components be as independent of each
other as possible
•Module A should access Module B functionalities through the interface B,
the contract
10/23/2017 LEARNYM CONFIDENTIAL 4
Module A Module B
Interface B
Approaches
•There are multiple approaches like lookup via factory, JNDI
•Adds dependency on the factory or makes code and packaging complex !!
10/23/2017 LEARNYM CONFIDENTIAL 5
Module A Module B
Interface B
Factory
Register
Lookup
A better approach
•The problem with the lookup and such approaches is that there is a
dependency with the framework, which makes the code complex
•A better approach is to make the code unspoilt with external concerns
•A meta-data based approach provides a solution
•Java Annotations is the way to define meta-data in Java code
•Annotations are just place holder for the compiler or the run-time
system to do something meaningful with it
•At runtime, Annotations in collaboration with Reflection can do a lot of
magic
10/23/2017 LEARNYM CONFIDENTIAL 6
How does it work?
public class ModuleA{
@Autowired
private ModuleB moduleB;
}
•The ModuleA “indicates” its dependence on the ModuleB contract
•At runtime, it expects an appropriate implementation of the ModuleB
interface to be available to it.
•Spring does it by injecting an instance of ModuleBImpl into the private
member variable
•How does this happen?
10/23/2017 LEARNYM CONFIDENTIAL 7
The process
•The application classes
• Define the module classes and contract
• In the dependent module, add a member variable with an Annotation
• The type of the member variable should be that of the dependency
•The Framework
• The framework should find out the classes that are dependent on other
modules by reflecting over them
• Create instances of the module classes via reflection
• On the dependent instance, set the member value to the instance of the
dependency
10/23/2017 LEARNYM CONFIDENTIAL 8
Breaking It Down
The framework does the following
1. Create an instance of the module that is desired
2. Identify the dependencies of this module by finding out fields that
are annotated as dependents
3. For each dependent field, identify the type and look through the
packages to find a class that implements the type
4. Find out if the implementation makes itself injectable via an
annotation and create an instance
5. Inject transitive dependencies
6. Set the private member variable of the target module “accessible”
7. Set the dependency instance to the private variable
10/23/2017 LEARNYM CONFIDENTIAL 9
Questions?
10/23/2017 LEARNYM CONFIDENTIAL 10
Thank You
•Do get in touch for any workshops on advanced Java and Scala.
•Look forward to hearing about any challenges you face that could be
covered in a workshop
•Feel free to connect with me on linkedIn @
https://www.linkedin.com/in/jerryk/
10/23/2017 LEARNYM CONFIDENTIAL 11

Dependency injection via annotations v1.0

  • 1.
    Dependency Injection viaAnnotations HOW SPRING DOES IT – SORT OF 
  • 2.
    About Presenter •I amJerry Kurian. Founder and Maker in Chief at Learnym Technologies (www.Learnym.com) •I have worked with Java for almost 20 years. • Developed distributed systems at Huawei Technologies • Lead developer on EJB container for our in-house app server •Post Huawei, worked on multiple products using Java and non-Java techs •Have kept in touch with the evolving eco-system and now code a lot with Java, Scala, Angular 4 and anything that seems like the right tool for the job •At Learnym, helping businesses define their products clearly and then help develop a scalable solution. We also do workshops on interesting topics 10/23/2017 LEARNYM CONFIDENTIAL 2
  • 3.
    Why do weneed dependency Injection •Modular architecture is scalable •Modularization has been the aim since long and various technologies and architectures have come and gone • EJB as a Component • JNDI for Lookup •Most of the componentization solutions suffer from being overly complex and too hard to maintain !! •Injection methods make code much more simpler, leaving the frameworks to do the heavy lifting 10/23/2017 LEARNYM CONFIDENTIAL 3
  • 4.
    What’s the challenge? •Modularizationrequires that two components be as independent of each other as possible •Module A should access Module B functionalities through the interface B, the contract 10/23/2017 LEARNYM CONFIDENTIAL 4 Module A Module B Interface B
  • 5.
    Approaches •There are multipleapproaches like lookup via factory, JNDI •Adds dependency on the factory or makes code and packaging complex !! 10/23/2017 LEARNYM CONFIDENTIAL 5 Module A Module B Interface B Factory Register Lookup
  • 6.
    A better approach •Theproblem with the lookup and such approaches is that there is a dependency with the framework, which makes the code complex •A better approach is to make the code unspoilt with external concerns •A meta-data based approach provides a solution •Java Annotations is the way to define meta-data in Java code •Annotations are just place holder for the compiler or the run-time system to do something meaningful with it •At runtime, Annotations in collaboration with Reflection can do a lot of magic 10/23/2017 LEARNYM CONFIDENTIAL 6
  • 7.
    How does itwork? public class ModuleA{ @Autowired private ModuleB moduleB; } •The ModuleA “indicates” its dependence on the ModuleB contract •At runtime, it expects an appropriate implementation of the ModuleB interface to be available to it. •Spring does it by injecting an instance of ModuleBImpl into the private member variable •How does this happen? 10/23/2017 LEARNYM CONFIDENTIAL 7
  • 8.
    The process •The applicationclasses • Define the module classes and contract • In the dependent module, add a member variable with an Annotation • The type of the member variable should be that of the dependency •The Framework • The framework should find out the classes that are dependent on other modules by reflecting over them • Create instances of the module classes via reflection • On the dependent instance, set the member value to the instance of the dependency 10/23/2017 LEARNYM CONFIDENTIAL 8
  • 9.
    Breaking It Down Theframework does the following 1. Create an instance of the module that is desired 2. Identify the dependencies of this module by finding out fields that are annotated as dependents 3. For each dependent field, identify the type and look through the packages to find a class that implements the type 4. Find out if the implementation makes itself injectable via an annotation and create an instance 5. Inject transitive dependencies 6. Set the private member variable of the target module “accessible” 7. Set the dependency instance to the private variable 10/23/2017 LEARNYM CONFIDENTIAL 9
  • 10.
  • 11.
    Thank You •Do getin touch for any workshops on advanced Java and Scala. •Look forward to hearing about any challenges you face that could be covered in a workshop •Feel free to connect with me on linkedIn @ https://www.linkedin.com/in/jerryk/ 10/23/2017 LEARNYM CONFIDENTIAL 11