我正在尝试使用具有不同配置文件的Spring接口的不同实现。我有多个模块,在不同的模块中有代码。接口和2个实现位于一个模块中,而调用该接口的类位于另一个模块中。我的代码是这样的:
来自module1:
public class FirstService {
@Autowired
private Interface interfaceImplementation;
}来自module2:
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谢谢你的帮助。
发布于 2020-10-16 03:56:53
我能够通过添加接口的基本包来修复这个问题,这些实现位于应用程序的@ComponentScan中的单独模块中。
发布于 2020-10-15 03:13:19
在你的应用程序在你的默认配置文件上启动,所以不会有“接口”类型的bean,因为在你的代码中,只有当你的应用程序在“开发”或“测试”配置文件中启动时,spring才会创建"Interace“的实现。有许多方法可以在spring中设置配置文件,最简单的方法是将其设置为系统参数或环境变量
例如,设置为系统参数
-Dspring.profiles.active=develop或-Dspring.profiles.active=test (根据您运行的环境)
例如,设置为环境变量
linux:- export spring_profiles_active=true窗口:- set spring_profiles_active=true
https://stackoverflow.com/questions/64359573
复制相似问题