小编Cos*_*scu的帖子

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

请参阅下面的示例,我正在尝试获取Map我的TypedServicebean但我更喜欢键是Type指定的枚举值TypeSafeQualifier而不是不安全的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
  } …
Run Code Online (Sandbox Code Playgroud)

spring spring-boot

6
推荐指数
1
解决办法
183
查看次数

标签 统计

spring ×1

spring-boot ×1