Spring Autowiring
Autowiring is a technique used in Spring to enable automatic dependency injection. By using it Spring container can autowire relationships between collaborating beans. It is known as Spring Autowiring.
Spring provides @Autowired annotation that enables you to inject the object dependency implicitly. It internally uses setter or constructor injection.
Note: We can not use @Autowired annotation to inject primitive and string values. It works with reference only.
Enable AutoWiring in Spring
The Spring framework enables automatic dependency injection. In other words, by declaring all the bean dependencies in a Spring configuration file, the Spring container can autowire relationships between collaborating beans. This is called Spring bean autowiring.
In a Java-based configuration, we can enable it by using the @componentScan annotation.
@Configuration
@ComponentScan("com.studytonight")
public class AppConfig{
}
and in case of XML configuration, we can use <context:annotation-config> tag inside the applicationcontext file.
Autowiring can be used with fields, methods, and constructors as well. Let's see some examples
After enabling annotation injection, we can use autowiring on properties, setters, and constructors.
Example: Field Autowiring
Spring allows using @Autowired annotation with fields to inject dependencies as we did in the below example. For a complete example, refer to our detailed article @autowiring with fields.
@Service
@Component
public class UserServices {
@Autowired
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
Example: Method Autowiring
Spring allows using @Autowired annotation with methods to inject dependencies as we did in the below example. For a complete example, refer to our detailed article @Autowiring with methods.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class FictionWriter implements Writer {
private Award award;
@Autowired
public void awardInstance(Award award) {
this.award = award;
}
}
Example: Constructor Autowiring
Spring allows using @Autowired annotation with constructors to inject dependencies as we did in the below example. For a complete example, refer to our detailed article @Autowiring with constructor.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class TechnicalWriter implements Writer{
private Award award;
@Autowired
public TechnicalWriter(@Qualifier("pulitzerAward") Award award) {
this.award = award;
}
}
We can mix it as well with fields, constructors, and methods.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class TechnicalWriter implements Writer{
@Autowired
private Award award; // Field Autowired
@Autowired // Constructor Autowired
public TechnicalWriter(Writer writer) {
TechnicalWriter tWriter = writer
}
}
Afterward, we'll talk about resolving bean conflicts using @Qualifier annotation, as well as potential exception scenarios.