SpringBoot使用@Autowired為多實(shí)現(xiàn)的接口注入依賴
使用@Autowired為多實(shí)現(xiàn)的接口注入依賴
問題描述
現(xiàn)在有UserRepositoryImpl,需要為其注入依賴。
@Repository
public class UserRepositoryImpl implements UserRepository {
@Autowired
private JdbcTemplate jdbcTemplate;
}
在本項目中的IOC容器中,JdbcTemplate有兩個實(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。在注入依賴時,IOC容器將會按byName的方式為其匹配Bean并且注入依賴。
@Repository
public class UserRepositoryImpl implements UserRepository {
@Autowired
private JdbcTemplate primaryJdbcTemplate;
}
方法三:使用@Primay
為Bean增加@Primary的注解,在@Autowired遇到多實(shí)現(xiàn)的接口時,IOC容器會將被@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);
}
一個接口多個實(shí)現(xiàn)類的Spring注入
1. 首先, Interface1 接口有兩個實(shí)現(xiàn)類
Interface1Impl1 和 Interface1Impl2
Interface1 接口:
package com.example.service;
/**
* Created by liuzh on 2018-05-29.
* 接口1
*/
public interface Interface1 {
void fun1();
}
以下是接口的兩個實(shí)現(xiàn)類,請注意@service注解的使用方式,這里給每個實(shí)現(xiàn)類標(biāo)注了不同的名稱,方便在@Resource注入時區(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; //正常啟動
3. 使用@Resource注入,根據(jù)默認(rèn)類名區(qū)分
@Resource(name = "interface1Impl1") Interface1 interface1; //正常啟動
4. 使用@Resource注入,根據(jù)@Service指定的名稱區(qū)分
@Resource(name = "s1") Interface1 interface1; //正常啟動
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring?boot?CommandLineRunner啟動任務(wù)傳參實(shí)例詳解
在使用SpringBoot構(gòu)建項目時,我們通常有一些預(yù)先數(shù)據(jù)的加載,下面這篇文章主要給大家介紹了關(guān)于Spring?boot?CommandLineRunner啟動任務(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ì)介紹,對Springboot整合Rabbitmq相關(guān)知識感興趣的朋友一起看看吧2022-02-02
Java中Runnable與Callable接口的區(qū)別詳解
這篇文章主要為大家詳細(xì)介紹了Java中Runnable與Callable接口的區(qū)別,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Java有一定的幫助,需要的可以參考一下2023-03-03

