Inside @Autowired SpringBoot

Chandan Kumar
5 min readJul 27, 2022

Since I’ve started with SpringBoot, I’ve heard lot about Spring magic. Multiple time when I asked people about how something is working internally in Spring the answer was it’s Spring magic and it works like this.

This was often not the best answer I expected kind of okay for me till sometime, but later when someone else asked me the same question I didn’t wanted to give the same answer but something reasonable and real.

So I started looking into the resources available on the internet, here I’ll be sharing my learnings and understanding of how @Autowired annotation works internally.

Every SpringBoot project contains an application app class where the invocation for “SpringApplication.run(DemoApp::class.java, *args)” happens, In order to understand the bigger picture we need to start form this file and the annotation on top of this class — “@SpringBootApplication

@SpringBootApplication

This annotation is the starting point of any spring application and is a combination of three further annotations —

  1. @EnableAutoConfiguration

This enables spring boot’s auto configuration mechanism,

Spring Boot auto-configuration attempts to automatically configure your Spring application based on the jar dependencies that you have added. For example, if HSQLDB is on your classpath, and you have not manually configured any database connection beans, then Spring Boot auto-configures an in-memory database.

You need to opt-in to auto-configuration by adding the @EnableAutoConfiguration or @SpringBootApplication annotations to one of your @Configuration classes. — Spring Docs

2. @ComponentScan

Enable @Component scan on the package where the application is located

3. @Configuration

Allow to register extra beans in the context or import additional configuration classes.

Now as we know a bit about the main annotation, its look into the run function for spring boot.

The function SpringApplication.run(DemoApp::class.java, *args) returns a ConfigurableApplicationContext using which you can access any class object directly if it’s already registered with spring, we’ll see how to register a class to spring, but for now lets take an example,

Consider you’ve created a class called Student and you want to create an instance of it, you don’t want to use new Student() every-time you want to use it instead you want to just worry about the application logic and let spring create and inject a new Object for the class for you,

class Student {
....
....
}

How this happens?

If for example we store return value of the function calls for “SpringApplication.run(DemoApp::class.java, *args)” inside some “context” you should be able to do,

Student student = context.getBean(Student.class);

Now when we’ll try to compile the application it will throw an error saying “Spring is not able to find a reference for class student” and the reason is clear,

How spring knows which classes to initialise? there could be many, right? if there are a hundreds of classes, it doesn’t make sense for spring to create instance for all, right?

Instead what if we tell spring what all classes it needs to initialise, there could be configurations or dependencies which we may want spring to initialise as soon as the application begins.

This can be done by annotation like “@Component “.

We have a jvm on which we run the application on top of the jvm we have something called “Spring Container” which stores objects, the question is what objects? the object which we store in the Spring container is called “Spring Bean” now this might sound similar.

Once we have the annotation on the class, spring knows that it needs to create an object for the same and also register in spring container.

@Component
class Student {
....
....
}

By default spring follows singleton pattern to create an object which means no matter how many time you’ve used the Object spring will just create a single object registry in the spring container with singleton pattern, so that everybody can use it.

There are ways to specify the scope for example we can have a prototype and then only when a request is made for a component it will get created.

Now you do “Student student = context.getBean(Student.class);” spring will check in the spring container if it already has an entry for Student class if so it can directly use that, if not throw and error.

All good till now as we’re using a single object which internally has no dependency on any other object, but what if we have two classes where one class is dependency for other.

@Component
class School {
private student Student;
...
...
}

Now that we have two classes one being Student class and other being School class, where School has a dependency Student.

What if we want to have an instance of School class similar to

Student student = context.getBean(Student.class);
School school = context.getBean(School.class);

There will be entries for both in spring container as mentioned in the below image.

It should work fine, right?…. Not really.

Though there is a registry in spring container spring doesn’t know if there’s an interdependency between any classes by default.

One way is to initialise it with new keyword but we don’t want to do this as dependency injection should be managed by spring, and we should only worry about the actual logic.

So, now we have another annotation which we can use to annotate dependency and auto injection of objects.

@Autowired

The @Autowired annotation marks a Constructor, Setter method, Properties and Config() method as to be autowired that is ‘injecting beans’(Objects) at runtime by Spring Dependency Injection — GFG

Once we add the annotation to the dependency class, spring now knows that there’s a dependency object that it needs to create first in order to initialise the object properly.

@Component
class School {
@Autowired
private student Student;
...
...
}

And now the below definitions should work fine

Student student = context.getBean(Student.class);
School school = context.getBean(School.class);

I hope this was helpful, please add your understanding and magic of any other SpringBoot Annotation in the comments.

Thank you :)

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Chandan Kumar
Chandan Kumar

Written by Chandan Kumar

A Devil’s Advocate and a Software Developer

Responses (3)

Write a response