使用spring上下文对象+注解实现工厂模式取不同的bean!
程序员文章站
2022-05-23 10:19:28
...
package com.coship.common.utils;
import com.coship.common.anno.MarkCode;
import org.apache.log4j.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @description Spring上下文工具类
*/
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static Logger logger = Logger.getLogger(SpringContextUtil.class);
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if (SpringContextUtil.applicationContext == null) {
SpringContextUtil.applicationContext = applicationContext;
}
logger.info("初始化ApplicationContext:" + applicationContext);
}
/**
* @return org.springframework.context.ApplicationContext
* @description 获取applicationContext
* @params []
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* @return java.lang.Object
* @description 通过beanName获取Bean
* @params [name]
*/
public static Object getBean(String beanName) {
return getApplicationContext().getBean(beanName);
}
/**
* @return T
* @description 通过class获取Bean
* @params [clazz]
*/
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
/**
* @return T
* @description 通过beanName和class返回指定的Bean
* @params [name, clazz]
*/
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
/**
* @return Map<String, T>
* @description 获取指定类型的所有bean实例
* @params [clazz]
*/
public static <T> Map<String, T> getBeansByType(Class<T> clazz) {
Map<String, T> instances = getApplicationContext().getBeansOfType(clazz);
return instances;
}
/**
* @return T
* @description 根据类型和MarkCode注解值获取bean实例
* @params [clazz, markCode]
*/
public static <T> T getBeanByMarkCode(Class<T> clazz, String markCode) {
Map<String, T> instances = getBeansByType(clazz);
if (instances.isEmpty()) {
logger.info("未获取到类型[" + clazz + "]Bean列表!");
return null;
}
logger.info("获取类型[" + clazz + "]Bean列表:" + instances);
for (String beanName : instances.keySet()) {
T instance = instances.get(beanName);
MarkCode code = instance.getClass().getAnnotation(MarkCode.class);
if (null == code) {
logger.info("BeanName[" + beanName + "]不包含MarkCode注解!");
continue;
}
if (StringUtil.isNil(code.value())) {
logger.info("BeanName[" + beanName + "]MarkCode注解值为空!");
continue;
}
if (code.value().equals(markCode)) {
return instance;
}
}
return null;
}
}
package com.coship.common.anno;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* @description 标记码注解(主要用来获取bean实例)
*/
@Documented
@Retention(RUNTIME)
@Target({ElementType.TYPE})
public @interface MarkCode {
String value() default "";
}
调用示例:
List<RuleSceneTriggerParam> triggerConditions = saveRuleSceneDetailDto.getTriggerConditions();
if (!triggerConditions.isEmpty()) {
RuleSceneDetailService ruleSceneTriggerService = SpringContextUtil.getBeanByMarkCode(RuleSceneDetailService.class, "trigger");
ruleSceneTriggerService.saveDetailData(new SaveRuleSceneTriggerDataDto(triggerConditions, spCode, ruleId, ruleName, creUserId));
}