Skip to content

Commit ff566d6

Browse files
committed
test addResourceHandler
1 parent dd53dd3 commit ff566d6

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/main/java/cn/netbuffer/springboot/demo/config/WebMvcConfig.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cn.netbuffer.springboot.demo.decorator.HttpTaskDecorator;
44
import lombok.extern.slf4j.Slf4j;
55
import org.springframework.aop.interceptor.AsyncExecutionAspectSupport;
6+
import org.springframework.beans.factory.annotation.Value;
67
import org.springframework.boot.actuate.trace.http.HttpTraceRepository;
78
import org.springframework.boot.actuate.trace.http.InMemoryHttpTraceRepository;
89
import org.springframework.context.annotation.Bean;
@@ -11,13 +12,20 @@
1112
import org.springframework.http.MediaType;
1213
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
1314
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
15+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
1416
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
1517
import java.util.concurrent.ThreadPoolExecutor;
1618

1719
@Slf4j
1820
@Configuration
1921
public class WebMvcConfig implements WebMvcConfigurer {
2022

23+
@Value("${extra-static.file-path1}")
24+
private String filePath1;
25+
26+
@Value("${extra-static.file-path2}")
27+
private String filePath2;
28+
2129
@Bean(name = AsyncExecutionAspectSupport.DEFAULT_TASK_EXECUTOR_BEAN_NAME)
2230
public AsyncTaskExecutor buildAsyncTaskExecutor() {
2331
log.info("buildAsyncTaskExecutor");
@@ -58,4 +66,13 @@ public void configureContentNegotiation(ContentNegotiationConfigurer configurer)
5866
.defaultContentType(MediaType.APPLICATION_JSON);
5967
}
6068

61-
}
69+
@Override
70+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
71+
log.info("bind static path /uploads1/** to file path:{}", filePath1);
72+
log.info("bind static path /uploads2/** to file path:{}", filePath2);
73+
String filePrefix = "file:";
74+
registry.addResourceHandler("/uploads1/**").addResourceLocations(filePrefix + filePath1);
75+
registry.addResourceHandler("/uploads2/**").addResourceLocations(filePrefix + filePath2);
76+
}
77+
78+
}

src/main/resources/application.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ info:
6969

7070
# 输出更多日志信息(embedded container, Hibernate, and Spring Boot)
7171
debug: false
72+
73+
extra-static:
74+
file-path1: /home/uploads1/
75+
file-path2: /home/uploads2/

0 commit comments

Comments
 (0)