一、组件自动扫描

可以按指定的包路径,将包下所有组件扫描,如果发现组件类定义前有以下标记,就会将组件扫描到容器。

@Component 其他组件

@Controller  控制层组件

@Service  业务层组件  XXXService

@Repository  数据访问层组件 XXXDao

@Named (不是Spring中定义的,需要引入第三方标准包)

组件自动扫描需要我们手动开启,开启方式即在applicationContext.xml中添加以下配置:

<context:component-scan  base-package=" "/>

例如:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.0.xsd" ><!-- 开启组件扫描 --><context:component-scan base-package="com.zlc.test"/>
</beans>

base-package属性说明:例如其值为com.zlc.test,即指定spring框架将对com.zlc.test包下以及子包下所有组件进行扫描。

这样我们就可以在该包路径下写相关的组件了,Spring框架将对其进行自动扫描:例如:

package com.zlc.test;import org.springframework.stereotype.Component;

@Component//扫描ExampleBean组件,默认id=exampleBean
public class ExampleBean {public void execute() {System.out.println("执行了方法");}
}

如果我们不想使用默认的id名,我们也可以这样为其指定特定的id名:

@Component("example")//指定id=example
public class ExampleBean {public void execute() {System.out.println("执行了方法");}
}

我们还可以像在xml文件中配置一样,指定该组件的某些属性,例如:scope, init-method, destroy-method, lazy-init等

例如:

//等价于指定<bean id="example">
@Component("example")
//等价于指定<bean scope="prototype">
@Scope("prototype")
//等价于指定<bean lazy-init="true">
@Lazy(true)
public class ExampleBean {//等价于<bean init-method="init">@PostConstructpublic void init() {System.out.println("初始化。。。");}//等价于<bean destroy-method="destroy">@PreDestroypublic void destroy() {System.out.println("释放资源。。。");}public void execute() {System.out.println("执行了方法");}
}

二、注解方式实现注入

(1)使用@Resource注解进行注入,注解可以加在需要注入的属性上,也可以加在相应的setter方法上

例如:

@Component("s")
public class Student {//定义在变量上
    @Resourceprivate Computer c;private Phone p;public void setC(Computer c) {this.c = c;}//定义在setter方法上
    @Resourcepublic void setP(Phone p) {this.p = p;}public void show() {System.out.println("学生信息。。。");c.show();p.show();}
}

(2)使用@Autowired注解进行注入,注解可以加在需要注入的属性上,也可以加在相应的setter方法上

例如:

@Component("s")
public class Student {//定义在变量上
    @Autowiredprivate Computer c;private Phone p;public void setC(Computer c) {this.c = c;}//定义在setter方法上
    @Autowiredpublic void setP(Phone p) {this.p = p;}public void show() {System.out.println("学生信息。。。");c.show();p.show();}
}

注意:我们甚至还可以去掉这两个属性的setter方法,直接在属性上添加@Resource或者@Autowired注解,这样也是可以完成注入的

例如:

@Component("s")
public class Student {//定义在变量上
    @Autowiredprivate Computer c;@Autowiredprivate Phone p;public void show() {System.out.println("学生信息。。。");c.show();p.show();}
}

使用@Resourse或者@Autowired注解方式进行注入的时候,默认使用的是"byName"的匹配方法,使用的是默认名称(即需要注入的属性的名称),如果"byName"的匹配方法找不到相应的组件,则会使用"byType"的匹配方法寻找相应的组件。

特别的,我们可以指定特定名称的组件进行注入

例如:

    @Autowired@Qualifier("p1")//指定id名称为p1的组件进行注入private Phone p;

或者

    @Resource(name="p1")//指定id名称为p1的组件进行注入private Phone p;

一般使用时,@Resource注解和@Autowired注解可以看做是等价的。

但是这两者在某些情况下仍是有些细微的差别的:

a、当注解直接声明在属性变量上,两者无差别,但是声明在setter方法上,默认使用 "byName"方法去匹配将要注入的bean时,选择的Name不一样。

如:

 //@Resource是以setXXX方法名中XXX首字母小写后的名字去进行匹配的,即标红的单词首字母小写public void setP(Phone p) {this.p = p;}

但是:

 //@Autowired是以setXXX方法中参数名去匹配的,即标红的单词public void setP(Phone p) {this.p = p;}

b、@Autowired注解可用于构造器上,进行构造注入,但是@Resource不行,例如可以这样使用:

    @Autowiredpublic Student(Computer c,Phone p) {this.c = c;this.p = p;}

但是这样使用则不行:

    @Resource(这样使用错误,将会提示编译错误)public Student(Computer c,Phone p) {this.c = c;this.p = p;}

(3)使用Spring表达式注入

    @Value("spring表达式")private Phone p;

   @Value("#{user.name}")private String name;

