gpt4 book ai didi

java - 如何在Spring中直接获取最高排序的bean而无需注入(inject)列表?

转载 作者:太空宇宙 更新时间:2023-11-04 10:14:04 25 4
gpt4 key购买 nike

共有3个类(class)U , V , W实现接口(interface)A并注释为 @Order Spring的注解具有不同的顺序值。

我现在通过注入(inject) List<A> 获得最高阶 bean然后搜索 List<A> 中的第一个元素.

有没有更直接的方法来获取最高优先级的bean,而无需注入(inject)A的整个集合?

最佳答案

考虑javax.annotation.Priority。看来这对你的情况可能有帮助。

@Service
@Priority(Ordered.HIGHEST_PRECEDENCE + 1)
class U implements A {}

@Service
@Priority(Ordered.HIGHEST_PRECEDENCE + 2)
class V implements A {}

@Service
@Priority(Ordered.HIGHEST_PRECEDENCE + 3)
class W implements A {}

interface A {}

@Service
public class Client {
@Autowired
private A a;

@PostConstruct
public void init() {
System.out.println("--------------------------" + a.getClass().getSimpleName());
}
}

“Client.a”将分配“U”实例。

默认ListableBeanFactory
    /**
* Determine the autowire candidate in the given set of beans.
* <p>Looks for {@code @Primary} and {@code @Priority} (in that order).
* @param candidates a Map of candidate names and candidate instances
* that match the required type, as returned by {@link #findAutowireCandidates}
* @param descriptor the target dependency to match against
* @return the name of the autowire candidate, or {@code null} if none found
*/
protected String determineAutowireCandidate(Map<String, Object> candidates, DependencyDescriptor descriptor) {
...
String priorityCandidate = determineHighestPriorityCandidate(candidates, requiredType);
...
}
    /**
* Determine the candidate with the highest priority in the given set of beans.
* <p>Based on {@code @javax.annotation.Priority}. As defined by the related
* {@link org.springframework.core.Ordered} interface, the lowest value has
* the highest priority.
* @param candidates a Map of candidate names and candidate instances
* (or candidate classes if not created yet) that match the required type
* @param requiredType the target dependency type to match against
* @return the name of the candidate with the highest priority,
* or {@code null} if none found
* @see #getPriority(Object)
*/
protected String determineHighestPriorityCandidate(Map<String, Object> candidates, Class<?> requiredType) {
String highestPriorityBeanName = null;
Integer highestPriority = null;
for (Map.Entry<String, Object> entry : candidates.entrySet()) {
String candidateBeanName = entry.getKey();
Object beanInstance = entry.getValue();
Integer candidatePriority = getPriority(beanInstance);
if (candidatePriority != null) {
if (highestPriorityBeanName != null) {
if (candidatePriority.equals(highestPriority)) {
throw new NoUniqueBeanDefinitionException(requiredType, candidates.size(),
"Multiple beans found with the same priority ('" + highestPriority +
"') among candidates: " + candidates.keySet());
}
else if (candidatePriority < highestPriority) {
highestPriorityBeanName = candidateBeanName;
highestPriority = candidatePriority;
}
}
else {
highestPriorityBeanName = candidateBeanName;
highestPriority = candidatePriority;
}
}
}
return highestPriorityBeanName;
}

关于java - 如何在Spring中直接获取最高排序的bean而无需注入(inject)列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51936378/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com