スプリング、スプリングブック注入インターフェース(実現)

9180 ワード

通常の開発では、いくつかのインターフェースを定義することができます.複数の実装クラスがあり、各実装クラスが注入される可能性があります.このとき、インターフェース(の例)を注入することができます.
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は明確にヒントを与えました.@Primary注解を使って優先させるか、@Qualfier指定を使ってビーンを注入することをおすすめします.
方式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
@Qualfierで注釈して、Service 2 AutoInterface 2を取り除きます. の@Primar注解を行い、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
注意:@Qualfierを使って指定ビーンを注入する場合、ビーンの名前が指定されていない場合は、デフォルトの名前はアルファベット小文字の類名です.実例のような                @Qualfier(「service 2 AutoInterface」)
もちろんいいです.
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=「Service 2 AutoInterface」)を使います.--(括弧内の内容がないと、デフォルト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