最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用SpringBoot根據(jù)配置注入接口的不同實現(xiàn)類(代碼演示)

 更新時間:2022年06月30日 16:12:39   作者:祁畫_  
使用springboot開發(fā)時經(jīng)常用到@Autowired和@Resource進行依賴注入,但是當我們一個接口對應多個不同的實現(xiàn)類的時候如果不進行一下配置項目啟動時就會報錯,那么怎么根據(jù)不同的需求注入不同的類型呢,感興趣的朋友一起看看吧

一.引言

我們在使用springboot進行開發(fā)的時候經(jīng)常用到@Autowired@Resource進行依賴注入,但是當我們一個接口對應多個不同的實現(xiàn)類的時候如果不進行一下配置項目啟動時就會報錯,那么怎么根據(jù)不同的需求注入不同的類型就是一個值得考慮的問題,雖然@Autowired@Resource就可以實現(xiàn),但是我們也可以選擇更加靈活的@ConditionalOnProperty注解來實現(xiàn)

二.代碼演示

1.問題描述

TestController.java

package com.example.demo.controller;
 
import com.example.demo.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @ClassName TestController
 * @Author xuwei
 * @DATE 2022/6/28
 */
@RestController
@RequestMapping("test")
public class TestController {
 
    //注入需要的service
    @Autowired
    TestService testService;
 
    @RequestMapping("test")
    public void test(){
        testService.sayHello();
    }
}

 TestService.java

package com.example.demo.service;
 
/**
 * @InterfaceName TestService
 * @Author xuwei
 * @DATE 2022/6/28
 */
public interface TestService {
    /**
     * sayHello方法
     */
    void sayHello();
}

TestService實現(xiàn)類一  TestServiceImplOne.java

package com.example.demo.service.impl;
 
import com.example.demo.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
 * @ClassName TestServiceImplOne
 * @Author xuwei
 * @DATE 2022/6/28
 */
@Service
public class TestServiceImplOne implements TestService {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestServiceImplOne.class);
    /**
     * sayHello方法
     */
    @Override
    public void sayHello() {
        LOGGER.info("I am TestServiceImplOne");
    }
}

TestService實現(xiàn)類二 TestServiceImplTwo.java

package com.example.demo.service.impl;
 
import com.example.demo.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
/**
 * @ClassName TestServiceImplTwo
 * @Author xuwei
 * @DATE 2022/6/28
 */
@Service
public class TestServiceImplTwo implements TestService {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestServiceImplTwo.class);
    /**
     * sayHello方法
     */
    @Override
    public void sayHello() {
        LOGGER.info("I am TestServiceImplTwo");
    }
}

這時我們的程序啟動會報錯,大概意思就是找到了兩個實現(xiàn)類

***************************
APPLICATION FAILED TO START
***************************
Description:
Field testService in com.example.demo.controller.TestController required a single bean, but 2 were found:
    - testServiceImplOne: defined in file [/Users/xuwei/Desktop/Projects/IdeaProjects/demo/target/classes/com/example/demo/service/impl/TestServiceImplOne.class]
    - testServiceImplTwo: defined in file [/Users/xuwei/Desktop/Projects/IdeaProjects/demo/target/classes/com/example/demo/service/impl/TestServiceImplTwo.class]
Action:
Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

2.解決方案

2.1使用@Autowired的時候將接口變量名改為實現(xiàn)類的限定名

TestController.java修改為如下

package com.example.demo.controller;
 
import com.example.demo.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @ClassName TestController
 * @Author xuwei
 * @DATE 2022/6/28
 */
@RestController
@RequestMapping("test")
public class TestController {
 
    //修改變量名為實現(xiàn)類的限定名
    @Autowired
    TestService testServiceImplOne;
 
    @RequestMapping("test")
    public void test(){
        testServiceImplOne.sayHello();
    }
}

我們可以將接口的命名改為對應實現(xiàn)類的限定名,默認為類名且首字母小寫,當然我們也可以自己給接口的實現(xiàn)類配置限定名,例如@Service("serviceOne") 之后在引用時使用我們配置的限定名,這樣程序都可以自動找到實現(xiàn)類,測試結果如下:

