使用注解装配bean
使用@Autowired注解
从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性。
Spring默认禁用注解装配,最简单的启用方式是使用Spring的context命名空间配置中的context:annotation-config元素,如下所示:

<?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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd"><context:annotation-config /><!-- bean declarations here --></beans>

继续上一节的例子,在xml文件中定义两个bean:falchion bean和guanyu bean,为了实现@Autowired自动装配,在GuanYu类中的setWeapon()方法前添加了@Autowired注解,如下:
GuanYu.java:

package com.moonlit.myspring;
import org.springframework.beans.factory.annotation.Autowired;
public class GuanYu implements Hero {    @Autowired   private Weapon weapon;    public void perform() {System.out.println("Guan Yu pick up his weapon.");weapon.attack();}    public Weapon getWeapon() {        return weapon;}public void setWeapon(Weapon weapon) {        this.weapon = weapon;}
}

通过基于注解的方式,可以不用在xml文件中为guanyu bean添加autowire属性了。
spring-idol内部的代码:

<context:annotation-config /><bean id="falchion" class="com.moonlit.myspring.Falchion"  /><bean id="guanyu" class="com.moonlit.myspring.GuanYu" />

@Autowired注解存在一个限制:
匹配多个Bean
限定歧义性的依赖
有可能存在多个bean满足装配条件,比如,这里,falchion bean和halberd bean都满足装配到guanyu bean的weapon属性中的条件。此时如果只是用@Autowired注解的话就会出问题,才@Autowired注解下添加@Qualifier注解如下:

    @Autowired@Qualifier("falchion")    private Weapon weapon;

就会将falchion bean装入到weapon中。
如上所示,@Qualifier注解将尝试注入ID为falchion的Bean。
除了通过Bean的ID来限定,也可以给Bean添加一个qualifier属性,通过这个qualifier属性来获得限定,如:
给halberd bean添加一个qualifier,值为"weaponOfGuanYu":

然后对GuanYu类weapon类的注解如下:
@Autowired
@Qualifier(“weaponOfGuanYu”)
private Weapon weapon;

输出如下:
Guan Yu pick up his weapon.
halberd is attacking!!!

可以看出,@qualifier降低了@Autowired的匹配范围,最终筛选得到了halberd bean装入weapon属性。
这里的元素限定了方天画戟(halberd)Bean是关羽使用的武器(weaponOgGuanYu)。除了可以在XML中指定qualifier,还可以使用Qualifier类来标注Halberd类:

package com.moonlit.myspring;import org.springframework.beans.factory.annotation.Qualifier;@Qualifier("weaponOfGuanYu")
public class Halberd implements Weapon {
public void attack() {System.out.println("halberd is attacking!!!");}
}

程序运行将得到相同的结果。
即使context:annotation-config有助于完全消除Spring配置文件中的元素,但是还是不能完全消除,仍然需要使用元素显示定义Bean。因此context:component-scan元素出现了,它除了完成context:annotation-config一样的工作,还允许Spring自动检测Bean和定义Bean。这就意味着不使用元素,Spring应用中的大多数(或者所有)Bean都能够自动实现定义和装配。
自动检测
为了配置Spring自动检测,需要使用context:component-scan元素来代替context:annotation-config元素:

<?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"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.xsd"><context:component-scan base-package="com.moonlit.myspring"></context:component-scan></beans>

context:component-scan元素会扫描指定的包以及所有子包,并查找出能够自动注册为Spring Bean的类。base-package属性标示了context:component-scan元素所扫描的包。
为自动检测标注Bean
默认情况下,context:component-scan查找使用构造型(stereotype)注解所标注的类,这些特殊的注解如下:
类型 说明
@component 通用的构造型注解,标示该类为Spring 组件。
@Controller 标识将该类定义为Spring MVC controller。
@Repository 标识将该类定义为数据仓库(例如:Dao层)。
@Service 标识将该类定义为服务(例如:Service层)。

效果相同,都是告诉Spring框架在启动时就初始化对象,但是语义不同

 @component("guanyu")ApplicationContext applicationContext= new ClassPathXmlApplicationContext("spring.xml");Hero hero = (Hero)applicationContext.getBean("guanyu");hero.perform();

