例子:

  1. public interface IFileService {
  2. String upload(String fileName);
  3. }

注解实现

通过 @Autowired 和 @Qualifier 配合注入

  1. @Autowired
  2. @Qualifier("interface1Impl1")
  3. IFileService fileService; //正常启动

使用 @Resource 注入,根据默认类名区分

  1. @Resource(name = "interface1Impl1")
  2. IFileService fileService; //正常启动

使用 @Resource 注入,根据 @Service 指定的名称区分

  1. @Resource(name = "s1")
  2. IFileService fileService; //正常启动

配置实现

  1. @Component("SsoConfig")
  2. @ConfigurationProperties(prefix = "sso")
  3. @Data
  4. public class SsoConfig {
  5. private UploadType type = SsoType.AWS;
  6. }
  7. public enum SsoType {
  8. AWS,
  9. ALIYUN
  10. }
  1. @Configuration
  2. @ConditionalOnProperty(name="sso.type",havingValue = "AWS")
  3. @Service
  4. public class AwsFileServiceImpl implements IFileService {
  5. public String upload(String fileName) {
  6. // AWS文件上传业务逻辑
  7. return null;
  8. }
  9. }
  10. @Configuration
  11. @ConditionalOnProperty(name="sso.type",havingValue = "ALIYUN")
  12. @Service
  13. public class AliyunFileServiceImpl implements IFileService {
  14. public String upload(String fileName) {
  15. // ALIYUN 文件上传业务逻辑
  16. return null;
  17. }
  18. }
  1. sso:
  2. type: aws