Skip to content

Commit cd33825

Browse files
committed
Merge pull request #45007 from dmytrodanilenkov
* pr/45007-squash: Polish "Add missing attributes to ServletRegistration annotation" Add missing attributes to ServletRegistration annotation Closes gh-45007
2 parents 475a046 + 5401327 commit cd33825

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletContextInitializerBeans.java

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
* @author Brian Clozel
6666
* @author Moritz Halbritter
6767
* @author Daeho Kwon
68+
* @author Dmytro Danilenkov
6869
* @since 1.4.0
6970
*/
7071
public class ServletContextInitializerBeans extends AbstractCollection<ServletContextInitializer> {
@@ -320,6 +321,10 @@ private void configureFromAnnotation(ServletRegistrationBean<Servlet> bean, Serv
320321
bean.setIgnoreRegistrationFailure(registration.ignoreRegistrationFailure());
321322
bean.setLoadOnStartup(registration.loadOnStartup());
322323
bean.setUrlMappings(Arrays.asList(registration.urlMappings()));
324+
for (WebInitParam param : registration.initParameters()) {
325+
bean.addInitParameter(param.name(), param.value());
326+
}
327+
bean.setMultipartConfig(new MultipartConfigElement(registration.multipartConfig()));
323328
}
324329

325330
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/ServletRegistration.java

+15
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.lang.annotation.Target;
2424

2525
import jakarta.servlet.Servlet;
26+
import jakarta.servlet.annotation.MultipartConfig;
27+
import jakarta.servlet.annotation.WebInitParam;
2628

2729
import org.springframework.core.Ordered;
2830
import org.springframework.core.annotation.AliasFor;
@@ -33,6 +35,7 @@
3335
* annotation-based alternative to {@link ServletRegistrationBean}.
3436
*
3537
* @author Moritz Halbritter
38+
* @author Dmytro Danilenkov
3639
* @since 3.5.0
3740
* @see ServletRegistrationBean
3841
*/
@@ -87,4 +90,16 @@
8790
*/
8891
int loadOnStartup() default -1;
8992

93+
/**
94+
* Init parameters to be used with the servlet.
95+
* @return the init parameters
96+
*/
97+
WebInitParam[] initParameters() default {};
98+
99+
/**
100+
* The multipart configuration.
101+
* @return the multipart configuration
102+
*/
103+
MultipartConfig multipartConfig() default @MultipartConfig;
104+
90105
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/ServletContextInitializerBeansTests.java

+14-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import jakarta.servlet.ServletContext;
2828
import jakarta.servlet.ServletRequest;
2929
import jakarta.servlet.ServletResponse;
30+
import jakarta.servlet.annotation.MultipartConfig;
3031
import jakarta.servlet.annotation.WebInitParam;
3132
import jakarta.servlet.http.HttpServlet;
3233
import jakarta.servlet.http.HttpServletRequest;
@@ -51,6 +52,7 @@
5152
* @author Andy Wilkinson
5253
* @author Moritz Halbritter
5354
* @author Daeho Kwon
55+
* @author Dmytro Danilenkov
5456
*/
5557
class ServletContextInitializerBeansTests {
5658

@@ -127,6 +129,13 @@ void shouldApplyServletRegistrationAnnotation() {
127129
assertThat(servletRegistrationBean.getServletName()).isEqualTo("test");
128130
assertThat(servletRegistrationBean.isAsyncSupported()).isFalse();
129131
assertThat(servletRegistrationBean.getUrlMappings()).containsExactly("/test/*");
132+
assertThat(servletRegistrationBean.getInitParameters())
133+
.containsExactlyInAnyOrderEntriesOf(Map.of("env", "test", "debug", "true"));
134+
assertThat(servletRegistrationBean.getMultipartConfig()).isNotNull();
135+
assertThat(servletRegistrationBean.getMultipartConfig().getLocation()).isEqualTo("/tmp");
136+
assertThat(servletRegistrationBean.getMultipartConfig().getMaxFileSize()).isEqualTo(1024);
137+
assertThat(servletRegistrationBean.getMultipartConfig().getMaxRequestSize()).isEqualTo(4096);
138+
assertThat(servletRegistrationBean.getMultipartConfig().getFileSizeThreshold()).isEqualTo(128);
130139
});
131140
}
132141

@@ -244,7 +253,11 @@ static class ServletConfigurationWithAnnotation {
244253

245254
@Bean
246255
@ServletRegistration(enabled = false, name = "test", asyncSupported = false, urlMappings = "/test/*",
247-
loadOnStartup = 1)
256+
loadOnStartup = 1,
257+
initParameters = { @WebInitParam(name = "env", value = "test"),
258+
@WebInitParam(name = "debug", value = "true") },
259+
multipartConfig = @MultipartConfig(location = "/tmp", maxFileSize = 1024, maxRequestSize = 4096,
260+
fileSizeThreshold = 128))
248261
TestServlet testServlet() {
249262
return new TestServlet();
250263
}

0 commit comments

Comments
 (0)