zoukankan      html  css  js  c++  java
  • @Autowired注解的使用

    使用Spring时,通过Spring注入的Bean一般都被定义成private,并且要有getter和setter方法,显得比较繁琐,增加了代码量,而且有时会搞忘造成错误。

    可以使用@Autowired注解来减少代码量。首先,在applicationContext中加入:

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

    Spring使用这个BeanPostProcessor解析@Autowired注解。

    然后,在变量上添加@Autowired注解,并去掉相应的getter和setter方法:

    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;
        
      ...
        
    }

    并且在applicationContext中将相应的<property></property>标签去掉:

        <bean id="clasService" class="com.school.service.ClasServiceImpl">
        </bean> 

    Spring启动时,AutowiredAnnotationBeanPostProcessor会扫描所有的Bean,当发现其中有@Autowired注解时,就会找相应类型的Bean,并且实现注入。

  • 相关阅读:
    Poj3678:Katu Puzzle
    2-SAT
    Bzoj3238: [Ahoi2013]差异
    expressJS
    expressJS
    expressJS
    [转]View属性 之 paddingStart & paddingEnd
    在Activity之间使用Intent传值和Bundle传值的区别和方式
    [转]Java初始化顺序总结
    final关键字修饰的变量
  • 原文地址:https://www.cnblogs.com/mstk/p/6213867.html
Copyright © 2011-2022 走看看