Polishing
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2006 the original author or authors.
|
* Copyright 2002-2014 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.
|
||||||
@@ -25,7 +25,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
|||||||
* TargetSourceCreator that enforces a LazyInitTargetSource for each bean
|
* TargetSourceCreator that enforces a LazyInitTargetSource for each bean
|
||||||
* that is defined as "lazy-init". This will lead to a proxy created for
|
* that is defined as "lazy-init". This will lead to a proxy created for
|
||||||
* each of those beans, allowing to fetch a reference to such a bean
|
* each of those beans, allowing to fetch a reference to such a bean
|
||||||
* without actually initialized the target bean instance.
|
* without actually initializing the target bean instance.
|
||||||
*
|
*
|
||||||
* <p>To be registered as custom TargetSourceCreator for an auto-proxy creator,
|
* <p>To be registered as custom TargetSourceCreator for an auto-proxy creator,
|
||||||
* in combination with custom interceptors for specific beans or for the
|
* in combination with custom interceptors for specific beans or for the
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2014 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.
|
||||||
@@ -24,7 +24,6 @@ import org.apache.commons.logging.LogFactory;
|
|||||||
import org.springframework.aop.TargetSource;
|
import org.springframework.aop.TargetSource;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
import org.springframework.beans.factory.BeanFactoryAware;
|
import org.springframework.beans.factory.BeanFactoryAware;
|
||||||
import org.springframework.util.ClassUtils;
|
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -48,8 +47,7 @@ import org.springframework.util.ObjectUtils;
|
|||||||
* @see ThreadLocalTargetSource
|
* @see ThreadLocalTargetSource
|
||||||
* @see CommonsPoolTargetSource
|
* @see CommonsPoolTargetSource
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractBeanFactoryBasedTargetSource
|
public abstract class AbstractBeanFactoryBasedTargetSource implements TargetSource, BeanFactoryAware, Serializable {
|
||||||
implements TargetSource, BeanFactoryAware, Serializable {
|
|
||||||
|
|
||||||
/** use serialVersionUID from Spring 1.2.7 for interoperability */
|
/** use serialVersionUID from Spring 1.2.7 for interoperability */
|
||||||
private static final long serialVersionUID = -4721607536018568393L;
|
private static final long serialVersionUID = -4721607536018568393L;
|
||||||
@@ -108,7 +106,7 @@ public abstract class AbstractBeanFactoryBasedTargetSource
|
|||||||
@Override
|
@Override
|
||||||
public void setBeanFactory(BeanFactory beanFactory) {
|
public void setBeanFactory(BeanFactory beanFactory) {
|
||||||
if (this.targetBeanName == null) {
|
if (this.targetBeanName == null) {
|
||||||
throw new IllegalStateException("Property'targetBeanName' is required");
|
throw new IllegalStateException("Property 'targetBeanName' is required");
|
||||||
}
|
}
|
||||||
this.beanFactory = beanFactory;
|
this.beanFactory = beanFactory;
|
||||||
}
|
}
|
||||||
@@ -185,8 +183,7 @@ public abstract class AbstractBeanFactoryBasedTargetSource
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder(getClass().getSimpleName());
|
||||||
sb.append(ClassUtils.getShortName(getClass()));
|
|
||||||
sb.append(" for target bean '").append(this.targetBeanName).append("'");
|
sb.append(" for target bean '").append(this.targetBeanName).append("'");
|
||||||
if (this.targetClass != null) {
|
if (this.targetClass != null) {
|
||||||
sb.append(" of type [").append(this.targetClass.getName()).append("]");
|
sb.append(" of type [").append(this.targetClass.getName()).append("]");
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ class ExtendedBeanInfo implements BeanInfo {
|
|||||||
matches.add(method);
|
matches.add(method);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// sort non-void returning write methods to guard against the ill effects of
|
// Sort non-void returning write methods to guard against the ill effects of
|
||||||
// non-deterministic sorting of methods returned from Class#getDeclaredMethods
|
// non-deterministic sorting of methods returned from Class#getDeclaredMethods
|
||||||
// under JDK 7. See http://bugs.sun.com/view_bug.do?bug_id=7023180
|
// under JDK 7. See http://bugs.sun.com/view_bug.do?bug_id=7023180
|
||||||
Collections.sort(matches, new Comparator<Method>() {
|
Collections.sort(matches, new Comparator<Method>() {
|
||||||
@@ -437,24 +437,24 @@ class SimpleIndexedPropertyDescriptor extends IndexedPropertyDescriptor {
|
|||||||
* See java.beans.IndexedPropertyDescriptor#equals(java.lang.Object)
|
* See java.beans.IndexedPropertyDescriptor#equals(java.lang.Object)
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object obj) {
|
public boolean equals(Object other) {
|
||||||
if (this == obj) {
|
if (this == other) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (obj != null && obj instanceof IndexedPropertyDescriptor) {
|
if (!(other instanceof IndexedPropertyDescriptor)) {
|
||||||
IndexedPropertyDescriptor other = (IndexedPropertyDescriptor) obj;
|
return false;
|
||||||
if (!compareMethods(getIndexedReadMethod(), other.getIndexedReadMethod())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!compareMethods(getIndexedWriteMethod(), other.getIndexedWriteMethod())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (getIndexedPropertyType() != other.getIndexedPropertyType()) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return PropertyDescriptorUtils.equals(this, obj);
|
|
||||||
}
|
}
|
||||||
return false;
|
IndexedPropertyDescriptor otherPd = (IndexedPropertyDescriptor) other;
|
||||||
|
if (!compareMethods(getIndexedReadMethod(), otherPd.getIndexedReadMethod())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!compareMethods(getIndexedWriteMethod(), otherPd.getIndexedWriteMethod())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (getIndexedPropertyType() != otherPd.getIndexedPropertyType()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return PropertyDescriptorUtils.equals(this, other);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -595,25 +595,23 @@ class PropertyDescriptorUtils {
|
|||||||
* editor and flags are equivalent.
|
* editor and flags are equivalent.
|
||||||
* @see PropertyDescriptor#equals(Object)
|
* @see PropertyDescriptor#equals(Object)
|
||||||
*/
|
*/
|
||||||
public static boolean equals(PropertyDescriptor pd1, Object obj) {
|
public static boolean equals(PropertyDescriptor pd, Object other) {
|
||||||
if (pd1 == obj) {
|
if (pd == other) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (obj != null && obj instanceof PropertyDescriptor) {
|
if (!(other instanceof PropertyDescriptor)) {
|
||||||
PropertyDescriptor pd2 = (PropertyDescriptor) obj;
|
return false;
|
||||||
if (!compareMethods(pd1.getReadMethod(), pd2.getReadMethod())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (!compareMethods(pd1.getWriteMethod(), pd2.getWriteMethod())) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
if (pd1.getPropertyType() == pd2.getPropertyType() &&
|
|
||||||
pd1.getPropertyEditorClass() == pd2.getPropertyEditorClass() &&
|
|
||||||
pd1.isBound() == pd2.isBound() && pd1.isConstrained() == pd2.isConstrained()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return false;
|
PropertyDescriptor otherPd = (PropertyDescriptor) other;
|
||||||
|
if (!compareMethods(pd.getReadMethod(), otherPd.getReadMethod())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!compareMethods(pd.getWriteMethod(), otherPd.getWriteMethod())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return (pd.getPropertyType() == otherPd.getPropertyType() &&
|
||||||
|
pd.getPropertyEditorClass() == otherPd.getPropertyEditorClass() &&
|
||||||
|
pd.isBound() == otherPd.isBound() && pd.isConstrained() == otherPd.isConstrained());
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2014 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.
|
||||||
@@ -101,6 +101,7 @@ public class ConfigurationClassPostConstructAndAutowiringTests {
|
|||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
static class Config2 {
|
static class Config2 {
|
||||||
|
|
||||||
TestBean testBean;
|
TestBean testBean;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import java.lang.annotation.RetentionPolicy;
|
|||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
@@ -47,7 +48,6 @@ public class ImportBeanDefinitionRegistrarTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldInvokeAwareMethodsInImportBeanDefinitionRegistrar() {
|
public void shouldInvokeAwareMethodsInImportBeanDefinitionRegistrar() {
|
||||||
|
|
||||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
|
||||||
context.getBean(MessageSource.class);
|
context.getBean(MessageSource.class);
|
||||||
|
|
||||||
@@ -57,19 +57,20 @@ public class ImportBeanDefinitionRegistrarTests {
|
|||||||
assertThat(SampleRegistrar.environment, is((Environment) context.getEnvironment()));
|
assertThat(SampleRegistrar.environment, is((Environment) context.getEnvironment()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Sample
|
@Sample
|
||||||
@Configuration
|
@Configuration
|
||||||
static class Config {
|
static class Config {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Target(ElementType.TYPE)
|
@Target(ElementType.TYPE)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Import(SampleRegistrar.class)
|
@Import(SampleRegistrar.class)
|
||||||
public static @interface Sample {
|
public static @interface Sample {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static class SampleRegistrar implements ImportBeanDefinitionRegistrar, BeanClassLoaderAware, ResourceLoaderAware,
|
static class SampleRegistrar implements ImportBeanDefinitionRegistrar, BeanClassLoaderAware, ResourceLoaderAware,
|
||||||
BeanFactoryAware, EnvironmentAware {
|
BeanFactoryAware, EnvironmentAware {
|
||||||
|
|
||||||
@@ -102,4 +103,5 @@ public class ImportBeanDefinitionRegistrarTests {
|
|||||||
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
|
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2013 the original author or authors.
|
* Copyright 2002-2014 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.
|
||||||
@@ -20,9 +20,14 @@ import java.lang.annotation.ElementType;
|
|||||||
import java.lang.annotation.Retention;
|
import java.lang.annotation.Retention;
|
||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
import org.hamcrest.Matcher;
|
||||||
|
import org.junit.BeforeClass;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.InOrder;
|
import org.mockito.InOrder;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
@@ -41,7 +46,8 @@ import org.springframework.core.type.AnnotationMetadata;
|
|||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.*;
|
import static org.hamcrest.CoreMatchers.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import static org.mockito.Matchers.*;
|
import static org.mockito.Matchers.anyObject;
|
||||||
|
import static org.mockito.Matchers.eq;
|
||||||
import static org.mockito.Mockito.*;
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,11 +57,19 @@ import static org.mockito.Mockito.*;
|
|||||||
*/
|
*/
|
||||||
public class ImportSelectorTests {
|
public class ImportSelectorTests {
|
||||||
|
|
||||||
|
static Map<Class<?>, String> importFrom = new HashMap<Class<?>, String>();
|
||||||
|
|
||||||
|
|
||||||
|
@BeforeClass
|
||||||
|
public static void clearImportFrom() {
|
||||||
|
ImportSelectorTests.importFrom.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void importSelectors() {
|
public void importSelectors() {
|
||||||
DefaultListableBeanFactory beanFactory = spy(new DefaultListableBeanFactory());
|
DefaultListableBeanFactory beanFactory = spy(new DefaultListableBeanFactory());
|
||||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(beanFactory);
|
||||||
beanFactory);
|
|
||||||
context.register(Config.class);
|
context.register(Config.class);
|
||||||
context.refresh();
|
context.refresh();
|
||||||
context.getBean(Config.class);
|
context.getBean(Config.class);
|
||||||
@@ -68,22 +82,31 @@ public class ImportSelectorTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void invokeAwareMethodsInImportSelector() {
|
public void invokeAwareMethodsInImportSelector() {
|
||||||
|
|
||||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);
|
||||||
context.getBean(MessageSource.class);
|
context.getBean(MessageSource.class);
|
||||||
|
|
||||||
assertThat(SampleRegistrar.beanFactory, is((BeanFactory) context.getBeanFactory()));
|
assertThat(SampleRegistrar.beanFactory, is((BeanFactory) context.getBeanFactory()));
|
||||||
assertThat(SampleRegistrar.classLoader, is(context.getBeanFactory().getBeanClassLoader()));
|
assertThat(SampleRegistrar.classLoader, is(context.getBeanFactory().getBeanClassLoader()));
|
||||||
assertThat(SampleRegistrar.resourceLoader, is(notNullValue()));
|
assertThat(SampleRegistrar.resourceLoader, is(notNullValue()));
|
||||||
assertThat(SampleRegistrar.environment, is((Environment) context.getEnvironment()));
|
assertThat(SampleRegistrar.environment, is((Environment) context.getEnvironment()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void correctMetaDataOnIndirectImports() throws Exception {
|
||||||
|
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(IndirectConfig.class);
|
||||||
|
Matcher<String> isFromIndirect = equalTo(IndirectImport.class.getName());
|
||||||
|
assertThat(importFrom.get(ImportSelector1.class), isFromIndirect);
|
||||||
|
assertThat(importFrom.get(ImportSelector2.class), isFromIndirect);
|
||||||
|
assertThat(importFrom.get(DeferredImportSelector1.class), isFromIndirect);
|
||||||
|
assertThat(importFrom.get(DeferredImportSelector2.class), isFromIndirect);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@Import(SampleImportSelector.class)
|
@Import(SampleImportSelector.class)
|
||||||
static class AwareConfig {
|
static class AwareConfig {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static class SampleImportSelector implements ImportSelector, BeanClassLoaderAware, ResourceLoaderAware,
|
static class SampleImportSelector implements ImportSelector, BeanClassLoaderAware, ResourceLoaderAware,
|
||||||
BeanFactoryAware, EnvironmentAware {
|
BeanFactoryAware, EnvironmentAware {
|
||||||
|
|
||||||
@@ -118,38 +141,45 @@ public class ImportSelectorTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Sample
|
@Sample
|
||||||
@Configuration
|
@Configuration
|
||||||
static class Config {
|
static class Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Target(ElementType.TYPE)
|
@Target(ElementType.TYPE)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Import({ DeferredImportSelector1.class, DeferredImportSelector2.class,
|
@Import({DeferredImportSelector1.class, DeferredImportSelector2.class, ImportSelector1.class, ImportSelector2.class})
|
||||||
ImportSelector1.class, ImportSelector2.class })
|
|
||||||
public static @interface Sample {
|
public static @interface Sample {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class ImportSelector1 implements ImportSelector {
|
public static class ImportSelector1 implements ImportSelector {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
||||||
|
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
|
||||||
return new String[] { ImportedSelector1.class.getName() };
|
return new String[] { ImportedSelector1.class.getName() };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class ImportSelector2 implements ImportSelector {
|
public static class ImportSelector2 implements ImportSelector {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
||||||
|
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
|
||||||
return new String[] { ImportedSelector2.class.getName() };
|
return new String[] { ImportedSelector2.class.getName() };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static class DeferredImportSelector1 implements DeferredImportSelector, Ordered {
|
public static class DeferredImportSelector1 implements DeferredImportSelector, Ordered {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
||||||
|
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
|
||||||
return new String[] { DeferredImportedSelector1.class.getName() };
|
return new String[] { DeferredImportedSelector1.class.getName() };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,16 +189,18 @@ public class ImportSelectorTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||||
public static class DeferredImportSelector2 implements DeferredImportSelector {
|
public static class DeferredImportSelector2 implements DeferredImportSelector {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
||||||
|
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
|
||||||
return new String[] { DeferredImportedSelector2.class.getName() };
|
return new String[] { DeferredImportedSelector2.class.getName() };
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public static class ImportedSelector1 {
|
public static class ImportedSelector1 {
|
||||||
|
|
||||||
@@ -178,6 +210,7 @@ public class ImportSelectorTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public static class ImportedSelector2 {
|
public static class ImportedSelector2 {
|
||||||
|
|
||||||
@@ -187,6 +220,7 @@ public class ImportSelectorTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public static class DeferredImportedSelector1 {
|
public static class DeferredImportedSelector1 {
|
||||||
|
|
||||||
@@ -196,6 +230,7 @@ public class ImportSelectorTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
public static class DeferredImportedSelector2 {
|
public static class DeferredImportedSelector2 {
|
||||||
|
|
||||||
@@ -205,4 +240,24 @@ public class ImportSelectorTests {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@Import(IndirectImportSelector.class)
|
||||||
|
public static class IndirectConfig {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class IndirectImportSelector implements ImportSelector {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
||||||
|
return new String[] {IndirectImport.class.getName()};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Sample
|
||||||
|
public static class IndirectImport {
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,26 +87,11 @@ import static org.springframework.core.env.EnvironmentIntegrationTests.Constants
|
|||||||
public class EnvironmentIntegrationTests {
|
public class EnvironmentIntegrationTests {
|
||||||
|
|
||||||
private ConfigurableEnvironment prodEnv;
|
private ConfigurableEnvironment prodEnv;
|
||||||
|
|
||||||
private ConfigurableEnvironment devEnv;
|
private ConfigurableEnvironment devEnv;
|
||||||
|
|
||||||
private ConfigurableEnvironment prodWebEnv;
|
private ConfigurableEnvironment prodWebEnv;
|
||||||
|
|
||||||
/**
|
|
||||||
* Constants used both locally and in scan* sub-packages
|
|
||||||
*/
|
|
||||||
public static class Constants {
|
|
||||||
public static final String XML_PATH = "org/springframework/core/env/EnvironmentIntegrationTests-context.xml";
|
|
||||||
|
|
||||||
public static final String ENVIRONMENT_AWARE_BEAN_NAME = "envAwareBean";
|
|
||||||
|
|
||||||
public static final String PROD_BEAN_NAME = "prodBean";
|
|
||||||
public static final String DEV_BEAN_NAME = "devBean";
|
|
||||||
public static final String DERIVED_DEV_BEAN_NAME = "derivedDevBean";
|
|
||||||
public static final String TRANSITIVE_BEAN_NAME = "transitiveBean";
|
|
||||||
|
|
||||||
public static final String PROD_ENV_NAME = "prod";
|
|
||||||
public static final String DEV_ENV_NAME = "dev";
|
|
||||||
public static final String DERIVED_DEV_ENV_NAME = "derivedDev";
|
|
||||||
}
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
@@ -122,9 +107,7 @@ public class EnvironmentIntegrationTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void genericApplicationContext_standardEnv() {
|
public void genericApplicationContext_standardEnv() {
|
||||||
ConfigurableApplicationContext ctx =
|
ConfigurableApplicationContext ctx = new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
|
||||||
new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
|
|
||||||
|
|
||||||
ctx.refresh();
|
ctx.refresh();
|
||||||
|
|
||||||
assertHasStandardEnvironment(ctx);
|
assertHasStandardEnvironment(ctx);
|
||||||
@@ -134,8 +117,7 @@ public class EnvironmentIntegrationTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void genericApplicationContext_customEnv() {
|
public void genericApplicationContext_customEnv() {
|
||||||
GenericApplicationContext ctx =
|
GenericApplicationContext ctx = new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
|
||||||
new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
|
|
||||||
ctx.setEnvironment(prodEnv);
|
ctx.setEnvironment(prodEnv);
|
||||||
ctx.refresh();
|
ctx.refresh();
|
||||||
|
|
||||||
@@ -191,11 +173,8 @@ public class EnvironmentIntegrationTests {
|
|||||||
@Test
|
@Test
|
||||||
public void genericXmlApplicationContext() {
|
public void genericXmlApplicationContext() {
|
||||||
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
|
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
|
||||||
|
|
||||||
assertHasStandardEnvironment(ctx);
|
assertHasStandardEnvironment(ctx);
|
||||||
|
|
||||||
ctx.setEnvironment(prodEnv);
|
ctx.setEnvironment(prodEnv);
|
||||||
|
|
||||||
ctx.load(XML_PATH);
|
ctx.load(XML_PATH);
|
||||||
ctx.refresh();
|
ctx.refresh();
|
||||||
|
|
||||||
@@ -208,8 +187,7 @@ public class EnvironmentIntegrationTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void classPathXmlApplicationContext() {
|
public void classPathXmlApplicationContext() {
|
||||||
ConfigurableApplicationContext ctx =
|
ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(XML_PATH);
|
||||||
new ClassPathXmlApplicationContext(new String[] { XML_PATH });
|
|
||||||
ctx.setEnvironment(prodEnv);
|
ctx.setEnvironment(prodEnv);
|
||||||
ctx.refresh();
|
ctx.refresh();
|
||||||
|
|
||||||
@@ -228,7 +206,7 @@ public class EnvironmentIntegrationTests {
|
|||||||
|
|
||||||
// strange - FSXAC strips leading '/' unless prefixed with 'file:'
|
// strange - FSXAC strips leading '/' unless prefixed with 'file:'
|
||||||
ConfigurableApplicationContext ctx =
|
ConfigurableApplicationContext ctx =
|
||||||
new FileSystemXmlApplicationContext(new String[] { "file:"+tmpFile.getPath() }, false);
|
new FileSystemXmlApplicationContext(new String[] {"file:" + tmpFile.getPath()}, false);
|
||||||
ctx.setEnvironment(prodEnv);
|
ctx.setEnvironment(prodEnv);
|
||||||
ctx.refresh();
|
ctx.refresh();
|
||||||
assertEnvironmentBeanRegistered(ctx);
|
assertEnvironmentBeanRegistered(ctx);
|
||||||
@@ -336,11 +314,8 @@ public class EnvironmentIntegrationTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void webApplicationContext() {
|
public void webApplicationContext() {
|
||||||
GenericWebApplicationContext ctx =
|
GenericWebApplicationContext ctx = new GenericWebApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
|
||||||
new GenericWebApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
|
|
||||||
|
|
||||||
assertHasStandardServletEnvironment(ctx);
|
assertHasStandardServletEnvironment(ctx);
|
||||||
|
|
||||||
ctx.setEnvironment(prodWebEnv);
|
ctx.setEnvironment(prodWebEnv);
|
||||||
ctx.refresh();
|
ctx.refresh();
|
||||||
|
|
||||||
@@ -588,7 +563,8 @@ public class EnvironmentIntegrationTests {
|
|||||||
try {
|
try {
|
||||||
ctx.refresh();
|
ctx.refresh();
|
||||||
fail("expected missing property exception");
|
fail("expected missing property exception");
|
||||||
} catch (MissingRequiredPropertiesException ex) {
|
}
|
||||||
|
catch (MissingRequiredPropertiesException ex) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -598,7 +574,6 @@ public class EnvironmentIntegrationTests {
|
|||||||
ctx.setEnvironment(new MockEnvironment().withProperty("foo", "fooValue"));
|
ctx.setEnvironment(new MockEnvironment().withProperty("foo", "fooValue"));
|
||||||
ctx.refresh(); // should succeed
|
ctx.refresh(); // should succeed
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -652,6 +627,7 @@ public class EnvironmentIntegrationTests {
|
|||||||
assertThat(ctx.getBean(EnvironmentAwareBean.class).environment, is(expectedEnv));
|
assertThat(ctx.getBean(EnvironmentAwareBean.class).environment, is(expectedEnv));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class EnvironmentAwareBean implements EnvironmentAware {
|
private static class EnvironmentAwareBean implements EnvironmentAware {
|
||||||
|
|
||||||
public Environment environment;
|
public Environment environment;
|
||||||
@@ -660,9 +636,9 @@ public class EnvironmentIntegrationTests {
|
|||||||
public void setEnvironment(Environment environment) {
|
public void setEnvironment(Environment environment) {
|
||||||
this.environment = environment;
|
this.environment = environment;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Mirrors the structure of beans and environment-specific config files
|
* Mirrors the structure of beans and environment-specific config files
|
||||||
* in EnvironmentIntegrationTests-context.xml
|
* in EnvironmentIntegrationTests-context.xml
|
||||||
@@ -711,4 +687,25 @@ public class EnvironmentIntegrationTests {
|
|||||||
return new Object();
|
return new Object();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constants used both locally and in scan* sub-packages
|
||||||
|
*/
|
||||||
|
public static class Constants {
|
||||||
|
|
||||||
|
public static final String XML_PATH = "org/springframework/core/env/EnvironmentIntegrationTests-context.xml";
|
||||||
|
|
||||||
|
public static final String ENVIRONMENT_AWARE_BEAN_NAME = "envAwareBean";
|
||||||
|
|
||||||
|
public static final String PROD_BEAN_NAME = "prodBean";
|
||||||
|
public static final String DEV_BEAN_NAME = "devBean";
|
||||||
|
public static final String DERIVED_DEV_BEAN_NAME = "derivedDevBean";
|
||||||
|
public static final String TRANSITIVE_BEAN_NAME = "transitiveBean";
|
||||||
|
|
||||||
|
public static final String PROD_ENV_NAME = "prod";
|
||||||
|
public static final String DEV_ENV_NAME = "dev";
|
||||||
|
public static final String DERIVED_DEV_ENV_NAME = "derivedDev";
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user