parent
3c3e8e6a8b
commit
b313b3395f
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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 javax.annotation.PreDestroy;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
|
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
|
||||||
import org.springframework.aop.framework.autoproxy.target.LazyInitTargetSourceCreator;
|
import org.springframework.aop.framework.autoproxy.target.LazyInitTargetSourceCreator;
|
||||||
import org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource;
|
import org.springframework.aop.target.AbstractBeanFactoryBasedTargetSource;
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.context.event.ApplicationContextEvent;
|
import org.springframework.context.event.ApplicationContextEvent;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Integration tests for {@link BeanNameAutoProxyCreator} and
|
||||||
|
* {@link LazyInitTargetSourceCreator}.
|
||||||
|
*
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Arrault Fabien
|
* @author Arrault Fabien
|
||||||
|
* @author Sam Brannen
|
||||||
*/
|
*/
|
||||||
public class AutoProxyLazyInitTests {
|
class AutoProxyLazyInitTests {
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void resetBeans() {
|
||||||
|
MyBeanImpl.initialized = false;
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void withStaticBeanMethod() {
|
void withStaticBeanMethod() {
|
||||||
MyBeanImpl.initialized = false;
|
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStatic.class);
|
||||||
|
MyBean bean = ctx.getBean(MyBean.class);
|
||||||
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStatic.class);
|
|
||||||
MyBean bean = ctx.getBean("myBean", MyBean.class);
|
|
||||||
|
|
||||||
assertThat(MyBeanImpl.initialized).isFalse();
|
assertThat(MyBeanImpl.initialized).isFalse();
|
||||||
bean.doIt();
|
bean.doIt();
|
||||||
assertThat(MyBeanImpl.initialized).isTrue();
|
assertThat(MyBeanImpl.initialized).isTrue();
|
||||||
|
|
||||||
|
ctx.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void withStaticBeanMethodAndInterface() {
|
void withStaticBeanMethodAndInterface() {
|
||||||
MyBeanImpl.initialized = false;
|
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStaticAndInterface.class);
|
||||||
|
MyBean bean = ctx.getBean(MyBean.class);
|
||||||
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithStaticAndInterface.class);
|
|
||||||
MyBean bean = ctx.getBean("myBean", MyBean.class);
|
|
||||||
|
|
||||||
assertThat(MyBeanImpl.initialized).isFalse();
|
assertThat(MyBeanImpl.initialized).isFalse();
|
||||||
bean.doIt();
|
bean.doIt();
|
||||||
assertThat(MyBeanImpl.initialized).isTrue();
|
assertThat(MyBeanImpl.initialized).isTrue();
|
||||||
|
|
||||||
|
ctx.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void withNonStaticBeanMethod() {
|
void withNonStaticBeanMethod() {
|
||||||
MyBeanImpl.initialized = false;
|
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStatic.class);
|
||||||
|
MyBean bean = ctx.getBean(MyBean.class);
|
||||||
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStatic.class);
|
|
||||||
MyBean bean = ctx.getBean("myBean", MyBean.class);
|
|
||||||
|
|
||||||
assertThat(MyBeanImpl.initialized).isFalse();
|
assertThat(MyBeanImpl.initialized).isFalse();
|
||||||
bean.doIt();
|
bean.doIt();
|
||||||
assertThat(MyBeanImpl.initialized).isTrue();
|
assertThat(MyBeanImpl.initialized).isTrue();
|
||||||
|
|
||||||
|
ctx.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void withNonStaticBeanMethodAndInterface() {
|
void withNonStaticBeanMethodAndInterface() {
|
||||||
MyBeanImpl.initialized = false;
|
ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStaticAndInterface.class);
|
||||||
|
MyBean bean = ctx.getBean(MyBean.class);
|
||||||
ApplicationContext ctx = new AnnotationConfigApplicationContext(ConfigWithNonStaticAndInterface.class);
|
|
||||||
MyBean bean = ctx.getBean("myBean", MyBean.class);
|
|
||||||
|
|
||||||
assertThat(MyBeanImpl.initialized).isFalse();
|
assertThat(MyBeanImpl.initialized).isFalse();
|
||||||
bean.doIt();
|
bean.doIt();
|
||||||
assertThat(MyBeanImpl.initialized).isTrue();
|
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;
|
initialized = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -110,46 +120,48 @@ public class AutoProxyLazyInitTests {
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public static class ConfigWithStatic {
|
static class ConfigWithStatic {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
|
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
|
||||||
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
|
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
|
||||||
|
autoProxyCreator.setBeanNames("*");
|
||||||
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
|
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
|
||||||
return autoProxyCreator;
|
return autoProxyCreator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
|
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
|
||||||
return new StrictLazyInitTargetSourceCreator();
|
return new StrictLazyInitTargetSourceCreator();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Lazy
|
@Lazy
|
||||||
public static MyBean myBean() {
|
static MyBean myBean() {
|
||||||
return new MyBeanImpl();
|
return new MyBeanImpl();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public static class ConfigWithStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {
|
static class ConfigWithStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
|
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
|
||||||
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
|
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
|
||||||
|
autoProxyCreator.setBeanNames("*");
|
||||||
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
|
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
|
||||||
return autoProxyCreator;
|
return autoProxyCreator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
|
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
|
||||||
return new StrictLazyInitTargetSourceCreator();
|
return new StrictLazyInitTargetSourceCreator();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Lazy
|
@Lazy
|
||||||
public static MyBean myBean() {
|
static MyBean myBean() {
|
||||||
return new MyBeanImpl();
|
return new MyBeanImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -160,46 +172,48 @@ public class AutoProxyLazyInitTests {
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public static class ConfigWithNonStatic {
|
static class ConfigWithNonStatic {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
|
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
|
||||||
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
|
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
|
||||||
|
autoProxyCreator.setBeanNames("*");
|
||||||
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
|
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
|
||||||
return autoProxyCreator;
|
return autoProxyCreator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
|
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
|
||||||
return new StrictLazyInitTargetSourceCreator();
|
return new StrictLazyInitTargetSourceCreator();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Lazy
|
@Lazy
|
||||||
public MyBean myBean() {
|
MyBean myBean() {
|
||||||
return new MyBeanImpl();
|
return new MyBeanImpl();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public static class ConfigWithNonStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {
|
static class ConfigWithNonStaticAndInterface implements ApplicationListener<ApplicationContextEvent> {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
|
BeanNameAutoProxyCreator lazyInitAutoProxyCreator() {
|
||||||
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
|
BeanNameAutoProxyCreator autoProxyCreator = new BeanNameAutoProxyCreator();
|
||||||
|
autoProxyCreator.setBeanNames("*");
|
||||||
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
|
autoProxyCreator.setCustomTargetSourceCreators(lazyInitTargetSourceCreator());
|
||||||
return autoProxyCreator;
|
return autoProxyCreator;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
|
LazyInitTargetSourceCreator lazyInitTargetSourceCreator() {
|
||||||
return new StrictLazyInitTargetSourceCreator();
|
return new StrictLazyInitTargetSourceCreator();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Lazy
|
@Lazy
|
||||||
public MyBean myBean() {
|
MyBean myBean() {
|
||||||
return new MyBeanImpl();
|
return new MyBeanImpl();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue