@ Autowired annotation, @ autowired Annotation

Source: Internet
Author: User

@ 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.