(4)int或者String等基本值的注入,也可以使用@Value注解,如:

    @Value("小明")private String name;

这样就可以为name属性注入值"小明"了

转载于:https://www.cnblogs.com/zlingchao/p/9409220.html

Spring应用注解配置实现IOC相关推荐

  1. Spring使用注解配置依赖注入

    大部分情况下,使用Spring配置依赖注入时,都是使用注解来进行配置,因为注解比xml要方便和简单.不过类似于数据源对象这种配置信息容易变更的对象除外,这种对象使用xml文件来进行配置会更适合,方便于 ...

  2. Spring - Bean注解配置光速入门

    Bean注解配置光速入门 步骤一: 创建 web 项目,引入 Spring 的开发包 在 Spring 的注解的 AOP 中需要引入 spring-aop 的 jar 包 步骤二: 引入相关配置文件 ...

  3. Spring框架学习笔记03:初探Spring——利用注解配置类取代Spring配置文件

    文章目录 一.课程引入 二.利用注解配置类取代Spring配置文件 (一)打开项目[SpringDemo2021] (二)创建net.hw.spring.lesson03包 (三)移植上一讲的接口和类 ...

  4. Spring事务注解配置

    一,spring基于注解式的事务配置方法:@Transactional 1.xml配置 <?xml version="1.0" encoding="UTF-8&qu ...

  5. 带你简化理解Spring 基于注解配置的原理

    1 需求说明 自己写一个简单的 Spring 容器, 通过读取类的注解 (@Component @Controller @Service @Reponsitory),将对象注入到 IOC 容器 bea ...

  6. 【spring学习笔记】(二)Spring MVC注解配置 参数转换注解@RequestMapping@RequestParam、@PathVariable@MatrixVariable

    @TOC 介绍 在Spring MVC项目中,<\context:component-scan>配置标签还会开启@Request-Mapping.@GetMapping等映射注解功能(也就 ...

  7. 20.Spring 通过注解配置Bean

    一.在classpath中扫描组件 1.组件扫描:Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件. 2.特定组件包括: @Component:基本注解,标识了一个受Sp ...

  8. Spring @Import注解配置类方法内部调用没有注入属性值的坑

    一.场景复现 application.yaml spring:application:name: config-testprofiles:active: devconfig:config-01:nam ...

  9. 深入学习Spring框架(二)- 注解配置

    1.为什么要学习Spring的注解配置? 基于注解配置的方式也已经逐渐代替xml.所以我们必须要掌握使用注解的方式配置Spring. 关于实际的开发中到底使用xml还是注解,每家公司有着不同的使用习惯 ...

最新文章

  1. 快手百度 4.34 亿美元投资知乎;腾讯回应“push团队全部被开”;Android Q Beta 6 发布!...
  2. 【Python】创建和使用类
  3. 《SVG精髓》笔记(二)
  4. 无软件linux,无需安装即可运行或演示Linux的方法 (1)
  5. C语言学习之从键盘输入一个小于1000的正数,要求输出它的平方根(如平方根不是整数,则输出其整数部分)
  6. vue 左右滑动菜单_Vue实现左右菜单联动实现代码
  7. SAP BSP, Java Web Project,Android和微信小程序的初始页面设置
  8. oracle中prad函数_024 SQL函数
  9. TensorFlow windows之Tensorboard使用
  10. 程序员如何避免半途而废?
  11. 多款浏览器修复基于 JavaScript 的地址栏欺骗漏洞
  12. 什么是Mybatis配置解析?(源码+图文)
  13. 学习Java,真的可以月薪过万嘛?真实个人经历告诉你,记录了平时学习的内容以及学习过程中最真实的感受(一)
  14. H3CSE路由-路由策略
  15. 在低代码中平台记录sortablejs拖拽使用经验
  16. c语言电子通讯录程序设计实验报告,定稿毕业设计通讯录c语言程序设计喜欢就下吧(电子版)...
  17. react(子传父、父传子)
  18. zcmu-1957: 乌鸦坐飞机
  19. 数据结构-KMP手算next与nextval(全网最简单,包会)
  20. [Python笔记_2]循环、字符串、列表、函数、异常处理

热门文章

  1. Django模型Model的定义
  2. easypoi实现Excel导入
  3. MongoDB报表实例 -- 标签成员方案
  4. 大型分布式网站架构技术总结
  5. 异构系统数据备份解决方案
  6. git提交pull request到主项目
  7. 来学学数据分析吧(二)第一章 预测和关联数量特征
  8. python能做什么
  9. Linux服务器默认建立的LVM逻辑卷磁盘空间分配不合理,根目录不够用,如何缩减和扩展逻辑卷?...
  10. 性能测试之开源的性能监控软件