如何在Spring中基于类型安全限定符注入bean的类型映射?

时间:2017-08-24 15:20:20

标签: spring spring-boot

请参阅下面的示例,我尝试获取MapTypedService bean,但我更希望密钥是Type枚举值{ {1}}代替不安全的TypeSafeQualifier" serviceName"。

String

SpringBoot版本:package org.test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Service; import java.lang.annotation.Retention; import java.lang.annotation.Target; import java.util.Map; import static org.test.Application.Type.ONE; import static org.test.Application.Type.TWO; import static java.lang.annotation.ElementType.CONSTRUCTOR; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; @SpringBootApplication public class Application { @Autowired Map<String, TypedService> works; @Autowired Map<Type, TypedService> fails; public static void main(String [] args) { SpringApplication.run(Application.class, args); } public enum Type { ONE, TWO } @Target({TYPE, METHOD, FIELD, CONSTRUCTOR}) @Retention(RUNTIME) @Qualifier public @interface TypeSafeQualifier { Type value(); } public interface TypedService { void startSignup(); void activate(); } @Service @TypeSafeQualifier(ONE) public class TypeOneService implements TypedService { @Override public void startSignup() { } @Override public void activate() { } } @Service @TypeSafeQualifier(TWO) public class TypeTwoService implements TypedService { @Override public void startSignup() { } @Override public void activate() { } } }

1 个答案:

答案 0 :(得分:0)

Spring提供了一种特殊的方法来处理这种类型的注入:AutowireCandidateResolver

在您的情况下,代码可能是:

package org.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.DependencyDescriptor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.core.ResolvableType;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.util.LinkedHashMap;
import java.util.Map;

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@SpringBootApplication
public class Application {

  @Autowired
  Map<String, TypedService> works;

  @Autowired
  Map<Type, TypedService> fails;

  @PostConstruct
  private void init() {
    System.out.println(fails);
  }

  public static void main(String[] args) {
    final SpringApplication application = new SpringApplication(Application.class);
    application.addInitializers(context -> {
      context.addBeanFactoryPostProcessor(beanFactory -> {
        final DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) beanFactory;
        dlbf.setAutowireCandidateResolver(new MyAutowireCandidateResolver(dlbf));
      });
    });
    application.run(args);
  }

  @QualifierValue(TypeSafeQualifier.class)
  public enum Type {
    ONE,
    TWO
  }

  @Target({TYPE, METHOD, FIELD, CONSTRUCTOR})
  @Retention(RUNTIME)
  @Qualifier
  public @interface TypeSafeQualifier {
    Type value();
  }

  public interface TypedService {
    void startSignup();
    void activate();
  }

  @Service
  @TypeSafeQualifier(Type.ONE)
  public class TypeOneService implements TypedService {

    @Override
    public void startSignup() {
    }

    @Override
    public void activate() {
    }
  }

  @Target({TYPE})
  @Retention(RUNTIME)
  public @interface QualifierValue {
    Class<? extends Annotation> value();
  }

  @Service
  @TypeSafeQualifier(Type.TWO)
  public class TypeTwoService implements TypedService {

    @Override
    public void startSignup() {
    }

    @Override
    public void activate() {
    }
  }

  private static class MyAutowireCandidateResolver extends ContextAnnotationAutowireCandidateResolver {

    private final DefaultListableBeanFactory beanFactory;

    private MyAutowireCandidateResolver(DefaultListableBeanFactory beanFactory) {
      this.beanFactory = beanFactory;
    }

    @Override
    public Object getSuggestedValue(DependencyDescriptor descriptor) {
      final Object result = super.getSuggestedValue(descriptor);
      if (result != null) {
        return result;
      }

      if (descriptor.getDependencyType() != Map.class) {
        return null;
      }

      final ResolvableType dependencyGenericType = descriptor.getResolvableType().asMap();
      final ResolvableType[] typeParams = dependencyGenericType.getGenerics();

      final QualifierValue qualifierValue = typeParams[0].getRawClass().getAnnotation(QualifierValue.class);
      if (qualifierValue == null) {
        return null;
      }

      final String[] candidateBeanNames = beanFactory.getBeanNamesForType(typeParams[1]);
      final LinkedHashMap<Object, Object> injectedMap = new LinkedHashMap<>(candidateBeanNames.length);

      for (final String candidateBeanName : candidateBeanNames) {
        final Annotation annotation = beanFactory.findAnnotationOnBean(candidateBeanName, qualifierValue.value());

        if (annotation == null) {
          continue;
        }

        final Map<String, Object> annotationAttributes = AnnotationUtils.getAnnotationAttributes(annotation, false);
        final Object value = annotationAttributes.get("value");

        if (value == null || value.getClass() != typeParams[0].getRawClass()) {
          continue;
        }

        injectedMap.put(value, beanFactory.getBean(candidateBeanName));
      }

      return injectedMap;
    }
  }
}

首先,我们添加TypeQualifierValue批注以使Spring知道具有给定类型的值的限定符。

第二个方法是在main方法中自定义SpringApplication:我们使用BeanFactoryPostProcessor设置自定义的AutowireCandidateResolver。

最后一步:我们编写MyAutowireCandidateResolver扩展了ContextAnnotationAutowireCandidateResolver(适用于委托而不是继承,因为甚至有一天Spring可以默认迁移到“ YetAnotherAutowireCandidateResolver”,所以效果甚至更好)。

这里的关键部分是覆盖的getSuggestedValue方法:在这里,我们可以考虑依赖项的通用类型(字段,方法参数)并通过应用一些类似getBean...的方法来定制注入逻辑带有Spring AnnotationUtils类的魔力的BeanFactory。