2.2 使用@Autowired配合@Qualifier指定限定名注入實現(xiàn)類

其實這個方法的原理和上面的很相似,@Autowired會默認根據(jù)type進行注入,如果type相同會根據(jù)id進行注入,也就是我們說的限定名,我們只需要讓它找到對應限定名的類即可,上面我們通過修改接口變量名的方式來實現(xiàn),同時我們還可以配合@Qualifier注解來實現(xiàn)相同的目的

TestController.java修改為如下

package com.example.demo.controller;
 
import com.example.demo.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
/**
 * @ClassName TestController
 * @Author xuwei
 * @DATE 2022/6/28
 */
@RestController
@RequestMapping("test")
public class TestController {
 
    //配合注解指定限定名
    @Qualifier("testServiceImplTwo")
    @Autowired
    TestService testService;
 
    @RequestMapping("test")
    public void test(){
        testService.sayHello();
    }
}

當然,和上一種方法相同,我們注解中填的值是實現(xiàn)類的限定名,可以使用默認,也可以和上面一樣在使用@Service時進行配置,測試結果如下:

2.3@ConditionalOnProperty

以上兩種方法都是硬編碼方式,在我們需要進行用戶配置時很不方便,所以我們可以使用@ConditionalOnProperty注解來實現(xiàn)配置文件控制的功能

在TestController中使用@Resource注入

package com.example.demo.controller;
 
import com.example.demo.service.TestService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
import javax.annotation.Resource;
 
/**
 * @ClassName TestController
 * @Author xuwei
 * @DATE 2022/6/28
 */
@RestController
@RequestMapping("test")
public class TestController {
 
    //使用@Resource注入
    @Resource
    TestService testService;
 
    @RequestMapping("test")
    public void test(){
        testService.sayHello();
    }
}

TestServiceImplOne.java

package com.example.demo.service.impl;
 
import com.example.demo.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
 
/**
 * @ClassName TestServiceImplOne
 * @Author xuwei
 * @DATE 2022/6/28
 */
@Component
@ConditionalOnProperty(name = "serviceControl",havingValue = "serviceOne")
public class TestServiceImplOne implements TestService {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestServiceImplOne.class);
    /**
     * sayHello方法
     */
    @Override
    public void sayHello() {
        LOGGER.info("I am TestServiceImplOne");
    }
}

TestServiceImplTwo.java

package com.example.demo.service.impl;
 
import com.example.demo.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
 
/**
 * @ClassName TestServiceImplTwo
 * @Author xuwei
 * @DATE 2022/6/28
 */
@Component
@ConditionalOnProperty(name = "serviceControl",havingValue = "serviceTwo")
public class TestServiceImplTwo implements TestService {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestServiceImplTwo.class);
    /**
     * sayHello方法
     */
    @Override
    public void sayHello() {
        LOGGER.info("I am TestServiceImplTwo");
    }
}

在配置文件中配置我們使用的類

測試結果如下

 三.總結

前兩種方法都是去尋找接口的限定名,第三種方法中@ConditionalOnProperty(name = "serviceControl",havingValue = "serviceOne")注解的name屬性對應配置文件中的key值,而havingValue屬性對應的是配置文件中我們上面定義的name屬性對應的value值

到此這篇關于SpringBoot根據(jù)配置注入接口的不同實現(xiàn)類的文章就介紹到這了,更多相關SpringBoot注入接口實現(xiàn)類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論

SHOW| 嵩明县| 昭平县| 亳州市| 邢台市| 岳阳县| 昭觉县| 浦江县| 津南区| 宜黄县| 桑日县| 绩溪县| 罗田县| 博野县| 藁城市| 抚松县| 栾城县| 洛南县| 满城县| 乐昌市| 定日县| 达日县| 清徐县| 资源县| 康马县| 望城县| 土默特右旗| 闸北区| 彭阳县| 格尔木市| 万全县| 桂平市| 建阳市| 襄汾县| 天全县| 二连浩特市| 农安县| 玉屏| 西吉县| 丰宁| 永济市|