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

标签 java spring spring-boot

共有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/

相关文章:

java - Android 资源中的图像导致内存泄漏

java - Spring中如何模拟私有(private)方法中调用的公共(public)方法的返回

java - 如何从命令行停止 spring boot 服务?

java - Spring Boot - 数据源运行时错误

java - 在 Android 上调用方法的成本

java - JAAS Realm 加载内部配置文件

javascript - 使用 jQuery 将 JSON 对象发布到 Spring 3 Controller

spring - 具有自定义权限的 Grails ACL

spring - 尽管 CORS 使用 jHipster oAuth 时出现未经授权的错误

spring-boot - OpenAPI 规范 3.0 在浏览器和服务器下拉列表中显示不同的 url