Reflection & Annotations — The Powerful Combination

Inversion of control

To get started with our discussion let’s talk about the inversion of control principle. It is commonly used by frameworks. The framework is a set of defined patterns to effectively complete a task. The framework has generic components with their roles. For example, let’s take frameworks for web applications, what kind of generic components or mechanisms they should have. Might be a controller where we can define endpoints and corresponding methods, a service where we can connect to the database and do some business logic. Now if we think almost every web project should have these mechanisms (controllers, services). But of course, the type of controllers and services and what they do will depend on the project.

Let’s create something simple

Let’s create our own small framework and utilize Inversion Of Control with annotations and reflection. This is not a production version or complete framework. The main idea is to understand how does it work.

test.java.restClient.ResstClient.java
test.java.restClient.ClientRequest.java
test.java.restClient.Response.java
package restClient;

public enum RequestMethod {
GET, POST, PUT, DELETE
}
test.java.restClient.RequestRunner.java
test.java.test.DummyRestApiTesting.java
test.java.test.DummyRestApiTesting.java

Summary

Inversion of control when the framework has ready flow and pattern but it lets user specify the business logic. The annotation and reflection can be used to build frameworks with an inversion of the control principle.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store