Spring Boot Tutorial
Spring Boot - Software Setup and Configuration (STS/Eclipse/IntelliJ)
Prerequisite (Spring Core Concepts)
Spring Boot Core
Spring Boot with REST API
Spring Boot with Database and Data JPA
Spring Boot with Kafka
Spring Boot with AOP
In Spring, autowiring allows developers to automatically inject dependencies without specifying them explicitly. This is done by the Spring container based on certain conventions or metadata.
When defining beans in the XML configuration or using Java-based configuration, the autowire functionality helps reduce the amount of configuration needed by automatically resolving bean references.
There are several modes of autowiring:
no
(default):This is the default setting, which means no autowiring. You must specify bean references manually using the <property>
or <constructor-arg>
elements in XML configuration.
<bean id="someBean" class="com.example.SomeBean" autowire="no"> <!-- Explicitly defined dependencies here --> </bean>
byType
:Autowiring by datatype. Spring looks for a bean with a matching data type in the container. If there's exactly one bean of the matching type, it will inject it. If none or multiple candidates are found, an exception will be thrown.
<bean id="someBean" class="com.example.SomeBean" autowire="byType"/>
byName
:Autowiring by bean name. Spring will look for a bean with a name matching the property or variable name that needs to be autowired.
<bean id="someBean" class="com.example.SomeBean" autowire="byName"/>
constructor
:Similar to byType
, but it applies to constructor arguments. If there's a constructor whose arguments can all be satisfied by matching the types of beans in the container, it will be used for injection.
<bean id="someBean" class="com.example.SomeBean" autowire="constructor"/>
autodetect
:Spring first tries to autowire by constructor, and if that fails, it tries by type.
Spring also provides annotation-based autowiring:
@Autowired
: It can be applied to variables, setters, or constructors. Spring will try to resolve the dependency by type.
@Autowired private SomeBean someBean;
@Qualifier
: Used along with @Autowired
to specify the name of the bean when multiple beans of the same type exist.
@Autowired @Qualifier("specificBeanName") private SomeBean someBean;
@Resource
: Similar to @Autowired
, but it does injection by name. If a name is not specified, it defaults to the field name.
@Resource(name="specificBeanName") private SomeBean someBean;
Reduced Configuration: Requires less explicit wiring in the configuration file or code.
Less Error-Prone: Reduces the chances of mistakes when specifying bean dependencies.
Clarity: The configuration might be harder to understand since dependencies aren't explicitly defined.
Potential Ambiguity: If there are multiple beans of the same type, you might run into exceptions or unexpected behavior unless you handle such situations carefully.
In general, developers should use autowiring judiciously and ensure that the advantages outweigh the potential challenges in their specific context.
Spring Autowiring example:
<bean id="car" class="com.example.Car" autowire="byType"> </bean>
@Configuration public class AppConfig { @Bean @Autowired public Car car() { return new Car(); } }
Autowiring in Spring with annotations:
@Autowired
annotation for autowiring:@Autowired private Engine engine;
Constructor Autowiring vs. Setter Autowiring in Spring:
@Autowired public Car(Engine engine) { this.engine = engine; }
@Autowired public void setEngine(Engine engine) { this.engine = engine; }
Autowired annotation in Spring:
@Autowired
for automatic dependency injection:@Autowired private Engine engine;
Spring @Qualifier with Autowiring:
@Qualifier
to specify which bean to inject:@Autowired @Qualifier("v8Engine") private Engine engine;
Dependency Injection in Spring with Autowiring:
@Service public class CarService { private final CarRepository carRepository; @Autowired public CarService(CarRepository carRepository) { this.carRepository = carRepository; } }