@ Autowired annotation, @ autowired Annotation
When using Spring, beans injected through Spring are generally defined as private, and the getter and setter methods are required, which is cumbersome and increases the amount of code, and sometimes forget to cause errors.
You can use the @ Autowired annotation to reduce the amount of code. First, add the following to applicationContext:
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
Spring uses this BeanPostProcessor to parse the @ Autowired annotation.
Then, add the @ Autowired annotation to the variable and remove the corresponding getter and setter methods:
package com.school.service;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import com.school.dao.ClasDAO;import com.school.entity.Clas;public class ClasServiceImpl implements ClasService{ @Autowired private ClasDAO clasDAO; ... }
In applicationContext, remove the corresponding <property> </property> tag:
<bean id="clasService" class="com.school.service.ClasServiceImpl"> </bean>
When Spring is started, AutowiredAnnotationBeanPostProcessor scans all beans. When @ Autowired annotation is found, corresponding types of beans are found and injected.