Solve the problem that IntelliJ IDEA cannot read configuration files

Recently, I am learning Mybatis, follow the video explanation to create a configuration file in a package of the project, and then read the configuration file, but it keeps prompting exception.
write picture description here

The official code to read the configuration file is:

String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();   

After repeated thinking, the ide used for the video is eclipse, and the configuration files (.propoties and .xml, etc.) under the package in eclipse will be automatically copied to the compiled folder after compilation, that is, the classes directory. So the code can be loaded to the config file by path.

Now we are using IntelliJ IDEA, and the way idea files these configurations is obviously different from eclipse. There is a  concept of Content Roots in idea. The corresponding Content Roots need to be configured for each folder. Content Roots include resources, sources, tests, etc.
write picture description here
as the picture shows:

  • The java folder is marked as Sources, then all the subfolders below are Sources, and .class files are generated after compilation.
  • The Resources folder is marked as Resources, then the configuration files in this folder will be automatically copied to the compilation folder when compiling.

So for the idea, there are the following solutions.

  1. Put the configuration file in the Resources folder and add the virtual path of the configuration file in the code. Select the file, right-click-Copy Reference, you can get the virtual path of the file.
  2. Put the configuration file in the com.zjut.ssm.config package, and modify the Content Roots of the config folder to Resources, then the code can be recognized after compilation.

The above two solutions are applicable to make project way to compile the project.

If it is a maven project. It can also be achieved by configuring pom.xml.

 <build>
    <finalName>springmvc-study</finalName>
    <resources>
        <resource>
            <directory>${basedir}/src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>${basedir}/src/main/resources</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>

        </resource>
    </resources>
 </build>

In this way, without setting the idea, you can also use the maven compile command to compile the project and copy the configuration file to the compiled folder.

 

  1. So the question is, does intellij have to manage source code and resource files in such a separate way? No, intellij can also mix source and resource into a pot of porridge like eclipse, and let's introduce how to do it.
    1. It should be noted here that Intellij IDEA Version 2016.2.5 has bugs. Sometimes, the resource files under the source can be copied to the target folder, and sometimes, they will be ignored. The problem solved in this article is that when When your intellij no longer copies the resource files for you, how do you let intellij automatically copy the resource files to the output folder for you by operating
  1. I checkout a project directly from a friend's server
 
  1. Do some configuration for this project, such as the output folder, the location of the library, and deploy tomcat
 
  1. After running, you can see that the classes file can be compiled successfully, but the configuration file cannot
 
  1. can be solved like this
    1. Then go to the settings and do the following settings - these two steps can completely solve the problem
First open the setting's build --> Compiler --> delete the ? in the !?*.java to become !*.java
Tick ​​the bottom
  1. The configuration files are all available

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326017830&siteId=291194637
Recommended