Fix AutoProxyLazyInitTests

See gh-24915
This commit is contained in:
Sam Brannen 2020-05-12 12:35:37 +02:00
parent 3c3e8e6a8b
commit b313b3395f
1 changed files with 58 additions and 44 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 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.
@ -18,83 +18,93 @@ package org.springframework.context.annotation;
import javax.annotation.PreDestroy;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.target.LazyInitTargetSourceCreator;
import org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.ApplicationContextEvent;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link BeanNameAutoProxyCreator} and
* {@link LazyInitTargetSourceCreator}.
*
* @author Juergen Hoeller
* @author Arrault Fabien
* @author Sam Brannen
*/
public class AutoProxyLazyInitTests {
class AutoProxyLazyInitTests {
@BeforeEach
void resetBeans() {
MyBeanImpl.initialized = false;
}
@Test
public void withStaticBeanMethod() {
MyBeanImpl.initialized = false;
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStatic.class);
MyBean bean = ctx.getBean("myBean", MyBean.class);
void withStaticBeanMethod() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStatic.class);
MyBean bean = ctx.getBean(MyBean.class);
assertThat(MyBeanImpl.initialized).isFalse();
bean.doIt();
assertThat(MyBeanImpl.initialized).isTrue();
ctx.close();
}
@Test
public void withStaticBeanMethodAndInterface() {
MyBeanImpl.initialized = false;
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStaticAndInterface.class);
MyBean bean = ctx.getBean("myBean", MyBean.class);
void withStaticBeanMethodAndInterface() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStaticAndInterface.class);
MyBean bean = ctx.getBean(MyBean.class);
assertThat(MyBeanImpl.initialized).isFalse();
bean.doIt();
assertThat(MyBeanImpl.initialized).isTrue();
ctx.close();
}
@Test
public void withNonStaticBeanMethod() {
MyBeanImpl.initialized = false;
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStatic.class);
MyBean bean = ctx.getBean("myBean", MyBean.class);
void withNonStaticBeanMethod() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStatic.class);
MyBean bean = ctx.getBean(MyBean.class);
assertThat(MyBeanImpl.initialized).isFalse();
bean.doIt();
assertThat(MyBeanImpl.initialized).isTrue();
ctx.close();
}
@Test
public void withNonStaticBeanMethodAndInterface() {
MyBeanImpl.initialized = false;
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStaticAndInterface.class);
MyBean bean = ctx.getBean("myBean", MyBean.class);
void withNonStaticBeanMethodAndInterface() {
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStaticAndInterface.class);
MyBean bean = ctx.getBean(MyBean.class);
assertThat(MyBeanImpl.initialized).isFalse();
bean.doIt();
assertThat(MyBeanImpl.initialized).isTrue();
ctx.close();
}
public static interface MyBean {
interface MyBean {
public String doIt();
String doIt();
}
public static class MyBeanImpl implements MyBean {
static class MyBeanImpl implements MyBean {
public static boolean initialized = false;
static boolean initialized = false;
public MyBeanImpl() {
MyBeanImpl() {
initialized = true;
}
@ -110,46 +120,48 @@ public class AutoProxyLazyInitTests {
@Configuration
public static class ConfigWithStatic {
static class ConfigWithStatic {
@Bean
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
autoProxyCreator.setBeanNames("*");
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
return autoProxyCreator;
}
@Bean
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
return new StrictLazyInitTargetSourceCreator();
}
@Bean
@Lazy
public static MyBean myBean() {
static MyBean myBean() {
return new MyBeanImpl();
}
}
@Configuration
public static class ConfigWithStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {
static class ConfigWithStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {
@Bean
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
autoProxyCreator.setBeanNames("*");
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
return autoProxyCreator;
}
@Bean
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
return new StrictLazyInitTargetSourceCreator();
}
@Bean
@Lazy
public static MyBean myBean() {
static MyBean myBean() {
return new MyBeanImpl();
}
@ -160,46 +172,48 @@ public class AutoProxyLazyInitTests {
@Configuration
public static class ConfigWithNonStatic {
static class ConfigWithNonStatic {
@Bean
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
autoProxyCreator.setBeanNames("*");
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
return autoProxyCreator;
}
@Bean
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
return new StrictLazyInitTargetSourceCreator();
}
@Bean
@Lazy
public MyBean myBean() {
MyBean myBean() {
return new MyBeanImpl();
}
}
@Configuration
public static class ConfigWithNonStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {
static class ConfigWithNonStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {
@Bean
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
autoProxyCreator.setBeanNames("*");
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
return autoProxyCreator;
}
@Bean
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
return new StrictLazyInitTargetSourceCreator();
}
@Bean
@Lazy
public MyBean myBean() {
MyBean myBean() {
return new MyBeanImpl();
}