Java project reads the resource file path problem in resources

This article is reproduced from: https://www.cnblogs.com/shuimuzhushui/p/7247864.html Author: shuimuzhushui Please indicate the statement when reprinting.

Normally, when reading a file under a certain path in a Java project, you can use an absolute path and a relative path. There is nothing to say about the absolute path, and the relative path is the path relative to the current class. The way of reading files in the local project and the server is different. The configuration file in the following figure is an example.

Read resource files locally

The java class needs to read the configuration file in the properties, which can be read in the way of file (File) :

1 File file = new File("src/main/resources/properties/basecom.properties");
2 InputStream in = new FileInputStream(file);

When running in eclipse (not deploying to the server), the file can be read.

The server (Tomcat) reads the resource file

Method 1: Using Stream + Properties

When the project is deployed to Tomcat, according to the above method, there will be an exception that the file path cannot be found. According to the search information, when the Java project is packaged and deployed to Tomcat, the path of properties changes to the top level (under classes), which is determined by the Maven project structure. For web projects built by Maven, the main code is placed in the src/main/java path, and the resources are placed in the src/main/resources path. When building a war package, the main code and resource files will be placed in the classes folder. :

Moreover, at this time, the file needs to be read in the way of stream (stream) , and loaded through the Properties class in the JDK, the information in the configuration file can be easily obtained, as follows:

1 InputStream in = this.getClass().getResourceAsStream("/properties/basecom.properties");
2 Properties properties = new Properties();
3 properties.load(in);
4 properties.getProperty("property_name");

 The slash before properties, relative to the calling class, is the common top-level path.

Method 2: Using Spring annotations

If Spring is used in the project, the configuration information can be obtained through annotations, but the configuration information can only be put into the context after the configuration file is scanned in the Spring configuration file.

1 <context:component-scan base-package="com.xxxx.service"/>
2 <context:property-placeholder location="classpath:properties/xxx.properties" ignore-unresolvable="true"/>

Then in the program, you can use @Value to get the property value in the properties file, as follows:

1 @Value("${xxxt.server}")
2 private static String serverUrl;

Method 3: Using Spring Configuration

You can also read property values ​​in the Spring configuration file and assign class member variables

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans 
 5     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
 6     
 7     <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 8         <property name="location" value="classpath:properties/xxx.properties"/>
 9     </bean>
10     
11     <bean id="service" class="com.xxxx.service.ServiceImpl">
12         <property name="serverUrl" value="${xxxt.server}" />
13     </bean>
14 
15 </beans>

 For example, the service class:

 1 package com.springtest.service;
 2 
 3 public class ServiceImpl {
 4 
 5     private String serverUrl;
 6 
 7     public String getServerUrl() {
 8         return serverUrl;
 9     }
10 
11     public void setServerUrl(String serverUrl) {
12         this.serverUrl = serverUrl;
13     }
14     
15     public void sayHello(){
16         System.out.println(serverUrl);
17     }
18 }

 Configuration file:

server=123.23.43.23

 test:

 1 public class ServiceTest {
 2 
 3     public static void main(String[] args) {
 4         // TODO Auto-generated method stub
 5         ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring.xml");
 6         ServiceImpl s = ctx.getBean("service", ServiceImpl.class);
 7         s.sayHello();
 8     }
 9 
10 }

 output:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
123.23.43.23

 

refer to:

Resource from src/main/resources not found after building with maven        

[Java] Read the files in the resources directory in the jar file     

Guess you like

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