AutoMapper import problem

preface

Recently, I was looking at a small project. There was a strange small problem, but it was this small problem that led to a major error after push ing to the server. This small problem led to the program unable to scan the corresponding entity, resulting in initialization failure, and finally the whole program could not run. After several days, I finally found the problem today. Therefore, this paper records the process of sad and bald.

Problem elaboration

The project structure is as follows
java
-------com
-------------aaaa.bbbb
----------------------------entity

@Configuration
public class AutoMapperConfig {
  @Bean
  public AutoMapper autoMapper() {
    return new AutoMapper(new String[] { "com.aaaa.bbbb.entity" }); //Configure the directory where the entity class is located (multiple are allowed, and configuration through the symbol * is not supported temporarily)
  }
}

When receiving a project, for convenience, you can directly use Git GUI Here on the desktop and clone the project with the command git clone. But when running, there was a series of errors.

A large string of balabalabala, which means that the file entity was not found, so the java file in the file could not be loaded, so the mapping failed. So the key problem is that Java does not find the entity file.

The road of exploration

Because the Java background said that it did not find the directory, and com.aaaa.bbbb.entity is the writing method of relative path, I was wondering whether it is the configuration problem of idea, so I marked the code file and test file in the directory respectively

Then mark sources and tests in the modules of file structure. A meal is as fierce as a tiger. Press run. I'm sorry, it's still the same mistake.

Then I found out that it may be the reason for caching. I heard that as long as you press a magic button, the dragon will meet your wishes.

Then idea closes and reloads a bunch of things... Then, it still reports an error...

As the saying goes, no more than three things happen. Then I found the third method, which may still be the problem of configuration.

Fill in your own file directory in the working directory.

However, the goose still reported an error....

The problem is solved temporarily

Found in continuous testing

java.com.aaaa.bbbb.entity
aaaa.bbbb.entity
bbbb.entity
entity

Can be initialized successfully, but com.aaaa.bbbb.entity cannot!!! Outrageous! Damn it!
Although you can change it back to com.aaaa.bbbb.entity when you push it up, it's too humiliating.

Fundamental problem

As the saying goes, if you want to practice divine skills, you must first, emmm and practice your internal skills.
In order to find out the reason, I choose to use debug to explore step by step.

@Configuration
public class AutoMapperConfig {
  @Bean
  public AutoMapper autoMapper() {
    return new AutoMapper(new String[] { "com.aaaa.bbbb.entity" }); //Configure the directory where the entity class is located (multiple are allowed, and configuration through the symbol * is not supported temporarily)
  }
}

The code calls the AutoMapper function
This function appears at com.github.dreamoung.mprelation

private String[] entityPackages;

	public AutoMapper() {
		this.entityPackages = new String[] {};
	}

	public AutoMapper(String[] entityPackages) {
		this.entityPackages = entityPackages;

		if (entityPackages != null && entityPackages.length > 0) {
			ClassLoader loader = Thread.currentThread().getContextClassLoader();
			for (int i = 0; i < entityPackages.length; i++) {
				String entityPackage = entityPackages[i];
				String packagePath = entityPackage.replace(".", "/");

				URL url = loader.getResource(packagePath);
				if (url != null) {
					String protocol = url.getProtocol();
					if (protocol.equals("file")) {
						File file = new File(url.getPath());
						File[] files = file.listFiles();
						for (File childFile : files) {
							String fileName = childFile.getName();
							if (fileName.endsWith(".class") && !fileName.contains("$")) {
								String className = entityPackage + "." + fileName.substring(0, fileName.length() - 6);

								autoMapperBean(className);

							}
						}

Continue debug ging

It is found that in the case of an error, although the backend can obtain com.aaaa.bbbb.entity and successfully convert it to com/aaaa/bbbb/entity, it cannot obtain the files in the corresponding directory. In short, it's like Hal's mobile castle. Although you open Hal's door, it's empty. For example, you want to buy milk tea, and then you successfully find the milk tea shop through the map, but the staff of the milk tea shop ran away and didn't get anything, so you can't get anything.

Problem solving


Then carefully observe the url and find that part of the url is a string of garbled code?
In other words, you may have really arrived at Renmin Road, but Renmin Road may be from Guangzhou, Shanghai and Suzhou... If you find the wrong place, you can't get it.
Why does this happen?
Maybe it's because my user name is in Chinese (code Nongda taboo)... And the code is still not very friendly to Chinese, resulting in the smell of passing it on. The solution is very simple. Just put the project on disk D.

summary

Code is really a mistake. You have to be strict. After solving this problem, I touched my cool skull and sighed/ (愒o愒)/~~

Keywords: Java IntelliJ IDEA

Added by amo on Fri, 08 Oct 2021 20:27:15 +0300