Tag Archives: Springboot startup error

[Solved] springboot Startup Error: Bean with name ‘xxxxService‘ has been injected into other beans

An error was reported when starting the project in the local test today:

Bean with name ‘commonService’ has been injected into other beans [] in its raw version as part of a circular reference, but has eventually been wrapped. This means that said other beans do not use the final version of the bean. This is often the result of over-eager type matching – consider using ‘getBeanNamesOfType’ with the ‘allowEagerInit’ flag turned off, for example.**

Reason for error reporting:
it depends on a loop. Now I have a method that ServiceA needs to call serviceb, so ServiceA depends on serviceb, and there is a method that calls ServiceA in serviceb, which forms a loop dependency. When spring initializes a bean, it does not know which bean to initialize first, and an error will be reported.

What is circular dependency
bean a depends on B, and bean B depends on A. in this case, circular dependency occurs
bean a → bean B → bean a
more complex indirect dependencies cause cyclic dependencies as follows
Bean A → Bean B → Bean C → Bean D → Bean E → Bean A

What are the consequences of circular dependencies
when spring is loading all beans, spring tries to create beans in the order that they can be created normally
for example, there are the following dependencies:
bean a → bean B → bean C
spring first creates bean C, then creates bean B (injects C into b), and finally creates bean a (injects B into a).

However, when there is a circular dependency, spring will not be able to decide which bean to create first. In this case, spring will generate the exception beancurrentyincreationexception.

Solution:
1. Refactor the code and decouple it
2. When injecting beans, add @lazy annotation on the two interdependent beans

[Solved] Springboot startup error: org.springframework.beans.factory.unsatisfieddependenceexception…

Error Messages:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'indexController': Unsatisfied dependency expressed through field 'indexService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'indexServiceImpl': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userMapper' defined in file [I:\LX\aci_parent\service\service_acl\target\classes\com\lianxi\aclservice\mapper\UserMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [I:\LX\aci_parent\service\service_acl\target\classes\com\lianxi\aclservice\mapper\xml\PermissionMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [I:\LX\aci_parent\service\service_acl\target\classes\com\lianxi\aclservice\mapper\xml\PermissionMapper.xml]'. Cause: org.apache.ibatis.builder.BuilderException: Error resolving class. Cause: org.apache.ibatis.type.TypeException: Could not resolve type alias 'com.atguigu.aclservice.entity.Permission'.  Cause: java.lang.ClassNotFoundException: Cannot find class: com.atguigu.aclservice.entity.Permission

There are various cases of this error, one is that your mysql configuration is wrong, one is that your SQL statement or entity class is not correct. Another is that the xml mapping path is not correct.

[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.