春季和普罗米修斯问题

发布时间:2020-07-07 15:50

我似乎无法理解为什么尝试使用普罗米修斯(Prometheus)自动对MeterRegistry进行自动接线时出现此错误的原因:Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'test': Unsatisfied dependency expressed through field 'meterRegistry'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'io.micrometer.core.instrument.MeterRegistry' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

这是主要类和测试类,它们试图进行自动装配:

@SpringBootApplication
@EnableScheduling
public class DemoApplication {
    
    public static void main(String[] args) {

        var factory = new AnnotationConfigApplicationContext(AppConfig.class, test.class);
        //factory.refresh();
        test tst = factory.getBean(test.class);
        tst.getDummy().test();

        Class aClass = test2.class;
        Annotation[] annotations = aClass.getAnnotations();

        for(Annotation annotation : annotations){
            System.out.println("Annotation type: " + annotation.annotationType().getSimpleName());
            if(annotation instanceof ClassPreamble){
                ClassPreamble myAnnotation = (ClassPreamble) annotation;
                System.out.println("name: " + myAnnotation.author());
                System.out.println("value: " + Arrays.toString(myAnnotation.reviewers()));
            }
        }

        SpringApplication.run(DemoApplication.class, args);
    }

}

@Component
public class test{
    @Autowired
    test2 dummy;

    @Autowired
    private MeterRegistry meterRegistry;

    public void setDummy(test2 dummy) {
        this.dummy = dummy;
    }

    public test2 getDummy() {
        return dummy;
    }

    void test(){
        System.out.println("test");
    }

    @Scheduled(fixedRate = 5000)
    public void reportCurrentTime() {
        System.out.println("Scheduled!");
    }
}

如果将@Autowired private MeterRegistry meterRegistry;移至DemoApplication类,则不再引发欠幅时间异常。知道为什么吗?

回答1