Preface
I recently saw the RuoYi-Vue-Plus
translation Translation
module 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 TranslationInterface
after Spring version 3.0 . I usually don’t notice this. Therefore, record this writing method.List
Map
previous approach
Previously, definitions were generally 策略模式+工厂模式
combined Spring上下文的Aware回调
to obtain them . Here are some methods 指定类型Bean对象
to obtain specified types :Bean
ApplicationContext
Method 1: Save the object during initialization- Method 2: Get the object through the
Spring
provided classutils
ApplicationContext
- Method 3: Inherit from abstract class
ApplicationObjectSupport
- Method 4: Inherit from abstract class
WebApplicationObjectSupport
- Method 5: Implement the interface
ApplicationContextAware
- Method 6: Through
Spring
the providedContextLoader
Translation module configuration class
TranslationConfig
This 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 TranslationInterface
translation interface
. Let's take a look at its implementation classes.
These implementation classes are defined under common
the module's translation
package. 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:用户名
Spring
TranslationInterface
List
Debug
TranslationInterface
@Autowired
Bean
Map
Map
Debug