Spring 注解一个接口多个实现
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.使用定义的接口
5.测试结果:
7.总结
a.实现的位置使用注解@Service
b.使用的时候@Autowired @Qualifier("A")