It seems the day has finally come when I will need to do a deep-dive of Java spring framework. Most enterprise microservices today are built using Spring Boot which goes one step further in structuring spring framework.
What is spring framework?
Read the exact definition on the web, but from what I understood it is a framework that allows dynamic instantiation of objects and usage of those objects without directly referencing them in your calling class. This is what they call "dependency injection". Class members that are derived forms of interfaces or inherited class are "dependencies" in this terminology, and "injecting" them means to instantiate objects of those classes and make them ready for use in encapsulated class functions. Wow, hopefully that makes sense.
How to do it?
There are various ways to achieve this dynamic instantiation or dependency injection. Updating an xml file and providing it as a configuration option to the ApplicationContext is the crude way. The method we are interested in is the annotations based configuration.
Here are some annotations and a brief description of how they work.
@Component
This annotation is declared on the class definition and it tells the spring framework that objects of this class can be instantiated dynamically throughout the project.
@Autowired
This annotation identifies members of a class that will use dependency injection from another class that has been marked as a @Component.
@Configuration
This defines the class which is the source of bean definitions. A bean is typically a method that produces instances of objects that are marked eligible for dependency injection.
@ComponentScan
Compliments the @Configuration tag and scans the project for classes that are configured to be beans.
There are others, but these form the core for my understanding.
Top comments (0)