SpringBoot uses @Autowired to inject implementation classes into List or Map collections

Preface

I recently saw the RuoYi-Vue-Plustranslation Translationmodule configuration class for the translation function TranslationConfig. One of the writing methods that injects the translation interface implementation class makes me feel very novel. However, this writing method has supported injection and TranslationInterfaceafter Spring version 3.0 . I usually don’t notice this. Therefore, record this writing method.ListMap

previous approach

Previously, definitions were generally 策略模式+工厂模式combined Spring上下文的Aware回调to obtain them . Here are some methods 指定类型Bean对象to obtain specified types :Bean

  • ApplicationContextMethod 1: Save the object during initialization
  • Method 2: Get the object through the Springprovided classutilsApplicationContext
  • Method 3: Inherit from abstract classApplicationObjectSupport
  • Method 4: Inherit from abstract classWebApplicationObjectSupport
  • Method 5: Implement the interfaceApplicationContextAware
  • Method 6: Through Springthe providedContextLoader

Translation module configuration class

TranslationConfigThis is where the translation configuration is initialized

package com.ruoyi.framework.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.annotation.TranslationType;
import com.ruoyi.common.translation.TranslationInterface;
import com.ruoyi.common.translation.handler.TranslationBeanSerializerModifier;
import com.ruoyi.common.translation.handler.TranslationHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 翻译模块配置类
 *
 * @author Lion Li
 */
@Slf4j
@Configuration
public class TranslationConfig {
    
    

    @Autowired
    private List<TranslationInterface<?>> list;

    @Autowired
    private ObjectMapper objectMapper;

    @PostConstruct
    public void init() {
    
    
        Map<String, TranslationInterface<?>> map = new HashMap<>(list.size());
        for (TranslationInterface<?> trans : list) {
    
    
            if (trans.getClass().isAnnotationPresent(TranslationType.class)) {
    
    
                TranslationType annotation = trans.getClass().getAnnotation(TranslationType.class);
                map.put(annotation.type(), trans);
            } else {
    
    
                log.warn(trans.getClass().getName() + " 翻译实现类未标注 TranslationType 注解!");
            }
        }
        TranslationHandler.TRANSLATION_MAPPER.putAll(map);
        // 设置 Bean 序列化修改器
        objectMapper.setSerializerFactory(
            objectMapper.getSerializerFactory()
                .withSerializerModifier(new TranslationBeanSerializerModifier()));
    }

}

You can see that a collection is injected into this configuration class List, its type is TranslationInterfacetranslation interface

. Let's take a look at its implementation classes.

These implementation classes are defined under commonthe module's translationpackage. The translation implementations of 部门, 字典, OSS, and will scan the translation interface implementation classes. Injected into Let's take a look at the effect. We can see that all implementation classes of the translation interface have indeed been injected. Since annotations can also be injected into , here we manually add the collection and debug it with breakpoints:用户名

SpringTranslationInterfaceList

DebugTranslationInterface

@AutowiredBeanMapMapDebug

Guess you like

Origin blog.csdn.net/qq_31762741/article/details/132098537