Browse Source

Polishing

pull/642/head
Juergen Hoeller 11 years ago
parent
commit
d2ef6dcb8d
  1. 4
      spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/target/LazyInitTargetSourceCreator.java
  2. 11
      spring-aop/src/main/java/org/springframework/aop/target/AbstractBeanFactoryBasedTargetSource.java
  3. 62
      spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java
  4. 3
      spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostConstructAndAutowiringTests.java
  5. 8
      spring-context/src/test/java/org/springframework/context/annotation/ImportBeanDefinitionRegistrarTests.java
  6. 75
      spring-context/src/test/java/org/springframework/context/annotation/ImportSelectorTests.java
  7. 67
      src/test/java/org/springframework/core/env/EnvironmentIntegrationTests.java

4
spring-aop/src/main/java/org/springframework/aop/framework/autoproxy/target/LazyInitTargetSourceCreator.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -25,7 +25,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; @@ -25,7 +25,7 @@ import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
* TargetSourceCreator that enforces a LazyInitTargetSource for each bean
* 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
* 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,
* in combination with custom interceptors for specific beans or for the

11
spring-aop/src/main/java/org/springframework/aop/target/AbstractBeanFactoryBasedTargetSource.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -24,7 +24,6 @@ import org.apache.commons.logging.LogFactory; @@ -24,7 +24,6 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.aop.TargetSource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
/**
@ -48,8 +47,7 @@ import org.springframework.util.ObjectUtils; @@ -48,8 +47,7 @@ import org.springframework.util.ObjectUtils;
* @see ThreadLocalTargetSource
* @see CommonsPoolTargetSource
*/
public abstract class AbstractBeanFactoryBasedTargetSource
implements TargetSource, BeanFactoryAware, Serializable {
public abstract class AbstractBeanFactoryBasedTargetSource implements TargetSource, BeanFactoryAware, Serializable {
/** use serialVersionUID from Spring 1.2.7 for interoperability */
private static final long serialVersionUID = -4721607536018568393L;
@ -108,7 +106,7 @@ public abstract class AbstractBeanFactoryBasedTargetSource @@ -108,7 +106,7 @@ public abstract class AbstractBeanFactoryBasedTargetSource
@Override
public void setBeanFactory(BeanFactory beanFactory) {
if (this.targetBeanName == null) {
throw new IllegalStateException("Property'targetBeanName' is required");
throw new IllegalStateException("Property 'targetBeanName' is required");
}
this.beanFactory = beanFactory;
}
@ -185,8 +183,7 @@ public abstract class AbstractBeanFactoryBasedTargetSource @@ -185,8 +183,7 @@ public abstract class AbstractBeanFactoryBasedTargetSource
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(ClassUtils.getShortName(getClass()));
StringBuilder sb = new StringBuilder(getClass().getSimpleName());
sb.append(" for target bean '").append(this.targetBeanName).append("'");
if (this.targetClass != null) {
sb.append(" of type [").append(this.targetClass.getName()).append("]");

62
spring-beans/src/main/java/org/springframework/beans/ExtendedBeanInfo.java

@ -115,7 +115,7 @@ class ExtendedBeanInfo implements BeanInfo { @@ -115,7 +115,7 @@ class ExtendedBeanInfo implements BeanInfo {
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
// under JDK 7. See http://bugs.sun.com/view_bug.do?bug_id=7023180
Collections.sort(matches, new Comparator<Method>() {
@ -437,24 +437,24 @@ class SimpleIndexedPropertyDescriptor extends IndexedPropertyDescriptor { @@ -437,24 +437,24 @@ class SimpleIndexedPropertyDescriptor extends IndexedPropertyDescriptor {
* See java.beans.IndexedPropertyDescriptor#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj) {
public boolean equals(Object other) {
if (this == other) {
return true;
}
if (obj != null && obj instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor other = (IndexedPropertyDescriptor) obj;
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);
if (!(other instanceof IndexedPropertyDescriptor)) {
return false;
}
IndexedPropertyDescriptor otherPd = (IndexedPropertyDescriptor) other;
if (!compareMethods(getIndexedReadMethod(), otherPd.getIndexedReadMethod())) {
return false;
}
if (!compareMethods(getIndexedWriteMethod(), otherPd.getIndexedWriteMethod())) {
return false;
}
return false;
if (getIndexedPropertyType() != otherPd.getIndexedPropertyType()) {
return false;
}
return PropertyDescriptorUtils.equals(this, other);
}
@Override
@ -595,25 +595,23 @@ class PropertyDescriptorUtils { @@ -595,25 +595,23 @@ class PropertyDescriptorUtils {
* editor and flags are equivalent.
* @see PropertyDescriptor#equals(Object)
*/
public static boolean equals(PropertyDescriptor pd1, Object obj) {
if (pd1 == obj) {
public static boolean equals(PropertyDescriptor pd, Object other) {
if (pd == other) {
return true;
}
if (obj != null && obj instanceof PropertyDescriptor) {
PropertyDescriptor pd2 = (PropertyDescriptor) obj;
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;
}
if (!(other instanceof PropertyDescriptor)) {
return false;
}
PropertyDescriptor otherPd = (PropertyDescriptor) other;
if (!compareMethods(pd.getReadMethod(), otherPd.getReadMethod())) {
return false;
}
if (!compareMethods(pd.getWriteMethod(), otherPd.getWriteMethod())) {
return false;
}
return false;
return (pd.getPropertyType() == otherPd.getPropertyType() &&
pd.getPropertyEditorClass() == otherPd.getPropertyEditorClass() &&
pd.isBound() == otherPd.isBound() && pd.isConstrained() == otherPd.isConstrained());
}
/*

3
spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostConstructAndAutowiringTests.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -101,6 +101,7 @@ public class ConfigurationClassPostConstructAndAutowiringTests { @@ -101,6 +101,7 @@ public class ConfigurationClassPostConstructAndAutowiringTests {
@Configuration
static class Config2 {
TestBean testBean;
@Autowired

8
spring-context/src/test/java/org/springframework/context/annotation/ImportBeanDefinitionRegistrarTests.java

@ -22,6 +22,7 @@ import java.lang.annotation.RetentionPolicy; @@ -22,6 +22,7 @@ import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
@ -47,7 +48,6 @@ public class ImportBeanDefinitionRegistrarTests { @@ -47,7 +48,6 @@ public class ImportBeanDefinitionRegistrarTests {
@Test
public void shouldInvokeAwareMethodsInImportBeanDefinitionRegistrar() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class);
context.getBean(MessageSource.class);
@ -57,19 +57,20 @@ public class ImportBeanDefinitionRegistrarTests { @@ -57,19 +57,20 @@ public class ImportBeanDefinitionRegistrarTests {
assertThat(SampleRegistrar.environment, is((Environment) context.getEnvironment()));
}
@Sample
@Configuration
static class Config {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(SampleRegistrar.class)
public static @interface Sample {
}
static class SampleRegistrar implements ImportBeanDefinitionRegistrar, BeanClassLoaderAware, ResourceLoaderAware,
BeanFactoryAware, EnvironmentAware {
@ -102,4 +103,5 @@ public class ImportBeanDefinitionRegistrarTests { @@ -102,4 +103,5 @@ public class ImportBeanDefinitionRegistrarTests {
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
}
}
}

75
spring-context/src/test/java/org/springframework/context/annotation/ImportSelectorTests.java

@ -1,5 +1,5 @@ @@ -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");
* you may not use this file except in compliance with the License.
@ -20,9 +20,14 @@ import java.lang.annotation.ElementType; @@ -20,9 +20,14 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
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.mockito.InOrder;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
@ -41,7 +46,8 @@ import org.springframework.core.type.AnnotationMetadata; @@ -41,7 +46,8 @@ import org.springframework.core.type.AnnotationMetadata;
import static org.hamcrest.CoreMatchers.*;
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.*;
/**
@ -51,11 +57,19 @@ import static org.mockito.Mockito.*; @@ -51,11 +57,19 @@ import static org.mockito.Mockito.*;
*/
public class ImportSelectorTests {
static Map<Class<?>, String> importFrom = new HashMap<Class<?>, String>();
@BeforeClass
public static void clearImportFrom() {
ImportSelectorTests.importFrom.clear();
}
@Test
public void importSelectors() {
DefaultListableBeanFactory beanFactory = spy(new DefaultListableBeanFactory());
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
beanFactory);
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(beanFactory);
context.register(Config.class);
context.refresh();
context.getBean(Config.class);
@ -68,22 +82,31 @@ public class ImportSelectorTests { @@ -68,22 +82,31 @@ public class ImportSelectorTests {
@Test
public void invokeAwareMethodsInImportSelector() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AwareConfig.class);
context.getBean(MessageSource.class);
assertThat(SampleRegistrar.beanFactory, is((BeanFactory) context.getBeanFactory()));
assertThat(SampleRegistrar.classLoader, is(context.getBeanFactory().getBeanClassLoader()));
assertThat(SampleRegistrar.resourceLoader, is(notNullValue()));
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
@Import(SampleImportSelector.class)
static class AwareConfig {
}
static class SampleImportSelector implements ImportSelector, BeanClassLoaderAware, ResourceLoaderAware,
BeanFactoryAware, EnvironmentAware {
@ -118,38 +141,45 @@ public class ImportSelectorTests { @@ -118,38 +141,45 @@ public class ImportSelectorTests {
}
}
@Sample
@Configuration
static class Config {
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import({ DeferredImportSelector1.class, DeferredImportSelector2.class,
ImportSelector1.class, ImportSelector2.class })
@Import({DeferredImportSelector1.class, DeferredImportSelector2.class, ImportSelector1.class, ImportSelector2.class})
public static @interface Sample {
}
public static class ImportSelector1 implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
return new String[] { ImportedSelector1.class.getName() };
}
}
public static class ImportSelector2 implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
return new String[] { ImportedSelector2.class.getName() };
}
}
public static class DeferredImportSelector1 implements DeferredImportSelector, Ordered {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
return new String[] { DeferredImportedSelector1.class.getName() };
}
@ -159,16 +189,18 @@ public class ImportSelectorTests { @@ -159,16 +189,18 @@ public class ImportSelectorTests {
}
}
@Order(Ordered.HIGHEST_PRECEDENCE)
public static class DeferredImportSelector2 implements DeferredImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
ImportSelectorTests.importFrom.put(getClass(), importingClassMetadata.getClassName());
return new String[] { DeferredImportedSelector2.class.getName() };
}
}
@Configuration
public static class ImportedSelector1 {
@ -178,6 +210,7 @@ public class ImportSelectorTests { @@ -178,6 +210,7 @@ public class ImportSelectorTests {
}
}
@Configuration
public static class ImportedSelector2 {
@ -187,6 +220,7 @@ public class ImportSelectorTests { @@ -187,6 +220,7 @@ public class ImportSelectorTests {
}
}
@Configuration
public static class DeferredImportedSelector1 {
@ -196,6 +230,7 @@ public class ImportSelectorTests { @@ -196,6 +230,7 @@ public class ImportSelectorTests {
}
}
@Configuration
public static class DeferredImportedSelector2 {
@ -205,4 +240,24 @@ public class ImportSelectorTests { @@ -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 {
}
}

67
src/test/java/org/springframework/core/env/EnvironmentIntegrationTests.java vendored

@ -87,26 +87,11 @@ import static org.springframework.core.env.EnvironmentIntegrationTests.Constants @@ -87,26 +87,11 @@ import static org.springframework.core.env.EnvironmentIntegrationTests.Constants
public class EnvironmentIntegrationTests {
private ConfigurableEnvironment prodEnv;
private ConfigurableEnvironment devEnv;
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";
private ConfigurableEnvironment devEnv;
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";
private ConfigurableEnvironment prodWebEnv;
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
public void setUp() {
@ -122,9 +107,7 @@ public class EnvironmentIntegrationTests { @@ -122,9 +107,7 @@ public class EnvironmentIntegrationTests {
@Test
public void genericApplicationContext_standardEnv() {
ConfigurableApplicationContext ctx =
new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
ConfigurableApplicationContext ctx = new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
ctx.refresh();
assertHasStandardEnvironment(ctx);
@ -134,8 +117,7 @@ public class EnvironmentIntegrationTests { @@ -134,8 +117,7 @@ public class EnvironmentIntegrationTests {
@Test
public void genericApplicationContext_customEnv() {
GenericApplicationContext ctx =
new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
GenericApplicationContext ctx = new GenericApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
ctx.setEnvironment(prodEnv);
ctx.refresh();
@ -191,11 +173,8 @@ public class EnvironmentIntegrationTests { @@ -191,11 +173,8 @@ public class EnvironmentIntegrationTests {
@Test
public void genericXmlApplicationContext() {
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
assertHasStandardEnvironment(ctx);
ctx.setEnvironment(prodEnv);
ctx.load(XML_PATH);
ctx.refresh();
@ -208,8 +187,7 @@ public class EnvironmentIntegrationTests { @@ -208,8 +187,7 @@ public class EnvironmentIntegrationTests {
@Test
public void classPathXmlApplicationContext() {
ConfigurableApplicationContext ctx =
new ClassPathXmlApplicationContext(new String[] { XML_PATH });
ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(XML_PATH);
ctx.setEnvironment(prodEnv);
ctx.refresh();
@ -228,7 +206,7 @@ public class EnvironmentIntegrationTests { @@ -228,7 +206,7 @@ public class EnvironmentIntegrationTests {
// strange - FSXAC strips leading '/' unless prefixed with 'file:'
ConfigurableApplicationContext ctx =
new FileSystemXmlApplicationContext(new String[] { "file:"+tmpFile.getPath() }, false);
new FileSystemXmlApplicationContext(new String[] {"file:" + tmpFile.getPath()}, false);
ctx.setEnvironment(prodEnv);
ctx.refresh();
assertEnvironmentBeanRegistered(ctx);
@ -336,11 +314,8 @@ public class EnvironmentIntegrationTests { @@ -336,11 +314,8 @@ public class EnvironmentIntegrationTests {
@Test
public void webApplicationContext() {
GenericWebApplicationContext ctx =
new GenericWebApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
GenericWebApplicationContext ctx = new GenericWebApplicationContext(newBeanFactoryWithEnvironmentAwareBean());
assertHasStandardServletEnvironment(ctx);
ctx.setEnvironment(prodWebEnv);
ctx.refresh();
@ -588,7 +563,8 @@ public class EnvironmentIntegrationTests { @@ -588,7 +563,8 @@ public class EnvironmentIntegrationTests {
try {
ctx.refresh();
fail("expected missing property exception");
} catch (MissingRequiredPropertiesException ex) {
}
catch (MissingRequiredPropertiesException ex) {
}
}
@ -598,7 +574,6 @@ public class EnvironmentIntegrationTests { @@ -598,7 +574,6 @@ public class EnvironmentIntegrationTests {
ctx.setEnvironment(new MockEnvironment().withProperty("foo", "fooValue"));
ctx.refresh(); // should succeed
}
}
@ -652,6 +627,7 @@ public class EnvironmentIntegrationTests { @@ -652,6 +627,7 @@ public class EnvironmentIntegrationTests {
assertThat(ctx.getBean(EnvironmentAwareBean.class).environment, is(expectedEnv));
}
private static class EnvironmentAwareBean implements EnvironmentAware {
public Environment environment;
@ -660,9 +636,9 @@ public class EnvironmentIntegrationTests { @@ -660,9 +636,9 @@ public class EnvironmentIntegrationTests {
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}
/**
* Mirrors the structure of beans and environment-specific config files
* in EnvironmentIntegrationTests-context.xml
@ -711,4 +687,25 @@ public class EnvironmentIntegrationTests { @@ -711,4 +687,25 @@ public class EnvironmentIntegrationTests {
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";
}
}

Loading…
Cancel
Save