EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Spring Tutorial spring boot autowired
 

spring boot autowired

Updated March 29, 2023

spring boot autowired

 

 

Introduction to spring boot autowired

Spring boot autowired is the feature of the spring boot framework, which was used to enable us to inject the dependency object implicitly; it is used in setter or in constructor injection internally. Autowired is not used in string values or in primitive injection; it requires less code because we have no need to write the code while injecting dependency explicitly. Furthermore, Autowired is allows spring to resolve the collaborative beans in our beans. Spring boot framework will enable the automatic injection dependency by using declaring all the dependencies in the xml configuration file.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Overviews

  • The autowired is providing fine-grained control on auto wiring, which is accomplished. Autowired annotation is used in the autowired bean and in the setter method.
  • We can use autowired annotation on the setter method to get rid of properties of elements in the configuration file of XML.
  • When spring boot will finding the setter method with autowired annotation, it will be trying to use byType auto wiring.
  • Below is the autowired annotation mode is as follows.
  • byName
  • no
  • constructor
  • byType
  • autodetect
  • The autowired annotation byName mode is used to inject the dependency object as per the bean name. In that case, our bean name and property name should be the same. This mode will internally call the setter method.
  • The autowired annotation no mode is the default mode of auto wiring. Therefore, we have no need to define this mode explicitly while using autowired annotation in our project.
  • The autowired annotation constructor mode will inject the dependency after calling the constructor in the class. This mode is calling the constructor by using more number parameters.
  • The autowired annotation byType mode will inject the dependency as per type.
  • When using byType mode in our application, the bean name and property name are different. This method is also calling the setter method internally.
  • The autowired annotation autodetect mode will be removed from spring boot version 3.
  • The bean property setter method is just a special case of a method of configuration. Autowired parameter is declared by using constructor parameter or in an individual method.

Using @Autowired

While enabling annotation injection, we can use the auto wiring on the setter, constructor, and properties.

We can use auto wiring in following methods.

  • Autowired on properties
  • Autowired on setter
  • Autowired on constructor

We can annotate the auto wiring on each method are as follows.

  • @Autowired on properties –

 We can annotate the properties by using the @Autowired annotation. This method will eliminated the need of getter and setter method.

To use this method first, we need to define then we need to inject the bean into service.

Define bean –

@Component ("autoproj")
public class autoproj {
public String format() {
return "aprj";
}
}

spring boot autowired output 1

Inject the bean into service –

`           @Component
public class autoprjser {
@Autowired
private autoproj autoproj;
}

spring boot autowired output 2

  • @Autowired on setter –

In the below example, we have adding autowired annotation in the setter method. In the below example, we have called the setter method autosetter.

public class autoset {
private autosetter autosetter;
@Autowired
public void setautosetter (autosetter autosetter) {
this.autosetter = autosetter;
}
}

spring boot autowired output 3

  • @Autowired on constructor –

In the below example, we have adding autowired annotation in the constructor method. Moreover, in the below example, we have injecting the spring argument with autocon constructor.

public class autocon {
private autosetter autosetter;
@Autowired
public autocon(autosetter autosetter) {
this.autosetter = autosetter;
}
}

spring boot autowired output 4

Example of autowired

The below example shows step by step implementation of autowired are as follows.

  • Create a project template using a spring initializer and give the following name to the project metadata.

In the below step, we provide the project group name as com. Examples include artifact name as spring-boot-autowired, project name as a spring-boot-autowired, package as a jar file, and selecting java version as 11.

Group – com.example
Artifact name – spring-boot-autowired
Name – spring-boot-autowired
Description – Project of spring-boot- autowired
Package name – com.example.spring-boot- autowired
Packaging – Jar
Java – 11
Dependencies – spring web.

spring boot autowired output 5

  • After generating project extract files and open this project by using spring tool suite –

spring boot autowired output 6

  • After opening the project using the spring tool suite, check the project and its files –

spring boot autowired output 7

  • Add the dependency –

Code –

<dependency>   -- Start of dependency tag.
<groupId>org.springframework.boot</groupId>   -- Start and end of groupId tag.
<artifactId>spring-boot-starter-web</artifactId>  -- Start and end of artifactId tag.
</dependency>    -- End of dependency tag.

spring boot autowired output 8

  • Create autowired java file –

Code –

public class springbootautowired {
private springauto springauto;
@Autowired
public void setSpellChecker ( springauto springauto ){
this.springauto = springauto;
}
public springauto getspringauto ( ) {
return springauto;
}
public void springauto() {
springauto.checkspringauto ();
}
}

spring boot autowired output 9

  • Crate another dependent class –

Code –

public class SpringAutoWired {
public SpringAutoWired(){
System.out.println ("Spring boot AutoWired" );
}
public void checkspringauto (){
System.out.println("Spring boot AutoWired" );
}        }

output 10

  • Create main application java file –

Code –

public class SpringBootAutowiredApplication {
ApplicationContext context = new ClassPathXmlApplicationContext ("Autowired.xml");
autowired te = (autowired) context.getBean ("autowired");
te.springauto ();
}

output 11

  • Create xml file –

Code –

<!-- Definition for autowired bean -->
<bean id = "autowired" class = "com.springbootautowired.autowired">
</bean>  -- End of bin tag.
<!-- Definition for springauto bean -->
<bean id = "springauto" class = "com.springbootautowired.springauto">

output 12

  • Run the autowired application using spring boot app –

output 13

Conclusion

Autowired is providing fine-grained control on auto wiring, which is accomplished. Spring boot autowired annotation is used in the autowired bean and setter method. Autowired is the feature of the spring boot framework, which was used to enable us to inject the dependency object implicitly.

Recommended Articles

This is a guide to spring boot autowired. Here we discuss the Overview and Example of autowired along with the codes. You may also have a look at the following articles to learn more –

  1. Spring Boot Logging
  2. Spring Batch Architecture
  3. Spring Boot HTTPS
  4. Spring Boot application.properties
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW