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. mysql排序,可以对统计的数据进行排序

    sql SELECT a.*,b.name as address_name,b.tel as address_tel,b.province as address_province,b.city as ...

  2. WordPress wp-admin/includes/post.php脚本安全漏洞

    漏洞名称: WordPress wp-admin/includes/post.php脚本安全漏洞 CNNVD编号: CNNVD-201309-168 发布时间: 2013-09-13 更新时间: 20 ...

  3. Light OJ 1031 - Easy Game(区间DP)

    题目大意: 给你一个n,代表n个数字,现在有两个选手,选手A,B轮流有有一次机会,每个选手一次可以得到一个或者多个数字,从左侧或者右侧,但是不能同时从两边取数字,当所有的数字被取完,那么游戏结束.然后 ...

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

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

  5. 计算机视觉code与软件

    Research Code A rational methodology for lossy compression - REWIC is a software-based implementatio ...

  6. jQuery 遍历

    .add() add() 方法将元素添加到匹配元素的集合中 .add(selector)//字符串值,表示查找供添加到匹配元素集合的元素的选择器表达式. .add(elements)//添加到匹配元素 ...

  7. phpmyadmin万能密码

    影响版本:2.11.3 / 2.11.4 利用方法:用户名处写入‘localhost’@'@”则登录成功. (注意全部是英文标点符号,最后一个为英文双引号)

  8. 深入hibernate的三种状态【转载】

    这篇文章写得很好,举了很多例子. http://www.cnblogs.com/xiaoluo501395377/p/3380270.html

  9. Myeclipse Professional 2014使用软件获取激活码的方法(声明:破解仅仅是为个人使用Myeclipse,不作为商业获利.商业活动请购买正版软件)

    一下说明全部为网络来源,已经忘了原地址了,一直保留的是获取激活码用的软件和说明, 原著看见可联系(非常谢谢原作者): 获取激活码用的软件下载地址(个人百度云):http://pan.baidu.com ...

  10. Selenium+Java+TestNG环境配置

    1. JDK 2.eclipse+TestNG >TestNG安装.   Name:testng  Location:http://beust.com/eclipse.如图: 3.seleniu ...