Updated March 29, 2023
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.
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";
}
}
Inject the bean into service –
` @Component
public class autoprjser {
@Autowired
private autoproj autoproj;
}
- @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;
}
}
- @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;
}
}
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.
- After generating project extract files and open this project by using spring tool suite –
- After opening the project using the spring tool suite, check the project and its files –
- 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.
- 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 ();
}
}
- 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" );
} }
- Create main application java file –
Code –
public class SpringBootAutowiredApplication {
ApplicationContext context = new ClassPathXmlApplicationContext ("Autowired.xml");
autowired te = (autowired) context.getBean ("autowired");
te.springauto ();
}
- 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">
- Run the autowired application using spring boot app –
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 –