zoukankan      html  css  js  c++  java
  • @Resource

    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进行装配,找不到或者找到多个,都会抛出异常。
  • 相关阅读:
    产生一个int数组,长度为100,并向其中随机插入1-100,并且不能重复。
    it人必进的几大网站
    可写可选dropdownlist(只测试过ie)
    Datatable转换为Json 的方法
    ref 和out的区别
    数据库事务
    Webservice 的安全策略
    【转】Zookeeper解析、安装、配置
    【转】activemq的几种基本通信方式总结
    【转】Java小应用:Eclipse中建立自己的类库,给不同的工程使用
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/9742236.html
Copyright © 2011-2022 走看看