[Solved] SpringBoot Startup Error: is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

It is found that when springboot starts, the following log is printed:

2021-10-13 17:20:47.549 [main] INFO  ... Bean 'xxx' of type [xxx] 
is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

This is because a service implements the beanpostprocessor interface and depends on other services. Examples are as follows:

@Service
public class RandomIntProcessor implements BeanPostProcessor {
    
    @Autowired
    private RandomIntGenerator randomIntGenerator;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        Field[] fields = bean.getClass().getDeclaredFields();
        for (Field field : fields) {
            RandomInt injectRandomInt = field.getAnnotation(RandomInt.class);
            if (injectRandomInt != null) {
                int min = injectRandomInt.min();
                int max = injectRandomInt.max();
                int randomValue = randomIntGenerator.generate(min, max);
                field.setAccessible(true);
                ReflectionUtils.setField(field, bean, randomValue);
            }
        }
        return bean;
    }
}

The randomintprocessor class depends on the randomintgenerator class, which will cause the randomintprocessor.postprocessbeforeinitialization method to fail to receive the randomintgenerator initialization event.

It can be modified as follows:

@Service
public class RandomIntProcessor implements BeanPostProcessor {
    private final RandomIntGenerator randomIntGenerator;

    @Lazy
    public RandomIntProcessor(RandomIntGenerator randomIntGenerator) {
        this.randomIntGenerator = randomIntGenerator;
    }

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        //...
    }
}

Use lazy annotation to delay initialization and break the circular dependency.

Start the test again, and the previous prompt information will not be output.

Similar Posts: