io.micronaut.context.env.Environment.scan()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(2.8k)|赞(0)|评价(0)|浏览(195)

本文整理了Java中io.micronaut.context.env.Environment.scan()方法的一些代码示例,展示了Environment.scan()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Environment.scan()方法的具体详情如下:
包路径:io.micronaut.context.env.Environment
类名称:Environment
方法名:scan

Environment.scan介绍

[英]Scan the current environment for classes annotated with the given annotation. Use with care, repeated invocations should be avoided for performance reasons.
[中]扫描当前环境中使用给定注释注释的类。小心使用,出于性能原因,应避免重复调用。

代码示例

代码示例来源:origin: io.micronaut/micronaut-inject

  1. private boolean matchesPresenceOfEntities(ConditionContext context, AnnotationValue<Requires> annotationValue) {
  2. if (annotationValue.contains("entities")) {
  3. Optional<AnnotationClassValue[]> classNames = annotationValue.get("entities", AnnotationClassValue[].class);
  4. if (classNames.isPresent()) {
  5. BeanContext beanContext = context.getBeanContext();
  6. if (beanContext instanceof ApplicationContext) {
  7. ApplicationContext applicationContext = (ApplicationContext) beanContext;
  8. final AnnotationClassValue[] classValues = classNames.get();
  9. for (AnnotationClassValue<?> classValue : classValues) {
  10. final Optional<? extends Class<?>> entityType = classValue.getType();
  11. if (!entityType.isPresent()) {
  12. context.fail("Annotation type [" + classValue.getName() + "] not present on classpath");
  13. return false;
  14. } else {
  15. Environment environment = applicationContext.getEnvironment();
  16. Class annotationType = entityType.get();
  17. if (!environment.scan(annotationType).findFirst().isPresent()) {
  18. context.fail("No entities found in packages [" + String.join(", ", environment.getPackages()) + "] for annotation: " + annotationType);
  19. return false;
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }
  26. return true;
  27. }

代码示例来源:origin: io.micronaut/inject

  1. private boolean matchesPresenceOfEntities(ConditionContext context, AnnotationValue<Requires> annotationValue) {
  2. if (annotationValue.contains("entities")) {
  3. Optional<AnnotationClassValue[]> classNames = annotationValue.get("entities", AnnotationClassValue[].class);
  4. if (classNames.isPresent()) {
  5. BeanContext beanContext = context.getBeanContext();
  6. if (beanContext instanceof ApplicationContext) {
  7. ApplicationContext applicationContext = (ApplicationContext) beanContext;
  8. final AnnotationClassValue[] classValues = classNames.get();
  9. for (AnnotationClassValue<?> classValue : classValues) {
  10. final Optional<? extends Class<?>> entityType = classValue.getType();
  11. if (!entityType.isPresent()) {
  12. context.fail("Annotation type [" + classValue.getName() + "] not present on classpath");
  13. return false;
  14. } else {
  15. Environment environment = applicationContext.getEnvironment();
  16. Class annotationType = entityType.get();
  17. if (!environment.scan(annotationType).findFirst().isPresent()) {
  18. context.fail("No entities found in packages [" + String.join(", ", environment.getPackages()) + "] for annotation: " + annotationType);
  19. return false;
  20. }
  21. }
  22. }
  23. }
  24. }
  25. }
  26. return true;
  27. }

相关文章