想要使用注解,首先在Spring的配置文件里面  引入命名空间及schema文件

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"<span style="background-color: rgb(51, 204, 255);">xmlns:context="http://www.springframework.org/schema/context</span>"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd<span style="color:#3366ff;">http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd</span><span style="color:#66ff99;"> </span>">

注释注入有两种@Resource 默认按名称,当名称匹配不到时,再按类型和@AutoWried按类型匹配

可以对字段进行注释,也可对setter方法进行注释;

--下面的代码是模拟Spring的注释注入

test代码

package junit.test;import org.junit.BeforeClass;
import org.junit.Test;import service.PersonService;
import service.impl.PersonServiceImpl1;public class SpringTest {@BeforeClasspublic static void setUpBeforeClass() throws Exception {}@Test public void instanceSpring(){ItCastClassPathXmlApplicationContext ctx=new ItCastClassPathXmlApplicationContext("beans.xml");PersonService ps2=(PersonServiceImpl1)ctx.getBean("ss2");ps2.save();}
}

要注入其他bean的类

package service.impl;import junit.test.ItCastResource;
import service.PersonService;
import dao.PersonDAO;public class PersonServiceImpl1 implements PersonService{@ItCastResource private PersonDAO personDao;private String name="易家享";private Integer id=99;public PersonDAO getPersonDao() {return personDao;}public void setPersonDao(PersonDAO personDao) {this.personDao = personDao;}public PersonServiceImpl1(PersonDAO personDao, String name, Integer id) {super();this.personDao = personDao;this.name = name;this.id = id;}public PersonServiceImpl1() {super();}@Overridepublic void save() {System.out.println(name+"-------------"+id);personDao.add();}}

注释类

package junit.test;import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
//在哪里起效
@Retention(RetentionPolicy.RUNTIME)
//标注于哪里
@Target({ElementType.FIELD, ElementType.METHOD})   public @interface ItCastResource {public String name() default "";
}

