Article Directory
Component scanning: After the Spring container starts, it will check a certain package and all the classes under its sub-packages. If there is a specific annotation (such as @Component) in front of the class, the container will incorporate the class into the container for management. (Using annotations saves us from configuring a Bean reference in XML, which is equivalent to configuring a Bean element)
The annotation configuration is turned off in Spring by default, and we need to activate it using <context:annotation-config/> in the configuration file.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
</beans>
Once the annotation configuration is activated, we can use annotations in the code for dependency injection. The following are some important notes:
@Required: The annotation is applied to the setter method of the bean property.
@Autowired: Annotations can be applied to setter methods, non-setter methods, constructors and properties of bean properties
@Qualifier, by specifying the exact bean to be referenced, @Autowired and @Qualifier annotations can be used to remove confusion