spring的注解使用,Autowired和Qualifier

2024-06-21 11:48

本文主要是介绍spring的注解使用,Autowired和Qualifier,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Spring的注解在使用时须在xml中做如下配置
<?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-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:annotation-config/></beans>

其中<context:annotation-config />的作用是在给Spring的容器注册了四个类,包括(AutowiredAnnotationBeanPostProcessor , CommonAnnotationBeanPostProcessor , PersistenceAnnotationBeanPostProcessor 和 RequiredAnnotationBeanPostProcessor),这几个类都是为了处理相应的注解。


(1)@Autowired

      此注解的作用是为了Spring在进行自动装配时调用的方法,可以注解在 对应属性上,对应属性的set方法上,和任意名字的方法上,可以注解多个,当存在多个此注解时,会多次调用注解了的方法。

例如:

package cn.mastery.service;
import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;import cn.mastery.dao.BaseDao;public class BaseService extends LifeCycle {@Autowiredprivate BaseDao baseDao;public BaseDao getBaseDao() {return baseDao;}@Autowiredpublic void setBaseDao(BaseDao baseDao) {this.baseDao = baseDao;}//调用加在此注解上的方法来动态装载,也可加在属性声明处,可不需要set和get方法@Autowiredpublic void wired(BaseDao baseDao) {this.baseDao = baseDao;}public Boolean add(Object entity){System.out.println(this.baseDao);return baseDao.add(entity);}public Boolean update(Object entity){return baseDao.update(entity);}public Boolean delete(Object entity){return baseDao.delete(entity);}public List<Object> query(){return baseDao.query();}}

(2)@Qualifier

      此注解的作用是为了Spring在进行自动装配时当配置xml时存在多个相同的bean时,Spring无法自动识别出调用的是哪个bean,配上这个注解可以让Spring的容器能够找到对应的bean之后赋值。

例如:

package cn.mastery.service;
import java.util.List;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;import cn.mastery.dao.BaseDao;public class BaseService extends LifeCycle {private BaseDao baseDao;public BaseDao getBaseDao() {return baseDao;}public void setBaseDao(BaseDao baseDao) {this.baseDao = baseDao;}//调用加在此注解上的方法来动态装载,也可加在属性声明处,可不需要set和get方法@Autowiredpublic void wired(@Qualifier("action")BaseDao baseDao) {this.baseDao = baseDao;}public Boolean add(Object entity){System.out.println(this.baseDao);return baseDao.add(entity);}public Boolean update(Object entity){return baseDao.update(entity);}public Boolean delete(Object entity){return baseDao.delete(entity);}public List<Object> query(){return baseDao.query();}}
xml配置如下:如上@Qualifier的后面名字是action,则Spring容器调用的是id为baseDao1的bean
<?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-2.5.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-2.5.xsd"><context:annotation-config></context:annotation-config><!-- 此配置的autowire说明是通过名字来匹配! 则匹配的是第一个bean --><bean id="baseDao" class="cn.mastery.dao.impl.BaseDaoImpl"><qualifier value="main"></qualifier><property name="daoId" value="8"></property><property name="daoStatus" value="good"></property></bean><!-- 此配置的autowire说明是通过名字来匹配! 则匹配的是第一个bean --><bean id="baseDao1" class="cn.mastery.dao.impl.BaseDaoImpl"><qualifier value="action"></qualifier><property name="daoId" value="2"></property><property name="daoStatus" value="bad"></property></bean><bean id="baseService" class="cn.mastery.service.BaseService"></bean><!-- more bean definitions go here --></beans>


这篇关于spring的注解使用,Autowired和Qualifier的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1081143

相关文章

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

解决SpringBoot启动报错:Failed to load property source from location 'classpath:/application.yml'

《解决SpringBoot启动报错:Failedtoloadpropertysourcefromlocationclasspath:/application.yml问题》这篇文章主要介绍... 目录在启动SpringBoot项目时报如下错误原因可能是1.yml中语法错误2.yml文件格式是GBK总结在启动S

鸿蒙中@State的原理使用详解(HarmonyOS 5)

《鸿蒙中@State的原理使用详解(HarmonyOS5)》@State是HarmonyOSArkTS框架中用于管理组件状态的核心装饰器,其核心作用是实现数据驱动UI的响应式编程模式,本文给大家介绍... 目录一、@State在鸿蒙中是做什么的?二、@Spythontate的基本原理1. 依赖关系的收集2.

Python基础语法中defaultdict的使用小结

《Python基础语法中defaultdict的使用小结》Python的defaultdict是collections模块中提供的一种特殊的字典类型,它与普通的字典(dict)有着相似的功能,本文主要... 目录示例1示例2python的defaultdict是collections模块中提供的一种特殊的字

Spring中配置ContextLoaderListener方式

《Spring中配置ContextLoaderListener方式》:本文主要介绍Spring中配置ContextLoaderListener方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录Spring中配置ContextLoaderLishttp://www.chinasem.cntene

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序