重头戏来了,下面是模拟spring注释注入代码片段

 private void annotationInject() {for(String beanName:sigletons.keySet()){Object bean=sigletons.get(beanName);if(bean!=null){try {//获取bean类所有的属性PropertyDescriptor[] ps=Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();//循环所有属性  根据setter方法注释注入for(PropertyDescriptor propertyDescriptor:ps){//根据属性查找setter方法Method setter=propertyDescriptor.getWriteMethod();//当setter方法存在时。查看其是否有ItCastResource的注释if(setter!=null&&setter.isAnnotationPresent(ItCastResource.class)){//有,得到注释resourceItCastResource resource=setter.getAnnotation(ItCastResource.class);Object value=null;//如果注释name不为空if(resource.name()!=null&&!"".equals(resource.name())){//根据resource名字获得valuevalue=sigletons.get(resource.name());if(value==null){throw new Exception("配置文件里面没有配置id为"+resource.name()+"的bean");}}else{//如果注释没有name参数,或者name参数是空字符串//根据属性名字查找valuevalue=sigletons.get(propertyDescriptor.getName());//如果根据字段名称查找不到if(value==null){//将字段的的类型与配置文件里面所有的类匹配,查找是否有其子类或这是本类,查到即退出循环for(String key:sigletons.keySet()){if(propertyDescriptor.getPropertyType().isAssignableFrom(sigletons.get(key).getClass())){value=sigletons.get(key);break;}}}}//根据setter注入setter.setAccessible(true);setter.invoke(bean,value);}}//根据字段注释注入 获取bean类所有字段Field[] fields=bean.getClass().getDeclaredFields();//循环所有字段for(Field feild:fields){//当字段不为空并且存在注释时注入if(feild!=null&&feild.isAnnotationPresent(ItCastResource.class)){ItCastResource resource=feild.getAnnotation(ItCastResource.class);Object value=null;//注释name是否为空或空字符串if(resource.name()!=null&&!"".equals(resource.name())){//不为空,根据name查找value=sigletons.get(resource.name());if(value==null){throw new Exception("配置文件里面没有配置id为"+resource.name()+"的bean");}}else{//没有name属性,根据字段名称找value=sigletons.get(feild.getName());//找不到if(value==null){//根据字段类型,与配置文件所有的class匹配for(String key:sigletons.keySet()){if(feild.getType().isAssignableFrom(sigletons.get(key).getClass())){value=sigletons.get(key);break;}}}}feild.setAccessible(true);feild.set(bean,value);}}} catch (Exception e) {e.printStackTrace();}}}}

下面是spring的xml配置文件

 <bean id="ss2" class="service.impl.PersonServiceImpl1"></bean><bean id="personDaoXX" class="dao.impl.PersonDaoImpl"></bean>

@Autowired 注释注入

 //当(required=true)时,必须有,否则就会报错,(required=false)时,会返回null@Autowired(required=true) @Qualifier("personDaoXX")private PersonDAO personDao;

------------------------------- 工作中不建议使用---------------------------
自动装配:在配置文件要注入bean的autowire:

 <bean id="ss2" class="service.impl.PersonServiceImpl1" autowire="byType"></bean>
<bean id="ss2" class="service.impl.PersonServiceImpl1" autowire="byName"></bean>

按属性名字进行装配。当在配置文件中没有id为字段名称的bean时,会返回一个null对象。
按类型匹配,在容器里面发现多个相同类型时,会抛出异常。

Spring学习-黎活明视频学习注解相关推荐

  1. 让人才不再稀缺,让就业水到渠成 ——记传智播客总裁黎活明

    专访简介: 2017年3月21日,"传智专修学院成立大会暨揭牌仪式"在江苏沭阳文化艺术中心隆重举行.江苏宿迁市相关领导.沭阳县相关领导.互联网行业精英.相关媒体.传智播客及传智专修 ...

  2. 3G手机Android应用开发视频教程_黎活明老师的视频下载地址(总共有八天课程)

    视频下载地址:http://jiyanet.com/read.php?tid=404

  3. 3G手机Android应用开发视频教程_黎活明老师的视频(第三天课程)总共有八天课程...

    下载地址:http://jiyanet.com/read.php?tid=402 转载于:https://www.cnblogs.com/javaspring/archive/2012/08/21/2 ...

  4. 3G手机Android应用开发视频教程_黎活明老师的视频(第五天课程)总共有八天课程...

    下载地址:http://jiyanet.com/read.php?tid=404 转载于:https://www.cnblogs.com/javaspring/archive/2012/08/22/2 ...

  5. build.xml(黎活明安全管理部署文件)

    <?xml version="1.0"?> <!-- ====================================================== ...

  6. XML学习-方立勋视频学习

    xml文档:标签语言  有利于数据的描述和交换. <![CDATA[...]]>里面的内容保持原有的格式,不被解析. <?xml version="1.0" en ...

  7. java学习之路---java学习的方法和java学习路线

    转载于: http://blog.csdn.net/zhangerqing 前段时间逛论坛,总会有很多新手很迷茫,问到:到底该怎么学好Java,这个问题很重要,尤其对于像我们这样大多数都是靠自学的人来 ...

  8. unity 5.x从入门到精通_Unity学习中值得收藏的学习资料(书籍)与博客

    前言: 对于入门任何领域,收集相关比较优秀的学习资料,都是你不断提高自己的必备技能.而在信息爆炸的时代想要在繁杂的网络世界中找到好的学习资料,必然会耗费一定的时间和精力.为了给大家节省一点时间,在这里 ...

  9. Spring注解驱动开发学习总结8:自动装配注解@Autowire、@Resource、@Inject

    Spring注解驱动开发学习总结8:自动装配注解@Autowire.@Resource.@Inject 1.自动装配@Autowire.@Resource.@Inject 1.1 构建bookDao ...

  10. Java张孝祥视频 学习笔记 注解

    /***************************************************************************************/ 此博客主要是在观看张 ...

最新文章

  1. 成熟的AI应该自己写代码,IBM发布5亿行代码数据集,包含55种语言|开源
  2. 较深度地递归转义过滤
  3. shell中循环安装软件包
  4. deepin-安装问题:unable to find a medium containing a live file
  5. dedecms织梦模板|绿色仿下载吧网站织梦dede模板源码
  6. 基于JAVA+SpringBoot+Vue+Mybatis+MYSQL的物资管理系统
  7. invalid LOC header(bad signature)错误及解决
  8. matlab练习程序(Prim最小生成树)
  9. 如何在一个bat批处理文件中调用另一个bat批处理文件?
  10. 门诊管理系统开发能提高医生的诊疗水平和质量吗
  11. tl-wn821n无线网卡驱动 linux,tlwn821n 300m无线网卡驱动 官方最新版
  12. 分享:一个Java写的教师信息管理系统(windowbuilder)(附码云源码)~~~
  13. php工具箱升级MySQL
  14. Win10系统更新显卡驱动无限蓝屏重启-驱动人生解决方案
  15. SOA对话:金融风暴不会阻碍SOA市场发展
  16. Modelsim软件仿真出错:Modelsim is exiting with code 7.
  17. 【BZOJ1818】【CQOI2010】【XSY2428】内部白点(树状数组+扫描线)
  18. uni-app项目中引入Vant UI组件库(完美避坑!!!)纯净版
  19. 罗生门:动荡时代下人性的恶与善
  20. 微信群机器人助手多群同步群发消息,管理100个微信群!

热门文章

  1. 苹果ipa签名工具免越狱下载_苹果签名——超级篇
  2. 教你如何迅速秒杀掉:99%的海量数据处理面试题 1
  3. PowerDesigner详细安装教程
  4. mysql根据经纬度计算距离并排序
  5. 多智能体强化学习与博弈论-博弈论基础3
  6. NotNorthwind-更新#1-您所有的Northwind都属于我们
  7. 搭建php实验报告_php实验报告..doc
  8. PHP写评论模块,uchome2.0 日志评论模块分析(php代码及js代码分析)
  9. 如何看Dlink DWA-125 无线网卡使用的芯片
  10. 掌上书院UMD文件格式分析