首页 > 解决方案 > 在配置文件之间切换以管理一个弹簧接口上的多个实现

问题描述

我正在尝试使用具有不同配置文件的 Spring 接口的不同实现。我有多个模块在不同的模块中有代码。接口和 2 个实现在一个模块中,调用接口的类在不同的模块中。我的代码是这样的:

从模块1:

public class FirstService {

    @Autowired
    private Interface interfaceImplementation;
}

从模块2:

public interface Interface {

}
@Service
@Profile("develop")
public class InterfaceImpl1 implements Interface {

}
@Service
@Profile("test")
public class InterfaceImpl2 implements Interface {

}

当我启动我的应用程序时,应用程序无法启动并出现以下错误:

Field interface in FirstService required a bean of type Interface that could not be found

谢谢您的帮助。

标签: javaspring-boot

解决方案


我能够通过将接口的基本包(位于单独模块中的实现)添加到应用程序中的 @ComponentScan 来解决此问题。


推荐阅读