Polishing

This commit is contained in:
Juergen Hoeller 2014-09-02 22:15:53 +02:00
parent cfd01ab100
commit 98eb0f75bc
6 changed files with 56 additions and 54 deletions

View File

@ -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;
* 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

View File

@ -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;
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;
* @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
@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
@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("]");

View File

@ -16,9 +16,6 @@
package org.springframework.cache.interceptor;
import static org.junit.Assert.*;
import static org.springframework.cache.CacheTestUtils.*;
import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
@ -26,6 +23,7 @@ import java.util.concurrent.atomic.AtomicLong;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
@ -40,12 +38,16 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ReflectionUtils;
import static org.junit.Assert.*;
import static org.springframework.cache.CacheTestUtils.*;
/**
* Provides various {@link CacheResolver} customisations scenario
*
* @author Stephane Nicoll
* @since 4.1
*/
public class CacheResolverCustomisationTests {
public class CacheResolverCustomizationTests {
private CacheManager cacheManager;
@ -127,13 +129,13 @@ public class CacheResolverCustomisationTests {
@Test
public void noCacheResolved() {
Method m = ReflectionUtils.findMethod(SimpleService.class, "noCacheResolved", Object.class);
Method method = ReflectionUtils.findMethod(SimpleService.class, "noCacheResolved", Object.class);
try {
simpleService.noCacheResolved(new Object());
fail("Should have failed, no cache resolved");
} catch (IllegalStateException e) {
String msg = e.getMessage();
assertTrue("Reference to the method must be contained in the message", msg.contains(m.toString()));
}
catch (IllegalStateException ex) {
assertTrue("Reference to the method must be contained in the message", ex.getMessage().contains(method.toString()));
}
}
@ -142,13 +144,13 @@ public class CacheResolverCustomisationTests {
try {
simpleService.unknownCacheResolver(new Object());
fail("Should have failed, no cache resolver with that name");
} catch (NoSuchBeanDefinitionException e) {
assertEquals("Wrong bean name in exception", "unknownCacheResolver", e.getBeanName());
}
catch (NoSuchBeanDefinitionException ex) {
assertEquals("Wrong bean name in exception", "unknownCacheResolver", ex.getBeanName());
}
}
@Configuration
@EnableCaching
static class Config extends CachingConfigurerSupport {
@ -204,6 +206,7 @@ public class CacheResolverCustomisationTests {
}
}
@CacheConfig(cacheNames = "default")
static class SimpleService {
@ -245,6 +248,7 @@ public class CacheResolverCustomisationTests {
}
}
/**
* Example of {@link CacheResolver} that resolve the caches at
* runtime (i.e. based on method invocation parameters).
@ -263,6 +267,7 @@ public class CacheResolverCustomisationTests {
}
}
private static class NullCacheResolver extends AbstractCacheResolver {
private NullCacheResolver(CacheManager cacheManager) {

View File

@ -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 {
@Configuration
static class Config2 {
TestBean testBean;
@Autowired

View File

@ -258,11 +258,10 @@ public class JmsListenerAnnotationBeanPostProcessor
/**
* Resolve the specified value if possible.
*
* @see ConfigurableBeanFactory#resolveEmbeddedValue
*/
private String resolve(String value) {
if (this.beanFactory != null && this.beanFactory instanceof ConfigurableBeanFactory) {
if (this.beanFactory instanceof ConfigurableBeanFactory) {
return ((ConfigurableBeanFactory) this.beanFactory).resolveEmbeddedValue(value);
}
return value;

View File

@ -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";
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
public void setUp() {
@ -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 {
@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 {
@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 {
@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 {
// 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);
@ -588,7 +566,8 @@ public class EnvironmentIntegrationTests {
try {
ctx.refresh();
fail("expected missing property exception");
} catch (MissingRequiredPropertiesException ex) {
}
catch (MissingRequiredPropertiesException ex) {
}
}
@ -598,7 +577,6 @@ public class EnvironmentIntegrationTests {
ctx.setEnvironment(new MockEnvironment().withProperty("foo", "fooValue"));
ctx.refresh(); // should succeed
}
}
@ -652,6 +630,7 @@ public class EnvironmentIntegrationTests {
assertThat(ctx.getBean(EnvironmentAwareBean.class).environment, is(expectedEnv));
}
private static class EnvironmentAwareBean implements EnvironmentAware {
public Environment environment;
@ -660,9 +639,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 +690,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";
}
}