我是spring框架的新手。我根据教程在做下面的代码。但这给了我错误。显示我需要一个bean,但是找到了2个。但我正在使用@Qualifier,它应该可以工作。我用过@primary,效果很好。 但仅适用于@Qualifier,按名称自动装配对我不起作用。
/pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.arman.spring.basics</groupId>
<artifactId>spring_project</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring_project</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
BubbleSortAlgorithm.java类
package com.arman.spring.basics.spring_project;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
@Qualifier("bubble")
public class BubbleSortAlgorithm implements SortAlgorithm {
public int[] sort(int[] numbers) {
return numbers;
}
}
QuickSortAlgorithm.java
package com.arman.spring.basics.spring_project;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
@Qualifier("quick")
public class QuickSortAlgorithm implements SortAlgorithm {
public int[] sort(int[] numbers) {
return numbers;
}
}
SortAlgorithm.java接口
package com.arman.spring.basics.spring_project;
public interface SortAlgorithm {
public int[] sort(int[] numbers);
}
BinarySearchImpl.java类
package com.arman.spring.basics.spring_project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class BinarySearchImpl {
@Autowired
@Qualifier("quick")
private SortAlgorithm bubbleSortAlgorithm;
public BinarySearchImpl(SortAlgorithm sortAlgorithm) {
this.bubbleSortAlgorithm = sortAlgorithm;
}
public SortAlgorithm getSortAlgorithm() {
return bubbleSortAlgorithm;
}
public void setSortAlgorithm(SortAlgorithm sortAlgorithm) {
this.bubbleSortAlgorithm = sortAlgorithm;
}
public int binarySearch(int[] numbers, int numberToSearchFor) {
int[] sortedNumbers = bubbleSortAlgorithm.sort(numbers);
System.out.println(bubbleSortAlgorithm);
// search the array
return 3;
}
}
包含主类的SpringProjectApplication.java类
package com.arman.spring.basics.spring_project;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class SpringProjectApplication {
// what are the beans?
// what are the dependency of a bean?
// where to search for them?
public static void main(String[] args) {
/*
* BinarySearchImpl binarySearch= new BinarySearchImpl(new
* QuickSortAlgorithm());
*/
ApplicationContext applicationContext = SpringApplication.run(SpringProjectApplication.class);
BinarySearchImpl binarySearchImpl = applicationContext.getBean(BinarySearchImpl.class);
BinarySearchImpl binarySearchImpl2 = applicationContext.getBean(BinarySearchImpl.class);
System.out.println(binarySearchImpl2);
System.out.println(binarySearchImpl);
int result = binarySearchImpl.binarySearch(new int[] { 12, 13, 10, 1, 3 }, 3);
System.out.println(result);
}
}
该应用程序的日志。
2019-02-06 23:25:33.265 INFO 11068 --- [ main] c.a.s.b.s.SpringProjectApplication : Starting SpringProjectApplication on DESKTOP-JDRMFHT with PID 11068 (C:\Users\Arman\git\spring-practice\spring_project\target\classes started by Arman in C:\Users\Arman\git\spring-practice\spring_project)
2019-02-06 23:25:33.275 INFO 11068 --- [ main] c.a.s.b.s.SpringProjectApplication : No active profile set, falling back to default profiles: default
2019-02-06 23:25:33.382 INFO 11068 --- [ main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@90c451: startup date [Wed Feb 06 23:25:33 BDT 2019]; root of context hierarchy
2019-02-06 23:25:34.687 WARN 11068 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'binarySearchImpl' defined in file [C:\Users\Arman\git\spring-practice\spring_project\target\classes\com\arman\spring\basics\spring_project\BinarySearchImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.arman.spring.basics.spring_project.SortAlgorithm' available: expected single matching bean but found 2: bubbleSortAlgorithm,quickSortAlgorithm
2019-02-06 23:25:34.705 INFO 11068 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-02-06 23:25:34.715 ERROR 11068 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.arman.spring.basics.spring_project.BinarySearchImpl required a single bean, but 2 were found:
- bubbleSortAlgorithm: defined in file [C:\Users\Arman\git\spring-practice\spring_project\target\classes\com\arman\spring\basics\spring_project\BubbleSortAlgorithm.class]
- quickSortAlgorithm: defined in file [C:\Users\Arman\git\spring-practice\spring_project\target\classes\com\arman\spring\basics\spring_project\QuickSortAlgorithm.class]
操作:
考虑将其中一个bean标记为@Primary,更新使用者以接受多个bean,或使用@Qualifier标识应消耗的bean
答案 0 :(得分:0)
您使用@Qualifier的方式错误。直接在@Component中设置bean名称:
@Component("bubble")
public class BubbleSortAlgorithm implements SortAlgorithm {
public int[] sort(int[] numbers) {
return numbers;
}
}
@Component("quick")
public class QuickSortAlgorithm implements SortAlgorithm {
public int[] sort(int[] numbers) {
return numbers;
}
}
是的,您是对的。构造函数的问题。春天还不清楚您要自动接线。使用构造函数自动布线具有优先权。
因此BinarySearchImpl应该如下所示:
private SortAlgorithm bubbleSortAlgorithm;
public Test(@Qualifier("quick") SortAlgorithm sortAlgorithm) {
this.bubbleSortAlgorithm = sortAlgorithm;
}
或没有构造函数。
@Autowired
@Qualifier("quick")
private SortAlgorithm bubbleSortAlgorithm;