SpringBoot使用@Autowired為多實(shí)現(xiàn)的接口注入依賴
使用@Autowired為多實(shí)現(xiàn)的接口注入依賴
問題描述
現(xiàn)在有UserRepositoryImpl,需要為其注入依賴。
@Repository
public class UserRepositoryImpl implements UserRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
}
在本項(xiàng)目中的IOC容器中,JdbcTemplate有兩個(gè)實(shí)現(xiàn)。
@Bean(name="primaryJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate (
@Qualifier("primaryDataSource") DataSource dataSource ) {
return new JdbcTemplate(dataSource);
}
@Bean(name="secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("secondaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
方法一:使用@Qualifier限定
在類UserRepositoryImpl中進(jìn)行修改,通過@Qualifier指定注入依賴的實(shí)現(xiàn)。
@Repository
public class UserRepositoryImpl implements UserRepository {
@Autowired
@Qualifier("primaryJdbcTemplate")
private JdbcTemplate jdbcTemplate;
}
方法二:利用@Autowired可以byName匹配Bean的特性
將UserRepositoryImpl中將待注入的成員變量的名稱修改為IOC容器中Bean的id。在注入依賴時(shí),IOC容器將會(huì)按byName的方式為其匹配Bean并且注入依賴。
@Repository
public class UserRepositoryImpl implements UserRepository {
@Autowired
private JdbcTemplate primaryJdbcTemplate;
}
方法三:使用@Primay
為Bean增加@Primary的注解,在@Autowired遇到多實(shí)現(xiàn)的接口時(shí),IOC容器會(huì)將被@Primary標(biāo)注的Bean注入。
@Primary
@Bean(name="primaryJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate (
@Qualifier("primaryDataSource") DataSource dataSource ) {
return new JdbcTemplate(dataSource);
}
@Bean(name="secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("secondaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
一個(gè)接口多個(gè)實(shí)現(xiàn)類的Spring注入
1. 首先, Interface1 接口有兩個(gè)實(shí)現(xiàn)類
Interface1Impl1 和 Interface1Impl2
Interface1 接口:
package com.example.service;
/**
* Created by liuzh on 2018-05-29.
* 接口1
*/
public interface Interface1 {
void fun1();
}
以下是接口的兩個(gè)實(shí)現(xiàn)類,請(qǐng)注意@service注解的使用方式,這里給每個(gè)實(shí)現(xiàn)類標(biāo)注了不同的名稱,方便在@Resource注入時(shí)區(qū)別注入
Interface1 接口實(shí)現(xiàn)類1:
@Service("s1")
public class Interface1Impl1 implements Interface1 {
@Override
public void fun1() {
System.out.println("接口1實(shí)現(xiàn)類 ...");
}
public void fun2(){
System.out.println("接口1實(shí)現(xiàn)類1 fun2 ...");
}
}
Interface1 接口實(shí)現(xiàn)類2:
@Service("s2")
public class Interface1Impl2 implements Interface1 {
@Override
public void fun1() {
System.out.println("接口1實(shí)現(xiàn)類 ...");
}
public void fun2(){
System.out.println("接口1實(shí)現(xiàn)類2 fun2 ...");
}
}
2. 通過 @Autowired 和 @Qualifier 配合注入
@Autowired
@Qualifier("interface1Impl1")
Interface1 interface1; //正常啟動(dòng)
3. 使用@Resource注入,根據(jù)默認(rèn)類名區(qū)分
@Resource(name = "interface1Impl1") Interface1 interface1; //正常啟動(dòng)
4. 使用@Resource注入,根據(jù)@Service指定的名稱區(qū)分
@Resource(name = "s1") Interface1 interface1; //正常啟動(dòng)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- SpringBoot頂層接口實(shí)現(xiàn)類注入項(xiàng)目的方法示例
- SpringBoot通過Filter實(shí)現(xiàn)整個(gè)項(xiàng)目接口的SQL注入攔截詳解
- Springboot根據(jù)配置文件動(dòng)態(tài)注入接口實(shí)現(xiàn)類詳解
- 使用SpringBoot根據(jù)配置注入接口的不同實(shí)現(xiàn)類(代碼演示)
- springboot接口多實(shí)現(xiàn)類選擇性注入解決方案
- 使用Springboot根據(jù)配置文件動(dòng)態(tài)注入接口實(shí)現(xiàn)類
- SpringBoot中多個(gè)實(shí)現(xiàn)的接口正確注入的六種方式
相關(guān)文章
Spring?boot?CommandLineRunner啟動(dòng)任務(wù)傳參實(shí)例詳解
在使用SpringBoot構(gòu)建項(xiàng)目時(shí),我們通常有一些預(yù)先數(shù)據(jù)的加載,下面這篇文章主要給大家介紹了關(guān)于Spring?boot?CommandLineRunner啟動(dòng)任務(wù)傳參的相關(guān)資料,需要的朋友可以參考下2022-06-06
Springboot整合Rabbitmq之Confirm和Return機(jī)制
這篇文章主要介紹了Springboot整合Rabbitmq之Confirm和Return詳解,本篇重點(diǎn)進(jìn)行Confirm?機(jī)制和Return?機(jī)制的實(shí)現(xiàn)和說明,通過實(shí)例代碼相結(jié)合給大家詳細(xì)介紹,對(duì)Springboot整合Rabbitmq相關(guān)知識(shí)感興趣的朋友一起看看吧2022-02-02
Java中Runnable與Callable接口的區(qū)別詳解
這篇文章主要為大家詳細(xì)介紹了Java中Runnable與Callable接口的區(qū)別,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2023-03-03

