How to introduce a typed beans map based on type specifier in Spring?

See the example below, I am trying to get Mapmy TypedServicebeans, but I would prefer the keys to be the enumeration values Typespecified in TypeSafeQualifier, rather than the unsafe String"serviceName".

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() {
    }
  }
}

SpringBoot Version: springBootVersion=1.5.3.RELEASE

+6
source share
1 answer

Spring offers a special approach for the treatment of this type of injection: AutowireCandidateResolver.

In your case, the code could be:

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;
    }
  }
}

First of all, we add the TypeQualifierValue annotation so that Spring learns about the qualifier with values ​​of this type.

- SpringApplication : BeanFactoryPostProcessor AutowireCandidateResolver.

: MyAutowireCandidateResolver, ContextAnnotationAutowireCandidateResolver ( , , Spring "YetAnotherAutowireCandidateResolver" ).

getSuggestedValue getSuggestedValue: , (, ) getBean... -like BeanFactory Spring AnnotationUtils.

0

Source: https://habr.com/ru/post/1684395/


All Articles