41
41
import org .mockito .InOrder ;
42
42
import org .mockito .MockitoAnnotations ;
43
43
44
+ import org .springframework .beans .BeansException ;
44
45
import org .springframework .beans .MutablePropertyValues ;
45
46
import org .springframework .beans .factory .BeanCreationException ;
46
47
import org .springframework .beans .factory .config .BeanDefinition ;
48
+ import org .springframework .beans .factory .config .BeanFactoryPostProcessor ;
47
49
import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
48
50
import org .springframework .beans .factory .config .ConstructorArgumentValues ;
49
51
import org .springframework .beans .factory .config .Scope ;
52
+ import org .springframework .beans .factory .support .AbstractBeanDefinition ;
50
53
import org .springframework .beans .factory .support .RootBeanDefinition ;
51
54
import org .springframework .boot .web .servlet .DelegatingFilterProxyRegistrationBean ;
52
55
import org .springframework .boot .web .servlet .FilterRegistrationBean ;
@@ -466,7 +469,8 @@ public void postProcessEmbeddedServletContainerFactory() throws Exception {
466
469
}
467
470
468
471
@ Test
469
- public void doesNotReplaceExistingScopes () throws Exception { // gh-2082
472
+ public void doesNotReplaceExistingScopes () throws Exception {
473
+ // gh-2082
470
474
Scope scope = mock (Scope .class );
471
475
ConfigurableListableBeanFactory factory = this .context .getBeanFactory ();
472
476
factory .registerScope (WebApplicationContext .SCOPE_REQUEST , scope );
@@ -482,6 +486,29 @@ public void doesNotReplaceExistingScopes() throws Exception { // gh-2082
482
486
.isSameAs (scope );
483
487
}
484
488
489
+ @ Test
490
+ public void servletRequestCanBeInjectedEarly () throws Exception {
491
+ // gh-14990
492
+ addEmbeddedServletContainerFactoryBean ();
493
+ RootBeanDefinition beanDefinition = new RootBeanDefinition (
494
+ WithAutowiredServletRequest .class );
495
+ beanDefinition .setAutowireMode (AbstractBeanDefinition .AUTOWIRE_CONSTRUCTOR );
496
+ this .context .registerBeanDefinition ("withAutowiredServletRequest" ,
497
+ beanDefinition );
498
+ this .context .addBeanFactoryPostProcessor (new BeanFactoryPostProcessor () {
499
+
500
+ @ Override
501
+ public void postProcessBeanFactory (
502
+ ConfigurableListableBeanFactory beanFactory ) throws BeansException {
503
+ WithAutowiredServletRequest bean = beanFactory
504
+ .getBean (WithAutowiredServletRequest .class );
505
+ assertThat (bean .getRequest ()).isNotNull ();
506
+ }
507
+
508
+ });
509
+ this .context .refresh ();
510
+ }
511
+
485
512
private void addEmbeddedServletContainerFactoryBean () {
486
513
this .context .registerBeanDefinition ("embeddedServletContainerFactory" ,
487
514
new RootBeanDefinition (MockEmbeddedServletContainerFactory .class ));
@@ -534,4 +561,18 @@ public void doFilter(ServletRequest request, ServletResponse response,
534
561
535
562
}
536
563
564
+ protected static class WithAutowiredServletRequest {
565
+
566
+ private final ServletRequest request ;
567
+
568
+ public WithAutowiredServletRequest (ServletRequest request ) {
569
+ this .request = request ;
570
+ }
571
+
572
+ public ServletRequest getRequest () {
573
+ return this .request ;
574
+ }
575
+
576
+ }
577
+
537
578
}
0 commit comments