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效率比较高,但是书写比较麻烦
          注解效率比较低,书写比较简单

Spring 中的注解的更多相关文章

  1. Spring中@Autowired注解、@Resource注解的区别 (zz)

    Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...

  2. Spring中Value注解的使用

    Spring中Value注解的使用 分类: Spring2014-08-16 17:28 2985人阅读 评论(0) 收藏 举报 有的时候我们定义了Properties文件,并且使用Spring的Pr ...

  3. 全面解析Spring中@ModelAttribute注解的用法

    本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:全面解析Spring中@ModelAttribute注解的用法: @ModelAttribute注解用于将方法的参数或方法的返回值绑定到 ...

  4. EnableAutoConfiguration注解 Spring中@Import注解的作用和使用

    EnableAutoConfiguration注解 http://www.51gjie.com/javaweb/1046.html springboot@EnableAutoConfiguration ...

  5. Spring中常用注解的介绍

    spring中使用注解时配置文件的写法: <?xml version="1.0" encoding="UTF-8"?> <span style ...

  6. Spring中使用注解时启用<context:component-scan/>

    在spring中使用注解方式时需要在spring配置文件中配置组件扫描器:http://blog.csdn.net/j080624/article/details/56277315 <conte ...

  7. Spring中异步注解@Async的使用、原理及使用时可能导致的问题

    前言 其实最近都在研究事务相关的内容,之所以写这么一篇文章是因为前面写了一篇关于循环依赖的文章: <面试必杀技,讲一讲Spring中的循环依赖> 然后,很多同学碰到了下面这个问题,添加了S ...

  8. Spring中@Import注解的使用

    Spring中@Import注解的使用 @Import注解算是SpringBoot自动配置原理中一个很重要的注解 认识@Import注解 先看一下源码 @Target(ElementType.TYPE ...

  9. Spring中常用注解

    1.@Component 创建类对象,相当于配置<bean/> 2.@Service @Service与@Component功能相同,写在ServiceImpl类上 3.@Reposito ...

  10. 【进阶】Spring中的注解与反射

    [进阶]Spring中的注解与反射 目录 [进阶]Spring中的注解与反射 前言 一.内置(常用)注解 1.1@Overrode 1.2@RequestMapping 1.3@RequestBody ...

随机推荐

  1. android获取设备屏幕大小的方法

    // 通过WindowManager获取 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay( ...

  2. HDU-1963

    http://acm.hdu.edu.cn/showproblem.php?pid=1963 完全背包. 题意:给出初始资金,还有年数,然后给出每个物品的购买价格与每年获得的利益,要求在给出的年份后所 ...

  3. Poj 2887-Big String Splay

    题目:http://poj.org/problem?id=2887       Big String Time Limit: 1000MS   Memory Limit: 131072K Total ...

  4. C++Primer第5版学习笔记(三)

    C++Primer第5版学习笔记(三) 第四/五章的重难点内容           你可以点击这里回顾第三章内容       因为第五章的内容比较少,因此和第四章的笔记内容合并.       第四章是 ...

  5. Live555 Streaming from a live source

    https://www.mail-archive.com/live-devel@lists.live555.com/msg05506.html-----ask--------------------- ...

  6. linux0.12 编译过程

    感谢这篇文章的作者:    http://www.cnblogs.com/strugglesometimes/p/4231359.html 编译是个很蛋疼的事情,本想把linux0.12在bochs上 ...

  7. 关于Cookie的有关内容

    1.首先谈谈http协议与状态保持. Http协议本身是无状态的,这与http协议本来的目的是相符的,客户端只需要简单的向服务器发出请求操作,然后服务器对请求作出响应.无论客户端还是服务器都没有必要记 ...

  8. 放爬虫nginx

    禁止火狐浏览器访问 例子

  9. Away3d 骨骼动画优化

    很多朋友说Away3D 的骨骼数限制在32根,确切的说应该是Stage3D 的限制.在 AGAL2.0之前 VC寄存器是128个,每个vc常量寄存器最大只能容纳4位,transform占用一个4*4的 ...

  10. Oracle 同步表权限分配(同义词)

    新建了同义词之后还要 分配权限