Spring 接口如何调用自己需要的实现类。

1.先定义接口

package com.example.demo;

public interface  defineClassName {
     public String test();
}
 

2.实现接口1

package com.example.demo;

import org.springframework.stereotype.Service;

@Service("A")
public class impleOne implements  defineClassName {

    @Override
    public String test() {
        
        System.out.println("测试1号");
        
        return "A";
    }
    
}

3.实现接口2

package com.example.demo;

import org.springframework.stereotype.Service;

@Service("impleTwo")
public class impleTwo implements  defineClassName {

    @Override
    public String test() {
        
        System.out.println("测试2号");
        
        return "B";
    }
    
}
 

4.使用定义的接口

Spring 注解一个接口多个实现

5.测试结果:

Spring 注解一个接口多个实现

7.总结

a.实现的位置使用注解@Service 

b.使用的时候@Autowired     @Qualifier("A")

相关文章:

  • 2021-12-19
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2023-02-26
  • 2021-09-06
  • 2021-06-16
  • 2021-11-26
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-07-04
  • 2022-12-23
  • 2022-01-16
  • 2022-12-23
相关资源
相似解决方案