spring、springboot 注入接口(實現)

在平時的開發中可以會有定義了一些接口,有多個實現類,且每個實現類都可能被注入,在這個時候就可以將接口(的實例)注入進去

1、如果接口有一個實現類,

        則可以直接通過注入接口的形式來注入實例

    定義一個接口

package com.example.demo.interface2.practice;

/**
 * @ProjectName: demo
 * @Package: com.example.demo.interface2.practice
 * @ClassName: PracticeAutoInterface
 * @Author: yu
 * @Description: 測試注入接口
 * @Date: 2020/5/28 10:59
 * @Version: 1.0
 */
public interface PracticeAutoInterface {
    public void print();
}

    實現接口

package com.example.demo.service.practiceAuto;

import com.example.demo.interface2.practice.PracticeAutoInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @ProjectName: demo
 * @Package: com.example.demo.service
 * @ClassName: Service2AutoInterface
 * @Author: yu
 * @Description: 測試注入接口
 * @Date: 2020/5/28 11:04
 * @Version: 1.0
 */
@Slf4j
@Service
public class Service2AutoInterface implements PracticeAutoInterface {
    @Override
    public void print() {
        int aa = 1;
        log.info("成功訪問實現{}",aa);
    }
}

    然後寫一個controller來注入實現並查看接口

package com.example.demo.controller;

import com.example.demo.entity.E;
import com.example.demo.interface2.practice.PracticeAutoInterface;
import com.example.demo.practice.PracticeJava8Service;
import com.example.demo.result.methodResult.Msg;
import com.example.demo.service.TestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ProjectName: demo
 * @Package: com.example.demo.controller
 * @ClassName: Java8Controller
 * @Author: yu
 * @Description: 其他技能測試類
 * @Date: 2020/5/9 13:32
 * @Version: 1.0
 */
@RestController(value = "/other")
@AllArgsConstructor(onConstructor_ = {@Autowired})
@Api(tags = "其他技能測試類")
public class OtherController {


    @RequestMapping(value = "/autoInterface",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE )
    @ApiOperation(value = "測試注入接口",notes = "。。。")
    public void autoInterfaceMethod(){
        practiceAutoInterface.print();
    }



   
    private PracticeAutoInterface practiceAutoInterface;


}

    結果

2020-05-28 11:39:19.706  INFO 14408 --- [nio-8082-exec-8] c.e.d.s.p.Service2AutoInterface          : 成功訪問實現1

2、如果接口有多個實現類

    再次創建一個實現類

package com.example.demo.service.practiceAuto;

import com.example.demo.interface2.practice.PracticeAutoInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

/**
 * @ProjectName: demo
 * @Package: com.example.demo.service
 * @ClassName: Service2AutoInterface
 * @Author: yu
 * @Description: 測試注入接口
 * @Date: 2020/5/28 11:04
 * @Version: 1.0
 */
@Slf4j
@Service
入而不會報錯
public class Service2AutoInterface2 implements PracticeAutoInterface {
    @Override
    public void print() {
        int aa = 2;
        log.info("成功訪問實現{}",aa);
    }
}

這個時候在啓動項目的時候,會提示找到兩個相同的bean(名稱),Spring給了明確提示說有2個Bean被找到,但是隻需要一個。建議使用@Primary註解使其優先被選擇,或者使用@Qualifier指定注入一個Bean。

方式1

此時可以通過@Primary註解,來指定優先注入哪個bean

package com.example.demo.service.practiceAuto;

import com.example.demo.interface2.practice.PracticeAutoInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

/**
 * @ProjectName: demo
 * @Package: com.example.demo.service
 * @ClassName: Service2AutoInterface
 * @Author: yu
 * @Description: 測試注入接口
 * @Date: 2020/5/28 11:04
 * @Version: 1.0
 */
@Slf4j
@Service
@Primary// 如果有多個接口接口實例要被注入且沒有配置獨有的bean名稱,次註解可以是spring優先被注入而不會報錯
public class Service2AutoInterface2 implements PracticeAutoInterface {
    @Override
    public void print() {
        int aa = 2;
        log.info("成功訪問實現{}",aa);
    }
}

