Merge branch '1.2.x'
This commit is contained in:
commit
673b4f6de5
|
|
@ -24,7 +24,11 @@ import javax.servlet.Servlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.springframework.aop.framework.autoproxy.AutoProxyUtils;
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
|
||||||
|
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
|
||||||
|
|
@ -93,6 +97,11 @@ public class ErrorMvcAutoConfiguration {
|
||||||
return new ErrorPageCustomizer(this.properties);
|
return new ErrorPageCustomizer(this.properties);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public static PreserveErrorControllerTargetClassPostProcessor preserveErrorControllerTargetClassPostProcessor() {
|
||||||
|
return new PreserveErrorControllerTargetClassPostProcessor();
|
||||||
|
}
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ConditionalOnProperty(prefix = "server.error.whitelabel", name = "enabled", matchIfMissing = true)
|
@ConditionalOnProperty(prefix = "server.error.whitelabel", name = "enabled", matchIfMissing = true)
|
||||||
@Conditional(ErrorTemplateMissingCondition.class)
|
@Conditional(ErrorTemplateMissingCondition.class)
|
||||||
|
|
@ -242,4 +251,29 @@ public class ErrorMvcAutoConfiguration {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@link BeanFactoryPostProcessor} to ensure that the target class of ErrorController
|
||||||
|
* MVC beans are preserved when using AOP.
|
||||||
|
*/
|
||||||
|
static class PreserveErrorControllerTargetClassPostProcessor
|
||||||
|
implements BeanFactoryPostProcessor {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
|
||||||
|
throws BeansException {
|
||||||
|
String[] errorControllerBeans = beanFactory
|
||||||
|
.getBeanNamesForType(ErrorController.class, false, false);
|
||||||
|
for (String errorControllerBean : errorControllerBeans) {
|
||||||
|
try {
|
||||||
|
beanFactory.getBeanDefinition(errorControllerBean).setAttribute(
|
||||||
|
AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE);
|
||||||
|
}
|
||||||
|
catch (Throwable ex) {
|
||||||
|
// Ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,10 @@ import java.lang.annotation.Target;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
|
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
@ -34,6 +38,7 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati
|
||||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||||
import org.springframework.boot.test.ApplicationContextTestUtils;
|
import org.springframework.boot.test.ApplicationContextTestUtils;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.EnableAspectJAutoProxy;
|
||||||
import org.springframework.context.annotation.Import;
|
import org.springframework.context.annotation.Import;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
|
@ -101,11 +106,21 @@ public class BasicErrorControllerDirectMockMvcTests {
|
||||||
setup((ConfigurableWebApplicationContext) new SpringApplication(
|
setup((ConfigurableWebApplicationContext) new SpringApplication(
|
||||||
WebMvcIncludedConfiguration.class).run("--server.port=0",
|
WebMvcIncludedConfiguration.class).run("--server.port=0",
|
||||||
"--server.error.whitelabel.enabled=false"));
|
"--server.error.whitelabel.enabled=false"));
|
||||||
|
|
||||||
this.thrown.expect(ServletException.class);
|
this.thrown.expect(ServletException.class);
|
||||||
this.mockMvc.perform(get("/error").accept(MediaType.TEXT_HTML));
|
this.mockMvc.perform(get("/error").accept(MediaType.TEXT_HTML));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void errorControllerWithAop() throws Exception {
|
||||||
|
setup((ConfigurableWebApplicationContext) new SpringApplication(
|
||||||
|
WithAopConfiguration.class).run("--server.port=0"));
|
||||||
|
MvcResult response = this.mockMvc
|
||||||
|
.perform(get("/error").accept(MediaType.TEXT_HTML))
|
||||||
|
.andExpect(status().isOk()).andReturn();
|
||||||
|
String content = response.getResponse().getContentAsString();
|
||||||
|
assertTrue("Wrong content: " + content, content.contains("status=999"));
|
||||||
|
}
|
||||||
|
|
||||||
@Target(ElementType.TYPE)
|
@Target(ElementType.TYPE)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Documented
|
@Documented
|
||||||
|
|
@ -127,6 +142,7 @@ public class BasicErrorControllerDirectMockMvcTests {
|
||||||
@MinimalWebConfiguration
|
@MinimalWebConfiguration
|
||||||
@EnableWebMvc
|
@EnableWebMvc
|
||||||
protected static class WebMvcIncludedConfiguration {
|
protected static class WebMvcIncludedConfiguration {
|
||||||
|
|
||||||
// For manual testing
|
// For manual testing
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(WebMvcIncludedConfiguration.class, args);
|
SpringApplication.run(WebMvcIncludedConfiguration.class, args);
|
||||||
|
|
@ -137,6 +153,7 @@ public class BasicErrorControllerDirectMockMvcTests {
|
||||||
@Configuration
|
@Configuration
|
||||||
@MinimalWebConfiguration
|
@MinimalWebConfiguration
|
||||||
protected static class VanillaConfiguration {
|
protected static class VanillaConfiguration {
|
||||||
|
|
||||||
// For manual testing
|
// For manual testing
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(VanillaConfiguration.class, args);
|
SpringApplication.run(VanillaConfiguration.class, args);
|
||||||
|
|
@ -147,11 +164,30 @@ public class BasicErrorControllerDirectMockMvcTests {
|
||||||
@Configuration
|
@Configuration
|
||||||
@MinimalWebConfiguration
|
@MinimalWebConfiguration
|
||||||
protected static class ChildConfiguration {
|
protected static class ChildConfiguration {
|
||||||
|
|
||||||
// For manual testing
|
// For manual testing
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new SpringApplicationBuilder(ParentConfiguration.class)
|
new SpringApplicationBuilder(ParentConfiguration.class)
|
||||||
.child(ChildConfiguration.class).run(args);
|
.child(ChildConfiguration.class).run(args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableAspectJAutoProxy(proxyTargetClass = false)
|
||||||
|
@MinimalWebConfiguration
|
||||||
|
@Aspect
|
||||||
|
protected static class WithAopConfiguration {
|
||||||
|
|
||||||
|
@Pointcut("within(@org.springframework.stereotype.Controller *)")
|
||||||
|
private void controllerPointCut() {
|
||||||
|
};
|
||||||
|
|
||||||
|
@Around("controllerPointCut()")
|
||||||
|
public Object mvcAdvice(ProceedingJoinPoint pjp) throws Throwable {
|
||||||
|
return pjp.proceed();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue