Question - What is the use of @Autowired annotation?
Answer -
@Autowired annotation is meant for the injection of a bean by means of its type along with methods and fields. This helps the Spring framework to resolve dependencies by injecting and collaborating the beans into another bean. For example, consider the below code snippet:
import org.Springframework.beans.factory.annotation.Autowired;
import java.util.*;
public class InterviewBit {
// Autowiring/Injecting FormatterUtil as dependency to InterviewBit class
@Autowired
private FormatterUtil formatterUtil;
public Date something( String value ){
Date dateFormatted = formatterUtil.formatDate(value);
return dateFormatted
}
}
/**
* Util class to format any string value to valid date format
*/
public class FormatterUtil {
public Date formatDate(String value){
//code to format date
}
}