Issues:
- Mapping失效,I added a controller (value="md") in my program and made the following configuration, but they are ineffective:
yaml server: servlet: context-path: /api
- Loading order, I loaded a static variable using Runner in Main.java, but I cannot access it through a member variable in the above controller, it shows as null, I can only load it dynamically in the method
code
@Slf4j
@SpringBootApplication
public class Main extends SpringBootServletInitializer implements ApplicationRunner {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
private static Path filePath;
//This method returns null when called at the member variable level, but returns the correct value when called within a method.
public static Path getFilePath() {
return filePath;
}
@Resource
private Environment env;
@Override
public void run(ApplicationArguments args) throws Exception {
String fileName="content.md";
String beifen = env.getProperty("dir.beifen");
log.info("初始化文件 备份路径:{}",beifen);
Path filePath = Paths.get(beifen, fileName);
Path parent = filePath.getParent();
// 创建父目录
if (parent != null) {
Files.createDirectories(parent);
}
// 如果路径已经存在且是目录,提示错误
if (Files.isDirectory(filePath)) {
throw new IOException("目标路径已是目录: " + filePath);
}
// 不存在则创建空文件(存在就跳过)
if (Files.notExists(filePath)) {
Files.createFile(filePath);
}
this.filePath = filePath;
log.info("初始化完成 path: {}",filePath.toString());
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
log.info("SpringApplicationBuilder-----------------");
log.info("this: {}",this.getClass());
log.info("Main: {}",Main.class);
return builder.sources(Main.class);
}
}
My dependency versions
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.4.5</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
Comment From: wenzhuo4657
Tomcat version is 10.1.46