Skip to content

Commit

Permalink
feat: Add ConditionalOnMissingBean for ServletRegistration (#17883) (#…
Browse files Browse the repository at this point in the history
…19944)

Co-authored-by: Mikhail Shabarov <61410877+mshabarov@users.noreply.github.com>
  • Loading branch information
vaadin-bot and mshabarov authored Sep 12, 2024
1 parent 1d19d44 commit 7127763
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletContextInitializer;
Expand Down Expand Up @@ -69,6 +70,7 @@ public ServletContextInitializer contextInitializer() {
* @return a custom ServletRegistrationBean instance
*/
@Bean
@ConditionalOnMissingBean(value = SpringServlet.class, parameterizedContainer = ServletRegistrationBean.class)
public ServletRegistrationBean<SpringServlet> servletRegistrationBean(
ObjectProvider<MultipartConfigElement> multipartConfig,
VaadinConfigurationProperties configurationProperties) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2000-2023 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.spring;

import org.junit.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.WebApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;

public class SpringBootAutoConfigurationConditionalTest {

@Test
public void customServletRegistrationBean() {
new WebApplicationContextRunner()
.withConfiguration(AutoConfigurations
.of(SpringBootAutoConfiguration.class))
.withUserConfiguration(
ServletRegistrationBeanConfiguration.class)
.run(context -> assertThat(context)
.getBean(ServletRegistrationBean.class).isInstanceOf(
ServletRegistrationBeanConfiguration.MockServletRegistrationBean.class));
}

@Configuration(proxyBeanMethods = false)
public static class ServletRegistrationBeanConfiguration {

@Bean
public ServletRegistrationBean<SpringServlet> servletRegistrationBean(
final WebApplicationContext webApplicationContext) {
return new MockServletRegistrationBean(webApplicationContext);
}

public static class MockServletRegistrationBean
extends ServletRegistrationBean<SpringServlet> {

public MockServletRegistrationBean(
final WebApplicationContext webApplicationContext) {
super(new SpringServlet(webApplicationContext, false));
}
}

}
}

0 comments on commit 7127763

Please sign in to comment.