Refactor the Actuator MVC configuration to allow more customization
There is a new spring.factories entry for org.springframework.boot.actuate.autoconfigure.EndpointWebMvcConfiguration which loads extra beans into the MVC config for the Actuator. If the management context is a child context all the beans go in the child (except the Spring Security filter still). A big bonus is that you can add WebConfigurerAdapters to configure static resources etc. A new component called ManagementContextResolver can be used to locate the ApplicationContext for the MVC endpoints. Fixes gh-3345
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright 2012-2014 the original author or authors.
|
||||
*
|
||||
* 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 org.springframework.boot.autoconfigure.web;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.ConfigurableWebApplicationContext;
|
||||
|
||||
/**
|
||||
* Tests for welcome page using {@link MockMvc} and {@link SpringJUnit4ClassRunner}.
|
||||
*
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class WelcomePageMockMvcTests {
|
||||
|
||||
private ConfigurableWebApplicationContext wac;
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@After
|
||||
public void close() {
|
||||
if (wac != null) {
|
||||
wac.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void homePageNotFound() throws Exception {
|
||||
wac = (ConfigurableWebApplicationContext) new SpringApplicationBuilder(
|
||||
TestConfiguration.class).run();
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
this.mockMvc.perform(get("/")).andExpect(status().isNotFound()).andReturn();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void homePageCustomLocation() throws Exception {
|
||||
wac = (ConfigurableWebApplicationContext) new SpringApplicationBuilder(
|
||||
TestConfiguration.class).properties(
|
||||
"spring.resources.staticLocations:classpath:/custom/").run();
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
this.mockMvc.perform(get("/")).andExpect(status().isOk()).andReturn();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void homePageCustomLocationNoTrailingSlash() throws Exception {
|
||||
wac = (ConfigurableWebApplicationContext) new SpringApplicationBuilder(
|
||||
TestConfiguration.class).properties("spring.resources.staticLocations:classpath:/custom").run();
|
||||
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
|
||||
this.mockMvc.perform(get("/")).andExpect(status().isOk()).andReturn();
|
||||
}
|
||||
|
||||
@Target(ElementType.TYPE)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Import({ EmbeddedServletContainerAutoConfiguration.EmbeddedTomcat.class,
|
||||
EmbeddedServletContainerAutoConfiguration.class,
|
||||
ServerPropertiesAutoConfiguration.class,
|
||||
DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
ErrorMvcAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class })
|
||||
protected static @interface MinimalWebConfiguration {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@MinimalWebConfiguration
|
||||
public static class TestConfiguration {
|
||||
|
||||
// For manual testing
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestConfiguration.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<html><body>Hello!</body></html>
|
||||
Reference in New Issue
Block a user