当前位置: 首页 > news >正文

网站建设与开发的收获与体会在线看网址不收费不登录

网站建设与开发的收获与体会,在线看网址不收费不登录,网站整站开发项目亮点,松江车墩网站建设文章目录1. Autowired和Resource的区别2. 一个接口多个实现类的处理2.1 注入时候报错情况2.2 使用Primary注解处理2.3 使用Qualifer注解处理2.4 根据业务情况动态的决定注入哪个serviceImpl1. Autowired和Resource的区别 Aurowired是根据type来匹配;Resource可以根…

文章目录

  • 1. @Autowired和@Resource的区别
  • 2. 一个接口多个实现类的处理
    • 2.1 注入时候报错情况
    • 2.2 使用@Primary注解处理
    • 2.3 使用@Qualifer注解处理
    • 2.4 根据业务情况动态的决定注入哪个serviceImpl

1. @Autowired和@Resource的区别

  1. @Aurowired是根据type来匹配;@Resource可以根据name和type来匹配,默认是name匹配
  2. @Aurowired是Spring定义的注解,@Resource是JSR250规范里面定义的注解,而Spring对JSR250规范提供了支持。
  3. @Autowired如果需要支持name匹配,就需要配合@Primary或者@Qualifier注解使用来实现

2. 一个接口多个实现类的处理

public interface FileService {void getFile();
}
@Slf4j
@Service
public class FileServiceAImpl implements FileService {@Overridepublic void getFile() {log.info("调用fileServiceAImpl...");}
}
@Slf4j
@Service
public class FileServiceBImpl implements FileService {@Overridepublic void getFile() {log.info("调用fileServiceBImpl...");}
}

2.1 注入时候报错情况

在这里插入图片描述

2.2 使用@Primary注解处理

@Primary表示主要的bean,当存在多个相同类型的Bean的时候,优先使用声明了@Primary的Bean

FileServiceAImpl 类添加@Primary注解:

@Slf4j
@Service
@Primary
public class FileServiceAImpl implements FileService {@Overridepublic void getFile() {log.info("调用fileServiceAImpl...");}
}

在这里插入图片描述

2.3 使用@Qualifer注解处理

@Qualifer的作用类似于条件筛选,它可以根据Bean的名字找到需要装备的目标Bean

@Slf4j
@Service("fileServiceAImpl")
public class FileServiceAImpl implements FileService {@Overridepublic void getFile() {log.info("调用fileServiceAImpl...");}
}
@Slf4j
@Service("fileServiceBImpl")
public class FileServiceBImpl implements FileService {@Overridepublic void getFile() {log.info("调用fileServiceBImpl...");}
}
@Slf4j
@SpringBootTest
class FileServiceTest {@Autowired@Qualifier("fileServiceBImpl")private FileService fileService;@Testvoid getFile() {fileService.getFile();}}

在这里插入图片描述

2.4 根据业务情况动态的决定注入哪个serviceImpl

使用 ApplicationContext的getBean()方法动态指定需要用到的bean

@Slf4j
@SpringBootTest
class FileServiceTest {@Autowiredprivate ApplicationContext context;@Testvoid getFile() {FileService fileService = getFileService(0);fileService.getFile();}/*** 根据业务参数生成需要的bean* @param num 具体的业务参数* @return*/private FileService getFileService(int num){FileService fileService;if(num==1){fileService = context.getBean("fileServiceBImpl",FileService.class);}else {fileService = context.getBean("fileServiceAImpl",FileService.class);}return fileService;}
}

在这里插入图片描述

http://www.dinnco.com/news/77923.html

相关文章:

  • 网站互联网设计图风格广州网站优化外包
  • 福田社会建设促进局网站百度下载软件
  • 网站公司做网站seo技术最新黑帽
  • 免费ppt模板下载大全网站最近一周国内热点新闻
  • 网站建设7make安卓内核级优化神器
  • wordpress 侧边栏菜单上海seo优化服务公司
  • 外外贸网站推广方案网站策划书案例
  • 江门免费模板建站做公司网站需要多少钱
  • 小说网站开发猪八戒抚顺优化seo
  • 建筑公司网站能显示二级建造师报名吗网络推广工作内容
  • wordpress如何设置中文深圳网站建设专业乐云seo
  • 设计制作小车二教学视频seo草根博客
  • 最低网网站多少钱中国培训网官网
  • 网站建设标语培训体系包括四大体系
  • 网站一般多长西安做网站公司
  • 二手房在哪个网站做合同海外营销公司
  • 做可视化的网站合肥seo建站
  • 网站建设加盟合作2024年重大新闻简短
  • 手游排行榜北京seo关键词优化外包
  • 开发网站的流程网络营销策划方案案例
  • 为什么要创建网站微信软文推广怎么做
  • 网站内容的实现方式阻断艾滋病的药有哪些
  • 字体图标网站如何在百度提交网站
  • 公司网站开发建设什么会计科目seo关键词是什么
  • 网站空间类型深圳网站建设微信开发
  • 东莞建设网办事指南seo网站排名
  • 网站个人信息页面布局软文推广有哪些平台
  • 邯郸企业做网站凡科网站建设
  • 做网站教程下载提高基层治理效能
  • 免费做简单网站今日新闻头条