Browse Source

Polishing

pull/639/head
Juergen Hoeller 12 years ago
parent
commit
98eb0f75bc
  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. 27
      spring-context/src/test/java/org/springframework/cache/interceptor/CacheResolverCustomizationTests.java
  4. 3
      spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostConstructAndAutowiringTests.java
  5. 3
      spring-jms/src/main/java/org/springframework/jms/annotation/JmsListenerAnnotationBeanPostProcessor.java
  6. 62
      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 @@
/* /*
* 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

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

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

27
spring-context/src/test/java/org/springframework/cache/interceptor/CacheResolverCustomisationTests.java → spring-context/src/test/java/org/springframework/cache/interceptor/CacheResolverCustomizationTests.java vendored

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

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

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

3
spring-jms/src/main/java/org/springframework/jms/annotation/JmsListenerAnnotationBeanPostProcessor.java

@ -258,11 +258,10 @@ public class JmsListenerAnnotationBeanPostProcessor
/** /**
* Resolve the specified value if possible. * Resolve the specified value if possible.
*
* @see ConfigurableBeanFactory#resolveEmbeddedValue * @see ConfigurableBeanFactory#resolveEmbeddedValue
*/ */
private String resolve(String value) { 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 ((ConfigurableBeanFactory) this.beanFactory).resolveEmbeddedValue(value);
} }
return value; return value;

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

@ -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 prodWebEnv;
/** private ConfigurableEnvironment devEnv;
* 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"; private ConfigurableEnvironment prodWebEnv;
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);
@ -588,7 +566,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 +577,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 +630,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 +639,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 +690,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";
}
} }

Loading…
Cancel
Save