@Slf4j
@Service
@ZdsxLb(type = ZdsxLbEnum.HQ)
public class ZdsxhqServiceImpl extends ServiceImpl<ZdsxhqDao, Zdsxhq> implements ZdsxhqService, ZdsxService
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import org.springframework.core.annotation.AnnotationUtils;
import java.lang.annotation.Annotation;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class ZdsxFactory {
public static <T> T get(ZdsxLbEnum zdsxLbEnum, Class<T> handler) {
Map<String, T> beans = SpringUtil.getBeansOfType(handler);
for (Map.Entry<String, T> entry : beans.entrySet()) {
// 原生方式拿不到注解
// Annotation[] annotations = entry.getValue().getClass().getAnnotations();
// 需要使用Spring的工具类AnnotationUtils
ZdsxLb zdsxLbAnnotation = AnnotationUtils.findAnnotation(entry.getValue().getClass(), ZdsxLb.class);
System.out.println(zdsxLbAnnotation);
if (ObjectUtil.isNotEmpty(zdsxLbAnnotation) && ObjectUtil.equal(zdsxLbEnum,zdsxLbAnnotation.type())) {
return entry.getValue();
}
}
return null;
}
}