再次訪問結果爲:

2020-05-28 11:47:01.108  INFO 15152 --- [nio-8082-exec-1] c.e.d.s.p.Service2AutoInterface2         : 成功訪問實現2

方式2

使用@Qualifier註解,去掉Service2AutoInterface2 的@Primary註解,改寫controller的代碼:

package com.example.demo.controller;

import com.example.demo.entity.E;
import com.example.demo.interface2.practice.PracticeAutoInterface;
import com.example.demo.practice.PracticeJava8Service;
import com.example.demo.result.methodResult.Msg;
import com.example.demo.service.TestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ProjectName: demo
 * @Package: com.example.demo.controller
 * @ClassName: Java8Controller
 * @Author: yu
 * @Description: 其他技能測試類
 * @Date: 2020/5/9 13:32
 * @Version: 1.0
 */
@RestController(value = "/other")
@Api(tags = "其他技能測試類")
public class OtherController {


    @RequestMapping(value = "/autoInterface",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE )
    @ApiOperation(value = "測試注入接口",notes = "。。。")
    public void autoInterfaceMethod(){
        practiceAutoInterface.print();
    }


   @Autowired
   @Qualifier("service2AutoInterface")
    private PracticeAutoInterface practiceAutoInterface;


}

再次執行並查看結果:

2020-05-28 11:55:28.967  INFO 13436 --- [nio-8082-exec-1] c.e.d.s.p.Service2AutoInterface          : 成功訪問實現1

注意:使用@Qualifier注入指定Bean的時候,若沒有指明Bean的名稱,則其默認名稱是首字母小寫的類名。如實例中的                @Qualifier("service2AutoInterface")

當然也可以這樣

Service2AutoInterface :

@Slf4j
//@Service
@Service( "Service2AutoInterface")
public class Service2AutoInterface implements PracticeAutoInterface {
    @Override
    public void print() {
        int aa = 1;
        log.info("成功訪問實現{}",aa);
    }
}


Service2AutoInterface2 :



@Slf4j
//@Service
@Service("Service2AutoInterface2")
//@Primary// 如果有多個接口接口實例要被注入且沒有配置獨有的bean名稱,次註解可以是spring優先被注入而不會報錯
public class Service2AutoInterface2 implements PracticeAutoInterface {
    @Override
    public void print() {
        int aa = 2;
        log.info("成功訪問實現{}",aa);
    }
}


controller:

  @Autowired
    @Qualifier("Service2AutoInterface")
    private PracticeAutoInterface practiceAutoInterface;

方式3、

使用@Resource(name = "Service2AutoInterface"),--(沒有括號內內容的話,默認byName。與@Autowired幹類似的事。)

package com.example.demo.controller;

import com.example.demo.entity.E;
import com.example.demo.interface2.practice.PracticeAutoInterface;
import com.example.demo.practice.PracticeJava8Service;
import com.example.demo.result.methodResult.Msg;
import com.example.demo.service.TestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ProjectName: demo
 * @Package: com.example.demo.controller
 * @ClassName: Java8Controller
 * @Author: yu
 * @Description: 其他技能測試類
 * @Date: 2020/5/9 13:32
 * @Version: 1.0
 */
@RestController(value = "/other")
@Api(tags = "其他技能測試類")
public class OtherController {


    @RequestMapping(value = "/autoInterface",method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_UTF8_VALUE )
    @ApiOperation(value = "測試注入接口",notes = "。。。")
    public void autoInterfaceMethod(){
        practiceAutoInterface.print();
    }


     @Resource(name = "Service2AutoInterface")
    private PracticeAutoInterface practiceAutoInterface;


}

結果爲:

2020-05-28 12:02:22.207  INFO 14916 --- [nio-8082-exec-1] c.e.d.s.p.Service2AutoInterface          : 成功訪問實現1

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章