Spring框架最终注解标签注入方法相关推荐

  1. spring : springmvc常用注解标签详解(转)

    新的项目,新的学习,好久没用这些注解了,同时在学习使用shiro ,lucene 等等.在网上找了些博文,感谢作者的总结和分享. 欢迎交流,言归正传: 1.@Controller 在SpringMVC ...

  2. Spring框架中XML配置文件注入集合(数组、LIST、MAP、SET)属性

    Spring框架中XML配置文件注入集合属性 前言 创建测试类与属性 配置XML配置文件 建立调用类 调用结果 前言 某些类的属性是可能是集合,包括:数组.LIST.MAP.SET等集合,在Sprin ...

  3. Spring框架@PostConstruct注解详解

    文章目录 前言 业务背景 通过依赖查找实现 `@PostConstruct`注解实现 @PostConstruct注解原理 `@PostConstruct`注解 `@PostConstruct`注解源 ...

  4. idea提示未配置 Spring Boot 配置注解处理器解决方法

    未配置 Spring Boot 配置注解处理器 解决方法: 在pom.xml里添加依赖 <dependency><groupId>org.springframework.boo ...

  5. spring框架 c p标签的区别_Spring学习初体验

    训练大纲(第057天)60 大家如果想快速有效的学习,思想核心是"以建立知识体系为核心",具体方法是"守破离".确保老师课堂上做的操作,反复练习直到熟练. 第1 ...

  6. Java普通类获取Spring框架Bean 的五种方法

    方法一:在初始化时保存ApplicationContext对象 代码: ApplicationContext ac = new FileSystemXmlApplicationContex(" ...

  7. Spring框架----自动按照类型注入的Autowired注解

    当执行如下代码时(配置文件中未注入) private IAccountDao accountDao; public void saveAccount() {accountDao.saveAccount ...

  8. Spring中继承配置的注入方法

    (1)两个java类.一个父类一个字类 package com.lc.inherit;/** 这里是父类*/ public class Student {protected String name;p ...

  9. java spring框架 注解_spring框架之注解的使用

    原标题:spring框架之注解的使用 今天是刘小爱自学Java的第122天. 感谢你的观看,谢谢你. 学习内容安排如下: Spring注解的使用. JavaWeb项目的搭建. Spring的Web集成 ...

最新文章

  1. PointPillar:利用伪图像高效实现3D目标检测
  2. [Eclipse]GEF入门系列(六、添加菜单和工具条)
  3. pve远程连接 spcie_proxmox折腾 篇一:解决j3455直通iommu分组问题,PVE内核编译教程...
  4. 设置socket.Receive()的等待时延
  5. another mysql daemon_MySQL错误Another MySQL daemon already running with the same unix socket.
  6. struts2.2 json配置
  7. L1-028. 判断素数-PAT团体程序设计天梯赛GPLT
  8. 3个框框带你理解EventLoop
  9. coin3D中导入机器人模型
  10. AI 杀疯了,NovelAI开源教程
  11. 2021年PMP考试模拟题11(含答案解析)
  12. 战地一自定义服务器怎么搜索,战地1怎么快速加入服务器?多种加入方法一览...
  13. python弹窗炸弹
  14. 专业PLC数据采集软件PLC-Recorder通过ADS通讯进行倍福TwinCAT2和TwubCAT3数据采集的介绍
  15. webpack安装问题(已解决)
  16. 毕业设计必备案例:Python开发桌面程序——各种版本学生信息管理系统
  17. Python逐行读取tsv文件
  18. 2023税务师DA考点抢先学
  19. 封装的PKPM BimView的方法
  20. 调用百度地图API出现 error inflating class com.baidu.mapapi.map.mapview

热门文章

  1. 大数据如何助力农业发展
  2. 大数据技术如何实现核心价值
  3. 数据分析数据挖掘(三)
  4. python将list转为数组_python如何将list中的字符转为数字
  5. python赋值标志_Python中的赋值、引用和深浅拷贝
  6. 字节跳动正式offer之前是哪一个环节_不是做梦!她在3天前拿到腾讯、百度、字节跳动的offer!...
  7. 计算机一级在线练习,计算机一级练习系统
  8. Shell脚本编程之(四)善用判断式
  9. Docker部署项目的步骤,按步骤一步一步来,一切都会成功
  10. Oracle BIEE 链接oracle 数据库的问题,报:Check if 'Oracle OCI 10G' database client is installed