一、说明

与@Component注解功能相同,但意义不同的注解还有三个:

1)@Repository:注解在Dao实现类上
2)@Service:注解在Service实现类上
3)@Controller:注解在SpringMVC的处理器上

Bean作用域:
@Scope("prototype"):用于指定对象创建模式,可以是单例模式或者原型模式,默认是singleton

基本类型属性注入:
@Value

@Autowired:byType方式的注解式注入,即根据类型注解
@Qualifier("mySchool"):byName方式的注解式注入,在使用@Qualifier时必须与@Autowired联合使用

域属性注解:
@Resource:不加name属性则为byType方式的注解式注入,但前提是注入的对象只能有一个
@Resource(name="mySchool"):byName方式的注解式注入

Bean的生命始末:
@PostConstruct:当前Bean初始化刚完毕
@PreDestroy:当前Bean即将被销毁

@Configuration:表示当前类充当Spring容器,即所有的Bean将由这个类来创建

注意:

  在举例之前声明几个问题:

  1、注解需要依赖spring-aop-4.3.9.RELEASE.jar包,所以需要导入依赖包。

  2、使用注解方式注入,配置文件需要添加约束头文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

  也可以自己从Spring的说明文档中找到此头文件:

  3、如果使用到了SpringJUnit4测试,则还需要导入spring-test-4.3.9.RELEASE.jar包

二、举例

1、首先创建一个School类:

  1. package com.ietree.spring.basic.annotation.demo1;
  2.  
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.stereotype.Component;
  5.  
  6. @Component("mySchool")
  7. public class School {
  8.  
  9. @Value(value = "清华大学")
  10. private String name;
  11.  
  12. public School() {
  13. super();
  14. }
  15.  
  16. public School(String name) {
  17. super();
  18. this.name = name;
  19. }
  20.  
  21. public void setName(String name) {
  22. this.name = name;
  23. }
  24.  
  25. @Override
  26. public String toString() {
  27. return "School [name=" + name + "]";
  28. }
  29.  
  30. }

创建Student类:

  1. package com.ietree.spring.basic.annotation.demo1;
  2.  
  3. import javax.annotation.PostConstruct;
  4. import javax.annotation.PreDestroy;
  5. import javax.annotation.Resource;
  6.  
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.annotation.Qualifier;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.stereotype.Component;
  11.  
  12. /**
  13. * 说明:
  14. * 与@Component注解功能相同,但意义不同的注解还有三个:
  15. * 1)@Repository:注解在Dao实现类上
  16. * 2)@Service:注解在Service实现类上
  17. * 3)@Controller:注解在SpringMVC的处理器上
  18. *
  19. * Bean作用域:
  20. * @Scope("prototype"):用于指定对象创建模式,可以是单例模式或者原型模式,默认是singleton
  21. *
  22. * 基本类型属性注入:
  23. * @Value
  24. *
  25. * @Autowired:byType方式的注解式注入,即根据类型注解
  26. * @Qualifier("mySchool"):byName方式的注解式注入,在使用@Qualifier时必须与@Autowired联合使用
  27. *
  28. * 域属性注解:
  29. * @Resource:不加name属性则为byType方式的注解式注入,但前提是注入的对象只能有一个
  30. * @Resource(name="mySchool"):byName方式的注解式注入
  31. *
  32. * Bean的生命始末:
  33. * @PostConstruct:当前Bean初始化刚完毕
  34. * @PreDestroy:当前Bean即将被销毁
  35. */
  36. //@Scope("prototype")
  37. @Component("myStudent")
  38. public class Student {
  39.  
  40. @Value(value = "小明")
  41. private String name;
  42.  
  43. @Value(value = "25")
  44. private int age;
  45.  
  46. // @Autowired
  47. // @Qualifier("mySchool")
  48. // @Resource(name="mySchool")
  49. @Resource
  50. private School school;// 对象属性,也叫做域属性
  51.  
  52. public Student() {
  53. super();
  54. }
  55.  
  56. public Student(String name, int age) {
  57. super();
  58. this.name = name;
  59. this.age = age;
  60. }
  61.  
  62. public void setName(String name) {
  63. System.out.println("执行setName()");
  64. this.name = name;
  65. }
  66.  
  67. public void setAge(int age) {
  68. System.out.println("执行setAge()");
  69. this.age = age;
  70. }
  71.  
  72. public void setSchool(School school) {
  73. this.school = school;
  74. }
  75.  
  76. @Override
  77. public String toString() {
  78. return "Student [name=" + name + ", age=" + age + ", school=" + school + "]";
  79. }
  80.  
  81. @PostConstruct
  82. public void initAfter(){
  83. System.out.println("当前Bean初始化刚完毕");
  84. }
  85.  
  86. @PreDestroy
  87. public void preDestroy(){
  88. System.out.println("当前Bean即将被销毁");
  89. }
  90. }

