Solve the problem that the ShardingSphere integration cannot be started

background

ShardingSphere is integrated in the project. After integration, it is found that it cannot be started. The error message is as follows:

image.png

solve

The root cause of the error is: Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.apache.shardingsphere.infra.config.mode.ModeConfiguration' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} Typical org.springframework.beans.factory.NoSuchBeanDefinitionExceptionproblem: More specifically, Spring cannot find org.apache.shardingsphere.infra.config.mode.ModeConfigurationit, so first check whether this class is included in the project.

image.png

  1. Obviously, this class exists in the project, but it is rather strange. This is a final-modified class, but there is no annotation. The error reported by Spring is that the class cannot be found during autowire, so it is suspected that it is caused by no annotation.

  2. Execute the mvn command to analyze dependencies

mvn dependency:tree > dependency.txt
复制代码

Analysis of dependency results: The dependency is the dependency of the common project. View the dependencies of the common project:

image.pngHere, a dependency of shardingsphere-infra-common is excluded, and another dependency is introduced. After verifying that this new dependency is a self-built version. Then the next step is to look at the code of this project.

  1. Clone the code from the warehouse. There is a lot of code in shardingsphere. The focus here is on common. Execute the following maven command to build the specified submodule.
mvn install -pl shardingsphere-infra -am
复制代码

Where pl is followed by the specified submodule, and -am indicates the specified module that builds the listed modules.

  1. Using the idea submodule, trying to ModeConfigurationadd annotations fails directly, so it must not be done by adding annotations to the source code, but if this configuration class is needed in the actual running process, there must be a place to create this object, and Injected into the container as a bean.

  2. I saw ShardingSphereAutoConfigurationthe injection of this class in , the code is as follows:

@Bean
public ModeConfiguration modeConfiguration() {
    return null == this.props.getMode() ? null : (new ModeConfigurationYamlSwapper()).swapToObject(this.props.getMode());
}
复制代码

Obviously, there needs to be a mode configuration in the properties, and then create a ModeConfiguration based on the mode property.

  1. You need to check which types ModeConfiguration supports. For details, please refer to sharding mode

  2. The final solution was added in the config file:

spring.shardingsphere.mode.type=Memory
复制代码

summary

For NoSuchBeanDefinitionExceptiontypical errors, the main thing is to find out whether the class that reports the error has been created. If it has not been created, you need to find the specific reason and then make targeted repairs.

Guess you like

Origin juejin.im/post/7086478069413724167