Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.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 通过构造函数参数2表示的未满足的依赖项_Java_Spring_Spring Boot - Fatal编程技术网

Java 通过构造函数参数2表示的未满足的依赖项

Java 通过构造函数参数2表示的未满足的依赖项,java,spring,spring-boot,Java,Spring,Spring Boot,我将spring从2.1.1版升级到2.2.0版。 从那时起,我在启动应用程序时面临以下错误: 原因:org.springframework.beans.factory.NoniqueBeandDefinitionException:没有可用的“ParentService”类型的合格bean:需要单个匹配bean,但找到2:MasterService、SlaveService ParentService是一个接口: public interface ParentService{ .. } 万事

我将spring从2.1.1版升级到2.2.0版。 从那时起,我在启动应用程序时面临以下错误: 原因:org.springframework.beans.factory.NoniqueBeandDefinitionException:没有可用的“ParentService”类型的合格bean:需要单个匹配bean,但找到2:MasterService、SlaveService

ParentService是一个接口:

public interface ParentService{
..
}
万事达服务:

@Service
@MasterProfile
public class MasterService implements ParentService{
.....
}
@Service
@SlaveProfile
public class SlaveService implements ParentService{
.....
}
奴隶服务:

@Service
@MasterProfile
public class MasterService implements ParentService{
.....
}
@Service
@SlaveProfile
public class SlaveService implements ParentService{
.....
}
主配置文件注释:

@Profile("MASTER")
public @interface MasterProfile {

}
从属配置文件:

@Profile("SLAVE")
public @interface SlaveProfile{

}
我正在向我的应用程序传递带有以下标志的配置文件:

-Dspring.profiles.include=MASTER

根据,他们做了一些更改,并且在maven中默认启用了fork。因此,传递参数的唯一方法是使用参数-Dspring-boot.run.jvmArguments。我使用了-Dspring boot.run.jvmArguments=-Dspring.profiles.include=MASTER,但仍然失败。

将配置文件作为参数传递取决于您如何运行应用程序。小心,你提到的文档是指maven spring引导插件

使用maven插件:mvn spring boot:run-dsspring boot.run.jvmArguments=-dsspring.profiles.include=MASTER 经典java应用程序:java-Dspring.profiles.include=MASTER-jar./myapp.jar 在两个cmd行中,如果用分隔符分隔,则可以传递多个参数,。请参阅文档:

升级后,您现在必须定义自定义配置文件注释,如下所示:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME) // Only this one is really needed
@Profile("SLAVE")
public @interface SlaveProfile {
}
解释: 在java中,注释有一个RetentionPolicy,它类似于作用域。请参见:。 如果没有任何RetentionPolicy集,默认行为是JVM不可见的注释,即在运行时

当您想要运行应用程序时,首先要编译它,这意味着要将.java文件转换为.class文件。您的类只是一组字节码,将人类可读的文件转换为计算机语言

然后,当Spring加载ApplicationContext时,它在引擎盖下所做的其他事情之一就是读取您的.class文件。在此过程中,请参见类名:org.springframework.asm.ClassReader Spring加载您声明的注释。如上所述,在运行时,您将得到两种注释:

InvisibleAnnotation:@RetentionRetentionPolicy.COMPILE VisibleAnnotation:@RetentionRetentionPolicy.RUNTIME 要总结并理解它以前工作的原因:

Spring boot 2.1.0使用Spring-core-5.1.2,它在运行时解释可见和不可见的注释,解释@SlaveProfile和@MasterProfile为何具有预期的行为。 Spring boot 2.2.0使用Spring-core-5.2.0,它在运行时只解释可见的注释,解释了为什么@SlaveProfile和@MasterProfile没有预期的行为。 假设Spring默默地修复了一个bug,该bug在不应该读取隐形注释的时候读取该注释,但没有提及它

希望有帮助

添加@Profile不会阻止bean被实例化。这导致了异常。将@Primary添加到应用程序不应默认使用的任何bean中


例如,将@Primary添加到MasterProfile bean中

但是我有@Profile注释,它可以帮助spring确定自动编写哪个bean。正如我提到的,在版本2.1.1中它起作用了。它们是我自己的注释,只是上面带有@profile注释的简单接口,与MasterProfile相同,只是名称不同。将其添加到主post。以下配置文件处于活动状态:test,native。这意味着它没有获取profilemaster或slave。与配置文件传递给Spring的方式有关,但如果我想激活多个配置文件,我会像你在第二个示例中那样运行应用程序。不过,它似乎没有通过参数。请注意,只有在我更改pom.xml中的springboot版本后才会发生这种情况。这是因为他的升级。请提供准确的命令行。运行应用程序时,您看到了什么?您是否看到以下内容:以下配置文件处于活动状态:主机乞讨时的主配置文件?我看到此消息,但在其他配置文件测试中,我看不到该行中的主配置文件。我不能共享整个命令,但我使用的是我在评论中提到的参数。它一定与spring版本的升级有关,所以我需要用另一个命令传递配置文件,并使用注释@retentionpolicy.RUNTIME?或者仅仅注释就足够了?问题是我没有在msg中看到配置文件。以下配置文件处于活动状态…如果您只添加@retentionpolicy.RUNTIME,那么您的配置文件将被激活。您说您使用的是第二个示例,即java-D…,但发行说明中提到的由Maven插件运行的Spring引导应用程序现在默认为分叉。你显然不是。