首页 > 技术文章 > @Resource

tonggc1668 2018-10-04 14:46 原文

package com.test;

import javax.annotation.Resource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class AtInterfaceTest {
  public static void main(String[] args) {
    String path = AtInterfaceTest.class.getClassLoader().getResource("").getPath();
    System.out.println("path = " + path);
    String filepath = path + "/sss.xml";

    ApplicationContext ctx = new FileSystemXmlApplicationContext(filepath);
    Person p = (Person) ctx.getBean("person");
    p.say();
  }
}


class Student {
  void say() {
    System.out.println("hello");
  }
}


class Person {
  @Resource(name = "student")
  //@Resource
  private Student student;

  void say() {
    this.student.say();
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <!--
            (只有下面的是需要自己添加的,其他的都是在新建spring配置xml文件的时候,就自带的啦)
            1、导入基于注解的xsd
                 xmlns:context="http://www.springframework.org/schema/context"
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context-2.5.xsd
          2、导入注解解析器
              <context:annotation-config></context:annotation-config>
             3、导入person和student
     -->
 
    <context:annotation-config/>
 
    <bean id="person" class="com.test.Person"/>
    <bean id="student" class="com.test.Student"/>
 
</beans>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com</groupId>
  <artifactId>test</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>test</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

<parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>1.5.9.RELEASE</version>  
        <relativePath/> 
  </parent>  
    
    
  <dependencies>
     <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
     </dependency>  
       
    <dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency>

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.1</version>
</dependency>
 </dependencies>
  
   <!-- Package as an executable jar -->  
    <build>  
        <plugins>  
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
            </plugin>  
        </plugins>  
    </build>  
  
</project>
  • @Resource默认按byName自动注入。
  • 既不指定name属性,也不指定type属性,则自动按byName方式进行查找。如果没有找到符合的bean,则回退为一个原始类型进行进行查找,如果找到就注入。
  • 只是指定了@Resource注解的name,则按name后的名字去bean元素里查找有与之相等的name属性的bean。
  • 只指定@Resource注解的type属性,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常。

推荐阅读