zoukankan      html  css  js  c++  java
  • Spring基于 @Autowired 和@Required区别与联系

    @Autowired  可以使用在Setter 方法中,属性,构造函数中。

    例如:在属性当中使用@Autowired,这里是 TextEditor.java 文件的内容:

     1 package com.spring.chapter6;
     2 
     3 import java.util.List;
      4 import java.util.Set;
     5 
      6 import org.springframework.beans.factory.annotation.Autowired;
      7 import org.springframework.beans.factory.annotation.Required;
     8 
      9 
     10 
     11 public class TextEditor {
     12 
     13  
     20     @Autowired
     21     private SpellChecker spellChecker;
     22     
     23     public void getSpell(){
     24         spellChecker.checkSpelling();
     25         
     26     }

    下面是另一个依赖的类文件 SpellChecker.java 的内容:

     1 package com.spring.chapter6;
     2 
     3 public class SpellChecker {
     4     
     5     public SpellChecker(){
     6           System.out.println("Inside SpellChecker constructor." );
     7        }
     8        public void checkSpelling(){
     9           System.out.println("Inside checkSpelling." );
    10        }  
    11 
    12 }

    下面是 MainApp.java 文件的内容:

     1 package com.spring.chapter6;
     2 
     3 
     4 import java.util.List;
     5 
     6 import org.springframework.context.ApplicationContext;
     7 import org.springframework.context.support.AbstractApplicationContext;
     8 import org.springframework.context.support.ClassPathXmlApplicationContext;
     9 
    10 public class Main {
    11     /**
    12      * Spring @autowired 注解注入
    13      * author:
    14      * mail:2536201485@qq.com
    15      * 时间:
    16      */
    17 
    18     public static void main(String[] args) {
    19         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring_xml/spring.xml");
    20         TextEditor editor=(TextEditor)applicationContext.getBean("textEditor");
    21         editor.getSpell();
    22         
    23         
    24     }
    25 
    26 }

    下面是配置文件 spring.xml

    1     <!-- @autowired 注解-->
    2     <bean id="textEditor" class="com.spring.chapter6.TextEditor">
    3     </bean>
    4     <bean id="SpellChecker" class="com.spring.chapter6.SpellChecker">
    5     </bean>

     

    运行结果:

    Inside SpellChecker constructor.
    Inside checkSpelling.

    @Required和@Autowired的区别:

      @Required

    @Autowired

    区别

    1.@Required作用在Setter方法上(用于检查一个Bean的属性的值在配置期间是否被赋予或设置(populated))

    2.@Required作用在Setter方法上就必须赋值,否则容器就会抛出一个 BeanInitializationException 异常。

    1.@Autowired  可以作用在Setter 方法中,属性,构造函数中

    2.可以使用 @Autowired 的 (required=false) 选项关闭默认行为。也就是被标注的属性不会被赋值

    联系 1.@Required作用在Setter方法上需要生成Setter方法

    1.@Autowired 作用在Setter 方法也需要生成Setter方法

    2.@Autowired 作用在属性上,则可以省略Setter方法,根据Bean类型来注入

  • 相关阅读:
    FastJson--阿里巴巴公司开源的速度最快的Json和对象转换工具
    如何去设计一个自适应的网页设计或HTMl5
    教育行业SaaS选型 需要注意的三点问题
    SaaS系列介绍之十五: SaaS知识重用
    SaaS系列介绍之十四: SaaS软件开发分析
    SaaS系列介绍之十三: SaaS系统体系架构
    SaaS系列介绍之十二: SaaS产品的研发模式
    个人总结
    用例图设计
    第二次结对作业
  • 原文地址:https://www.cnblogs.com/ysource/p/12787367.html
Copyright © 2011-2022 走看看