当前位置: 首页 > 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/5405.html

相关文章:

  • java 网站开发开源抖音代运营公司
  • eclipse做购物网站百度账号登录
  • 昆明网站建设公司哪家好台州seo排名扣费
  • 别人帮做的网站怎么修改病句收录网
  • wordpress网页北京seo公司wyhseo
  • seo网站运营网站怎么推广
  • 网址地址seo分析师
  • 惠州网站建设制作郑州模板网站建设
  • 网站域名需icp备案市场推广方案范文
  • 张店做网站正规的培训学校
  • wordpress菜单目录层叠重庆自动seo
  • 泉州网站建站模板晚上偷偷看b站软件推荐
  • 合肥企业网站seo如何优化图片
  • 门户网站欣赏如何制作百度网页
  • 海纳企业网站建设网站设计用什么软件
  • 太原市给企业做网站游戏推广公司靠谱吗
  • 开发者模式伤手机吗而的跟地seo排名点击软件
  • sns社交网站开发新闻发布平台
  • 桂平市住房和城乡建设局门户网站品牌推广外包公司
  • 哪个网站可以做初一政治试卷seo快速排名源码
  • 毕设做网站可以用模板吗地推网app推广平台
  • 删除后台wordpress新闻怎样做关键词排名优化
  • 微信公众号做留言网站模板网站免费
  • 网页设计网站建设电商大数据查询平台
  • 扬州建设工程招标信息网长沙网站seo诊断
  • 虎门做外贸网站交换友情链接
  • 网站被攻击了怎么处理怎么建设自己的网站
  • 茶叶网站建设网页设计制作如何刷app推广次数
  • 宿迁房产网官网合肥网络公司seo
  • 网站源码获取在线百度网站官网入口