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. HDOJ 1312 (POJ 1979) Red and Black

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  2. CSS3 概览 更新时间 2014-0412-1317

    CSS3 概览 CSS3可以划分为:文字.边框模型.背景.动画等. CSS3颜色模块 CSS2.1的时候可以使用4种颜色方式,直接使用颜色名,如 redRGB值,如 rgb(0,90,255)RGB百 ...

  3. hdu 4280 最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4280 #pragma comment(linker, "/STACK:1024000000, ...

  4. springMVC能做什么,做j2ee时候要考虑什么

    转载: http://jinnianshilongnian.iteye.com/category/231099 [置顶] 跟我学SpringMVC目录汇总贴.PDF下载.源码下载 博客分类: 跟开涛学 ...

  5. Redis教程01——命令

    APPEND key value追加一个值到key上 AUTH password验证服务器 BGREWRITEAOF 异步重写追加文件 BGSAVE 异步保存数据集到磁盘上 BITCOUNT key ...

  6. 错误137(net::ERR_NAME_RESOLUTION_FAILED):未知错误的解决办法

    现象:之前遇到一些新闻网站打不开的情况...而让异地的朋友打开却能打开.. 解决①:配置dns ,因为公司内部的网络是桥接的 我们dns服务器默认是192.168.1.1 ,dns被封锁,直接导致一些 ...

  7. reloadData should be in main thread

    reloadData should be called in main thread, so if you call it in work thread, you should call it as ...

  8. Linux下安装MySQL5.6

    传送门:http://www.jianshu.com/p/f4a98a905011 字数802 阅读164 评论0 喜欢4 环境:1.操作系统:CentOS release 6.8 (Final)2. ...

  9. uvalive4513

    https://vjudge.net/problem/UVALive-4513 终于做出来了......... 各种sb错误,最后对拍出来了,还没改对..................... 快半天 ...

  10. EnterpriseArchitectect 软件的勾选的几个选项对应的中文意思

    Business Process 业务流程 Requirements 需求分析 Use Case 用例 Domain Model 领域模型 Class 类 Database 数据库设计 Compone ...