创建MyJavaConfig类:

  1. package com.ietree.spring.basic.annotation.demo1;
  2.  
  3. import org.springframework.beans.factory.annotation.Autowire;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6.  
  7. /**
  8. * @Configuration:表示当前类充当Spring容器,即所有的Bean将由这个类来创建
  9. */
  10. @Configuration
  11. public class MyJavaConfig {
  12.  
  13. @Bean(name="mySchool")
  14. public School mySchoolCreator(){
  15. return new School("清华大学");
  16. }
  17.  
  18. // autowire=Autowire.BY_TYPE:指从当前类这个容器中查找与域属性的类型具有is-a关系的Bean
  19. // autowire=Autowire.BY_NAME:指从当前类这个容器中查找与域属性同名的Bean
  20. @Bean(name="myStudent", autowire=Autowire.BY_TYPE)
  21. public Student myStudentCreator(){
  22. return new Student("小明", 25);
  23. }
  24. }

创建配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  7.  
  8. <!-- 扫描 com.ietree.spring.basic.annotation.demo1这个包及其子包 -->
  9. <context:component-scan base-package="com.ietree.spring.basic.annotation.demo1"/>
  10.  
  11. <!-- 扫描 com.ietree.spring.basic这个包的子包 -->
  12. <context:component-scan base-package="com.ietree.spring.basic.*"/>
  13.  
  14. </beans>

创建测试类:

  1. package com.ietree.spring.basic.annotation.demo1;
  2.  
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. import org.springframework.test.context.ContextConfiguration;
  9. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  10.  
  11. @RunWith(SpringJUnit4ClassRunner.class)
  12. @ContextConfiguration(locations="classpath:com/ietree/spring/basic/annotation/demo1/applicationContext.xml")
  13. public class MyTest {
  14.  
  15. @Autowired
  16. private Student student;
  17.  
  18. @Test
  19. public void test01() {
  20.  
  21. String resource = "com/ietree/spring/basic/annotation/demo1/applicationContext.xml";
  22. ApplicationContext ctx = new ClassPathXmlApplicationContext(resource);
  23.  
  24. School school = (School) ctx.getBean("mySchool");
  25. System.out.println(school);
  26.  
  27. Student student = (Student) ctx.getBean("myStudent");
  28. System.out.println(student);
  29.  
  30. ((ClassPathXmlApplicationContext)ctx).close();
  31. }
  32.  
  33. public void test02(){
  34. System.out.println(student);
  35. }
  36.  
  37. }

注意:如果配置了XML,同时又配置了注解,那么程序会首先选择XML配置创建对象。

