Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在配置文件之间切换以管理一个spring接口上的多个实现_Java_Spring Boot - Fatal编程技术网

Java 在配置文件之间切换以管理一个spring接口上的多个实现

Java 在配置文件之间切换以管理一个spring接口上的多个实现,java,spring-boot,Java,Spring Boot,我试图使用不同配置文件的Spring接口的不同实现。我有多个模块,在不同的模块中有代码。接口和2个实现位于一个模块中,调用接口的类位于另一个模块中。我的代码是这样的: 从模块1: public class FirstService { @Autowired private Interface interfaceImplementation; } 来自模块2: public interface Interface { } 启动应用程序时,应用程序启动失败,出现以下错误: F

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

从模块1:

public class FirstService {

    @Autowired
    private Interface interfaceImplementation;
}
来自模块2:

public interface Interface {

}
启动应用程序时,应用程序启动失败,出现以下错误:

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

感谢您的帮助。

在您的应用程序上,应用程序是在您的默认配置文件上启动的,因此不会有“Interface”类型的bean,因为在您的代码中,只有当您的应用程序在“develop”或“test”配置文件中启动时,“Interace”的实现才会由spring创建。在spring中设置配置文件的方法有很多,最简单的方法是将其设置为系统参数或环境变量

设置为系统参数的Eg

-Dspring.profiles.active=develop
-Dspring.profiles.active=test
(根据您运行的环境)

环境变量的设置

linux:
export-spring\u profiles\u active=true

windows:-
设置spring\u profiles\u active=true

我可以通过将界面的基本包、单独模块中的实现添加到应用程序中的@ComponentScan来解决此问题。

如何启动应用程序?启动应用程序时是否启用其中一个配置文件?我已在pom文件中进行了开发。我尝试通过eclipse和命令行通过以下命令启动:java-jar-Dspring.profiles.active=developmentmyapplication.jar。不管我怎么做都失败了。谢谢。我在pom中将开发配置文件设置为默认配置文件。感谢开发真正的开发如上所述开发配置文件在我的pom文件中设置为默认值。当我从eclipse启动应用程序时,我通过单击“运行配置”并选择应用程序添加了一个环境变量spring.profiles.active。
@Service
@Profile("test")
public class InterfaceImpl2 implements Interface {

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