zoukankan      html  css  js  c++  java
  • @Autowired @Resource @Inject 自动注入

    一、@AutoWired ( spring 的注解 )自动注入

    /**
     * @Autowired:
     *         默认按照 Student 类型去容器中找对应的组件:applicationContext.getBean(Student.class);
     *         如果找到多个相同类型的组件,再将 student 这个属性名作为 id 去容器中找对应组件 applicationContext.getBean("student");
     *         required = false,容器中如果没有该组件,就为 null
     * @Qualifier:
     *         指定需要装配的组件 id,而不是使用 student 这个属性名作为 id
     */
    @Qualifier("student2")
    @Autowired(required = false)
    private Student student;

    二、@Resource ( JSR250 规范的注解 )

    /**
     * @Resource:
     *         默认按照组件名 student 作为 id 去容器中找对应的属性
     *         使用 name = "student2" 指定 id
    *     没有 required = false 功能  
    */ @Resource(name = "student2") private Student student;

    三、@Inject ( JSR330 规范 ) 和 @AutoWired 功能一样。但是没有 required = false  功能

      导入 jar

    <!-- https://mvnrepository.com/artifact/javax.inject/javax.inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>
    @Inject
    private Student student;
  • 相关阅读:
    Matplotlib.pyplot 三维绘图
    Matplotlib.pyplot 二维绘图
    面对对象进阶
    面对对象基础
    python安装第三方模块
    json & pickle
    os模块
    sys模块
    正则表达式
    Python2与Python3的编码差异
  • 原文地址:https://www.cnblogs.com/fangwu/p/8685906.html
Copyright © 2011-2022 走看看