Spring框架第四篇之基于注解的DI注入的更多相关文章

  1. Spring框架第三篇之基于XML的DI注入

    一.注入分类 Bean实例在调用无参构造器创建空值对象后,就要对Bean对象的属性进行初始化.初始化是由容器自动完成的,称为注入.根据注入方式的不同,常用的有两类:设值注入.构造注入.实现特定接口注入 ...

  2. SSM-Spring-07:Spring基于注解的di注入

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 注解: 说起注解,哇哦,每个人都或多或少的用到过 像什么@Overried,@Test,@Param等等之前就 ...

  3. 07 Spring框架 依赖注入(四)基于注解的依赖注入

    前面几节我们都在使用xml进行依赖的注入,但是在实际的开发中我们往往偏爱于使用注解进行依赖注入,因为这样更符合我们人的思维,并且更加快捷,本节就来讲述Spring基于注解的依赖注入: 信息注入注解 @ ...

  4. Spring的第四天AOP之注解版

    Spring的第四天AOP之注解版 ssm框架 spring  在上一篇博客中,介绍了Spring的AOP的xml版本的使用,在这篇博客中,我将介绍一下,注解版的使用. 常用注解 注解 通知 @Aft ...

  5. Spring声明式事务管理(基于注解方式实现)

    ----------------------siwuxie095                                 Spring 声明式事务管理(基于注解方式实现)         以转 ...

  6. 解决Spring框架的Dao层改用@Repository注解,无法使用JdbcDaoSupport的问题

    解决Spring框架的Dao层改用@Repository注解,无法使用JdbcDaoSupport的问题 Alternatively, create an own implementation of ...

  7. 使用Spring框架入门四:基于注解的方式的AOP的使用

    一.简述 前面讲了基于XML配置的方式实现AOP,本文简单讲讲基于注解的方式实现. 基于注解的方式实现前,要先在xml配置中通过配置aop:aspectj-autoproxy来启用注解方式注入. &l ...

  8. spring框架学习(四)——注解方式AOP

    注解配置业务类 使用@Component("s") 注解ProductService 类 package com.how2java.service; import org.spri ...

  9. Spring Cloud第四篇 | 客户端负载均衡Ribbon

    ​ 本文是Spring Cloud专栏的第四篇文章,了解前三篇文章内容有助于更好的理解本文: ​Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...

随机推荐

  1. 按SCI影响因子排序的前50人工智能期刊列表

    附录二:按SCI影响因子排序的前50人工智能期刊列表 出版物名称,影响因子 IEEE TRANSACTIONS ON FUZZY SYSTEMS, 6.701    International Jou ...

  2. 006杰信—factory更新数据

    本博客的资源全部来源于传智播客. factroy更新的执行流程和003杰信-在jsp页面输入数据,然后在oracle数据库中插入factory数据,当字段允许为空时要特殊处理差不多, 1.在jFact ...

  3. Spring MVC属于SpringFrameWork的后续产品

    Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring MVC 分离了控制器.模型对象.分派器以及处理程序对象的角色,这种分离让它 ...

  4. Unity3D项目之 Survival Shooter 记录

    1.导入资源 2.把预设文件的环境拖到场景中, 3.位置归0 4.保存场景 5.删除默认灯光,把预设灯光拖到场景中,位置归0 6.新建一个 Quad 7.旋转90度,设置缩放100,100,1 重命名 ...

  5. android 开发之hello world!

    http://blog.sina.com.cn/s/blog_4e08922b0100nh6e.html http://blog.csdn.net/poechant/article/details/7 ...

  6. 微软ASP.NET网站部署指南(8):部署Code-Only更新

    1.  综述 初始化部署以后,你须要继续维护和更新你的网站.本章节将向你展示一个不包含数据库改变的部署升级流程.(下一章节将展示数据库改变的部署升级流程.) 提醒:假设依据本章节所做的操作出现错误信息 ...

  7. thinkPHP 上传文件的中文乱码

    最新版本~用了里面的上传文件类,发现在保存文件原本名称的时候当有中文名的时候保存文件会显示乱码,看了下源代码发现在Tp上传驱动那里有点问题. // if (!move_uploaded_file($f ...

  8. linux上限制用户进程数、cpu占用率、内存使用率

    限制进程CPU占用率的问题,给出了一个shell脚本代码如下: renice +10 `ps aux | awk '{ if ($3 > 0.8 && id -u $1 > ...

  9. superresolution_v_2.0 Application超分辨率程序文档

    SUPERRESOLUTION GRAPHICAL USER INTERFACE DOCUMENTATION Contents 1.- How to use this application. 2.- ...

  10. 【代码备份】原图降采样后进行NLM滤波

    文件路径: 滤波算法main.m: %% 测试函数 %NLM滤波及滤波与 clc,clear all,close all; ima_ori=double(imread('F:\Users\****n\ ...