"Welcome To Ashok IT"
"Spring Boot and MicroServices"
Topic : Autowiring Using Annotations
Date : 19/03/2024
(Session - 07)
_____________________________________________________________________________________________________________________________
Autowiring through Annotation
=============================
* Autowiring >> Reducing the XML Configuration in Spring Application
Injecting the dependencies implictly with out any XML configuration
It can be applicable only for "Dependency in the form of objects" properties
Autowiring strategies are no,byName,byType,Constructor
We can achieve autowiring concept using xml Approach and Annotation Approach
We already completed xml approach now we are going to explore on Annotation Approach
* If we need to work with Autowiring using Annotations the spring provided @Autowired annotation
@Autowired Annotation >> Field level, Setter method, Constructor
* Most in the project development we are always prefer to use @Autowired annotation on top of Fields only.
* If we add the @Autowired Annotation on top of Field means it will apply either byName/byType Strategy.
byName/byType strategy container will use Setter Injection internally.
Case-1
=======
@Autowired
private User user; // Field
Spring.xml
===========
Observation :: Property Name and bean definition id value is matched then container will apply byName strategy
Case-2
======
@Autowired
private User userObj; // Field
Spring.xml
===========
Observation :: Property Name and bean definition id value is not matched then container will apply byType strategy
Activating the IOC Container to Read and execute @Autowired Annotation
=======================================================================
Demo.java
=========
package com.ashokit.spring.beans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
public class Demo {
@Autowired
@Qualifier(value = "currentUser1") // Qualifying the particular bean definition out of multiple bean definitions
private User user;
public void displayData() {
String userDetails = user.getUserDetails();
System.out.println(userDetails);
}
}
User.java
=========
package com.ashokit.spring.beans;
public class User {
private String username;
private String password;
public void setUsername(String username) {
this.username = username;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [username=" + username + ", password=" + password + "]";
}
public String getUserDetails() {
return String.format("Username %s and Password %s", username,password);
}
}
SpringClient.java
=================
package com.ashokit.spring.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ashokit.spring.beans.Demo;
public class SpringClient {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Demo demo = (Demo)context.getBean("demo");
demo.displayData();
}
}
Spring.xml
==========
-----------------------------------------------------------------------------------------------------------------------------