Spring 中的注解

1、普通方式注解

  a、在配置文件中配置

     1、导入命名空间
              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、把student和person导入进来

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context 
            http://www.springframework.org/schema/context/spring-context-2.5.xsd">
                            
    <context:annotation-config></context:annotation-config>
    <bean id="student" class="cn.test.annotation2.Student"></bean>
    <bean id="person" class="cn.test.annotation2.Person"></bean>
</beans>
配置Demo

  b、再类中添加注解@Resource--字段注解   @PostConstruct--初始化注解    @PreDestroy--销毁注解

package cn.test.annotation2;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

public class Person {
    
    private Long pid;
    @Resource(name="student")
    //@Autowired//按照类型进行匹配
    //@Qualifier("student")
    private Student student;
    
    public void say(){
        this.student.say();
    }
    
    @PostConstruct
    public void init(){
        System.err.println("Person Init");
    }
    
    @PreDestroy
    public void destory(){
        System.err.println("Person distory");
    }
}
类注解Demo

  c、测试

   原理
         启动spring容器,并且加载配置文件会为student和person两个类创建对象
         当解析到<context:annotation-config></context:annotation-config>会启动依赖注入的注解解析器
         会在纳入spring管理的bean的范围内查找看哪些bean的属性上有@Resource注解
           如果@Resource注解的name属性的值为"",则会把注解所在的属性的名称和spring容器中bean的id进行匹配如果匹配成功,则把id对应的对象赋值给该属性,如果匹配不成功,则按照类型进行匹配,如果再匹配不成功,则报错
       如果@Resource注解的name属性的值不为"",会把name属性的值和spring容器中bean的id做匹配,如果匹配成功,则赋值,如果匹配不成功 ,则直接报错
  说明:
     注解只能用于引用类型

@Test
    public void dosome(){
        ClassPathXmlApplicationContext applicationContext=new ClassPathXmlApplicationContext("cn/test/annotation2/applicationContext.xml");
        Person person= (Person) applicationContext.getBean("person");
        person.say();
        applicationContext.close();
    }
客户端测试Demo

2、扫描注解

  1、配置
          1、导入命名空间
              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、 component就是bean
            base-package
                会在base-package的值所在的包及子包下扫描所有的类
        <context:component-scan base-package="cn.test.annotation.scan"></context:component-scan>

  2、在类中添加注释

    @Component
     public class Person {   
        @Resource
        private Student student;

      }

  3、测试

  原理
          启动spring容器,加载配置文件
          spring容器解析到
              <context:component-scan base-package="cn.test.annotation.scan"></context:component-scan>
          spring容器会在指定的包及子包中查找类上是否有@Component
          如果@Component注解没有写任何属性
             @Component
             public class Person{   }
             ==
             <bean id="person" class="..Person">
           如果@Component("aa")
             @Component
             public class Person{ }
             ==
             <bean id="aa" class="..Person">
           在纳入spring管理的bean的范围内查找@Resource注解
           执行@Resource注解的过程
       说明:
          xml效率比较高,但是书写比较麻烦
          注解效率比较低,书写比较简单
          

原文地址:https://www.cnblogs.com/liuwt365/p/4227261.html