What is Dependency Injection in Spring? Creating a simple Spring project with Dependency Injection in the constructor with the attributes @Component, @Autowired | Editor IntelliJ Idea and language Java
last updated: 7 April 2025
DI (
Dependency Injection ) is used when classes depend on each other.
For example: A class in a constructor needs another class.
Java
public class Book
{
...
public Book(Author author)
{
_author = author;
}
}
The
Spring library automatically creates nested objects by itself.
Spring will create a
Author object, but you need to specify
the dependency injection type .
Dependency injection type - this is the place where one class will be injected into another:
1) In the constructor
I'll use in my example below
My example:
There is a class
Book
There is a class
Author
When creating
Book , you need
Author
Why should I use
Spring, Dependency Injection if I can create my objects in 2 lines:
Java
Author author = new Author();
Book book = new Book(author);
Answer: Because a good style is to specify through the dependency attribute, and
Spring will create nested objects by itself.
And the biggest advantage is that when you create nested objects, they become loosely coupled (
Spring creates automatically) and this makes it less code and easier to write tests.
What is Spring Bean and how do I specify dependencies in different bean ?
Spring Bean is a regular object that is not created by me, but the object is created by Spring Container .
Java
ApplicationContext context = new ClassPathXmlApplicationContext("MyConfig.xml" ); // Create Spring Container from the configuration file
Book book = (Book) context.getBean("myBookBean" );
Spring by name myBookBean creates Book .
And since Author is needed internally, using dependency injection creates Author
Dependent objects Spring Container are created by itself
Spring Container knows how to create Book because I use the @Component attribute:
At the beginning of the class, I will use @Component (" come up with any name for bean ").
To inject the dependency, I will use the @Autowired annotation in the constructor
Java
@Component ("myBookBean" )
public class Book // Class Book is bean with the name myBookBean
{
...
@Autowired
public Book(Author author)
{
_author = author;
}
}
At the beginning of the class, I will use @Component (" come up with any name for bean ").
Java
@Component ("myAuthorBean" )
public class Author // Class Author is bean with the name myAuthorBean
{
...
public Author()
{
_authorName = "Mark Twain" ;
}
}
In the configuration file for context:component I wrote base-package (where our classes are located):
Xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans" ...>
<context:component-scan base-package="com.example.springproj1" />
</beans>
Which configuration is best for IoC Spring Container (from this configuration, Spring will know how to create bean ):
•
configuration in the XML file ... (used in older programs)
• configuration via
@Component ,
@Autowired attributes (used in modern programs and I use in my example below)
• configuration via
Java code using
@Configuration ,
@ComponentScan ,
@Bean (sometimes used)
Configuration for
IoC Spring Container is an explanation for
IoC Spring Container how to create
bean objects.
Someone who controls and can manage all of your classes is called
ApplicationContext (IoC Spring Container)
Creating a simple Spring project with Dependency Injection in the constructor | Editor IntelliJ Idea and language Java
Download an example:
Note! You must have Java JDK installed. If you don't have it, then need to download and install Java JDK ...
Note! You must have IntelliJ IDEA Ultimate installed. If you don't have it, then need to download and install IntelliJ IDEA ...
Step 1. Create a new project with the type Spring Boot
Select Spring Boots :
Since we have a simple project, do not check the boxes:
The project was created:
Step 2. Create a new Book.java file
Right-click on my package folder and create a new java file
Let's write the name Book
And choose the type Class
Press Enter
The file was created.
Inside the Book.java file, add the code:
package com.example.springproj1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("myBookBean" )
public class Book {
protected String _name;
protected Author _author;
@Autowired
public Book(Author author)
{
_name = "Tom Sawyer" ;
_author = author;
}
public void ShowBook()
{
System.out.println(_name);
_author.ShowAuthor();
}
}
Step 3. Create a new Author.java file
package com.example.springproj1;
import org.springframework.stereotype.Component;
@Component("myAuthorBean" )
public class Author {
protected String _authorName;
public Author()
{
_authorName = "Mark Twain" ;
}
public void ShowAuthor()
{
System.out.println(_authorName);
}
}
Step 4. Create a new configuration MyConfig.xml file
Right-click on the folder resources and create a new Spring Config file
Let's write the name MyConfig
Press Enter
The file was created.
Inside the MyConfig.xml file, add the code:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd" >
<context:component-scan base-package="com.example.springproj1" />
</beans>
Step 5. In the application java file, add the code
package com.example.springproj1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//@SpringBootApplication
public class SpringProj1Application {
public static void main(String[] args) {
SpringApplication.run(SpringProj1Application.class, args);
ApplicationContext context = new ClassPathXmlApplicationContext("MyConfig.xml" );
Book book = (Book) context.getBean("myBookBean" );
book.ShowBook();
}
}
Note!
// Option 1
Book book = (Book) context.getBean("myBookBean" );
// Or you can do it like this
// Option 2
Book book = context.getBean("myBookBean" , class.Book);
Note!
In order for Spring to create bean , I had to comment //@SpringBootApplication in the SpringProj1Application.java file
Step 6. Launching the project
To start the project, you need to open the application java file and click on the green triangle:
After launching the program, we will see the result on the screen: