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. Solr -- Solr Facet 2

    solr将以导航为目的的查询结果称为facet. 它并不会修改查询结果信息, 只是在查询结果上根据分类添加了count信息, 然后用户根据count信息做进一步的查询, 比如淘宝的查询列表中, 上面会 ...

  2. solrnet - document

      Overview and basic usage Mapping Initialization Create/Update/Delete Querying Faceting Highlightin ...

  3. 【转】Android Listener侦听的N种写法

    原文网址:http://blog.csdn.net/ithomer/article/details/7489274 Android中,View的Listener方法,在是否使用匿名类匿名对象时,有各种 ...

  4. 【转】压缩Virtualbox的vdi文件

    原文网址:http://i.rexdf.org/blog/2014/10/06/ya-suo-virtualboxde-vdiwen-jian/ 问题实际上比较简单,我在Arch Linux杂记中给出 ...

  5. (转载)PHP isset()函数作用

    (转载)http://www.cnblogs.com/neve/archive/2011/03/21/1990165.html isset函数是检测变量是否设置. 格式:bool isset ( mi ...

  6. 字符串(后缀数组):HAOI2016 找相同子串

    [HAOI2016]找相同子串 [题目描述] 给定两个字符串,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两个子串中有一个位置不同. [输入格式] 两行,两个字符 ...

  7. 游戏开发设计模式之对象池模式(unity3d 示例实现)

    前篇:游戏开发设计模式之命令模式(unity3d 示例实现) 博主才学尚浅,难免会有错误,尤其是设计模式这种极富禅意且需要大量经验的东西,如果哪里书写错误或有遗漏,还请各位前辈指正. 原理:从一个固定 ...

  8. eclipse配置j2ee项目

    1.下载jdk (1.5,1.6) 安装 从sun的官方网站下载,我下的是jdk-1_5_0_19-nb-6_5_1-windows-ml.exe,集成netbean的版本,下载后一路默认安装. 配置 ...

  9. ethtool的基本设置

    ethtool是设置网卡属性.IP/TCP相关协议属性的基本工具,功能还是很强大的.ubuntu的版本里没有默认集成,使用apt-get install 安装之.... 使用概要:ethtool et ...

  10. 《A First Course in Probability》-chaper4-离散型随机变量-随机变量和或积的期望

    基于随机变量一系列最基本的概念,我们便会开始探讨一些随机变量函数的期望.多个随机变量和的期望.多个随机变量乘积的期望. 这里我们就简单的讨论后两个期望. 这两个问题可以基于如下的一个现实模型: 同时掷 ...