Browse Source

Polishing

pull/418/head
Juergen Hoeller 11 years ago
parent
commit
f39bb02628
  1. 13
      spring-beans-groovy/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java
  2. 44
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java
  3. 14
      spring-context/src/main/java/org/springframework/scheduling/annotation/AbstractAsyncConfiguration.java
  4. 12
      spring-core/src/main/java/org/springframework/core/env/StandardEnvironment.java
  5. 14
      spring-tx/src/main/java/org/springframework/transaction/annotation/AbstractTransactionManagementConfiguration.java
  6. 7
      spring-tx/src/main/java/org/springframework/transaction/annotation/TransactionManagementConfigurationSelector.java

13
spring-beans-groovy/src/main/java/org/springframework/beans/factory/groovy/GroovyBeanDefinitionReader.java

@ -221,7 +221,6 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp @@ -221,7 +221,6 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
};
binding.setVariable("beans", beans);
int countBefore = getRegistry().getBeanDefinitionCount();
try {
GroovyShell shell = new GroovyShell(getResourceLoader().getClassLoader(), binding);
@ -239,11 +238,11 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp @@ -239,11 +238,11 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
/**
* Defines a set of beans for the given block or closure.
* @param c the block or closure
* @param closure the block or closure
* @return this GroovyBeanDefinitionReader instance
*/
public GroovyBeanDefinitionReader beans(Closure c) {
return invokeBeanDefiningClosure(c);
public GroovyBeanDefinitionReader beans(Closure closure) {
return invokeBeanDefiningClosure(closure);
}
/**
@ -433,7 +432,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp @@ -433,7 +432,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
private GroovyBeanDefinitionWrapper invokeBeanDefiningMethod(String beanName, Object[] args) {
boolean hasClosureArgument = args[args.length - 1] instanceof Closure;
if (args[0] instanceof Class) {
Class beanClass = (args[0] instanceof Class ? (Class) args[0] : args[0].getClass());
Class<?> beanClass = (args[0] instanceof Class ? (Class) args[0] : args[0].getClass());
if (args.length >= 1) {
if (hasClosureArgument) {
if (args.length-1 != 1) {
@ -593,7 +592,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp @@ -593,7 +592,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
GroovyBeanDefinitionWrapper current = this.currentBeanDefinition;
try {
Closure callable = (Closure) value;
Class parameterType = callable.getParameterTypes()[0];
Class<?> parameterType = callable.getParameterTypes()[0];
if (parameterType.equals(Object.class)) {
this.currentBeanDefinition = new GroovyBeanDefinitionWrapper("");
callable.call(this.currentBeanDefinition);
@ -689,7 +688,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp @@ -689,7 +688,7 @@ public class GroovyBeanDefinitionReader extends AbstractBeanDefinitionReader imp
private final String name;
private Object value;
public Object value;
public DeferredProperty(GroovyBeanDefinitionWrapper beanDefinition, String name, Object value) {
this.beanDefinition = beanDefinition;

44
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassEnhancer.java

@ -22,6 +22,7 @@ import java.util.Arrays; @@ -22,6 +22,7 @@ import java.util.Arrays;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.scope.ScopedProxyFactoryBean;
import org.springframework.asm.Type;
import org.springframework.beans.factory.BeanFactory;
@ -57,8 +58,6 @@ import org.springframework.util.ReflectionUtils; @@ -57,8 +58,6 @@ import org.springframework.util.ReflectionUtils;
*/
class ConfigurationClassEnhancer {
private static final Log logger = LogFactory.getLog(ConfigurationClassEnhancer.class);
private static final Callback[] CALLBACKS = new Callback[] {
new BeanMethodInterceptor(),
new DisposableBeanMethodInterceptor(),
@ -66,12 +65,13 @@ class ConfigurationClassEnhancer { @@ -66,12 +65,13 @@ class ConfigurationClassEnhancer {
NoOp.INSTANCE
};
private static final ConditionalCallbackFilter CALLBACK_FILTER =
new ConditionalCallbackFilter(CALLBACKS);
private static final ConditionalCallbackFilter CALLBACK_FILTER = new ConditionalCallbackFilter(CALLBACKS);
private static final String BEAN_FACTORY_FIELD = "$$beanFactory";
private static final Log logger = LogFactory.getLog(ConfigurationClassEnhancer.class);
/**
* Loads the specified class and generates a CGLIB subclass of it equipped with
* container-aware callbacks capable of respecting scoping and other bean semantics.
@ -125,12 +125,13 @@ class ConfigurationClassEnhancer { @@ -125,12 +125,13 @@ class ConfigurationClassEnhancer {
}
/**
* Uses enhancer to generate a subclass of superclass, ensuring that
* {@link #callbackInstances} are registered for the new subclass.
* Uses enhancer to generate a subclass of superclass,
* ensuring that callbacks are registered for the new subclass.
*/
private Class<?> createClass(Enhancer enhancer) {
Class<?> subclass = enhancer.createClass();
// registering callbacks statically (as opposed to threadlocal) is critical for usage in an OSGi env (SPR-5932)
// Registering callbacks statically (as opposed to thread-local)
// is critical for usage in an OSGi environment (SPR-5932)...
Enhancer.registerStaticCallbacks(subclass, CALLBACKS);
return subclass;
}
@ -162,6 +163,7 @@ class ConfigurationClassEnhancer { @@ -162,6 +163,7 @@ class ConfigurationClassEnhancer {
boolean isMatch(Method candidateMethod);
}
/**
* A {@link CallbackFilter} that works by interrogating {@link Callback}s in the order
* that they are defined via {@link ConditionalCallback}.
@ -197,14 +199,14 @@ class ConfigurationClassEnhancer { @@ -197,14 +199,14 @@ class ConfigurationClassEnhancer {
}
}
/**
* Intercepts the invocation of any {@link DisposableBean#destroy()} on @Configuration
* class instances for the purpose of de-registering CGLIB callbacks. This helps avoid
* garbage collection issues. See SPR-7901.
* @see EnhancedConfiguration
*/
private static class DisposableBeanMethodInterceptor implements MethodInterceptor,
ConditionalCallback {
private static class DisposableBeanMethodInterceptor implements MethodInterceptor, ConditionalCallback {
@Override
public boolean isMatch(Method candidateMethod) {
@ -231,23 +233,20 @@ class ConfigurationClassEnhancer { @@ -231,23 +233,20 @@ class ConfigurationClassEnhancer {
* Intercepts the invocation of any
* {@link BeanFactoryAware#setBeanFactory(BeanFactory)} on {@code @Configuration}
* class instances for the purpose of recording the {@link BeanFactory}.
*
* @see EnhancedConfiguration
*/
private static class BeanFactoryAwareMethodInterceptor implements MethodInterceptor,
ConditionalCallback {
private static class BeanFactoryAwareMethodInterceptor implements MethodInterceptor, ConditionalCallback {
@Override
public boolean isMatch(Method candidateMethod) {
return candidateMethod.getName().equals("setBeanFactory")
&& candidateMethod.getParameterTypes().length == 1
&& candidateMethod.getParameterTypes()[0].equals(BeanFactory.class)
&& BeanFactoryAware.class.isAssignableFrom(candidateMethod.getDeclaringClass());
return candidateMethod.getName().equals("setBeanFactory") &&
candidateMethod.getParameterTypes().length == 1 &&
candidateMethod.getParameterTypes()[0].equals(BeanFactory.class) &&
BeanFactoryAware.class.isAssignableFrom(candidateMethod.getDeclaringClass());
}
@Override
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
Field field = obj.getClass().getDeclaredField(BEAN_FACTORY_FIELD);
Assert.state(field != null, "Unable to find generated bean factory field");
field.set(obj, args[0]);
@ -396,8 +395,7 @@ class ConfigurationClassEnhancer { @@ -396,8 +395,7 @@ class ConfigurationClassEnhancer {
enhancer.setUseFactory(false);
enhancer.setCallback(new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args,
MethodProxy proxy) throws Throwable {
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
if (method.getName().equals("getObject") && args.length == 0) {
return beanFactory.getBean(beanName);
}
@ -411,8 +409,8 @@ class ConfigurationClassEnhancer { @@ -411,8 +409,8 @@ class ConfigurationClassEnhancer {
Field field = ReflectionUtils.findField(enhancedConfigInstance.getClass(), BEAN_FACTORY_FIELD);
Assert.state(field != null, "Unable to find generated bean factory field");
Object beanFactory = ReflectionUtils.getField(field, enhancedConfigInstance);
Assert.state(beanFactory != null, "The BeanFactory has not been injected into the @Configuration class");
Assert.state(beanFactory instanceof ConfigurableBeanFactory, "The injected BeanFactory is not a ConfigurableBeanFactory");
Assert.state(beanFactory != null, "BeanFactory has not been injected into @Configuration class");
Assert.state(beanFactory instanceof ConfigurableBeanFactory, "Injected BeanFactory is not a ConfigurableBeanFactory");
return (ConfigurableBeanFactory) beanFactory;
}
}

14
spring-context/src/main/java/org/springframework/scheduling/annotation/AbstractAsyncConfiguration.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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,6 +25,7 @@ import org.springframework.context.annotation.ImportAware; @@ -25,6 +25,7 @@ import org.springframework.context.annotation.ImportAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/**
* Abstract base {@code Configuration} class providing common structure for enabling
@ -38,15 +39,16 @@ import org.springframework.util.Assert; @@ -38,15 +39,16 @@ import org.springframework.util.Assert;
public abstract class AbstractAsyncConfiguration implements ImportAware {
protected AnnotationAttributes enableAsync;
protected Executor executor;
@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
this.enableAsync = AnnotationAttributes.fromMap(
importMetadata.getAnnotationAttributes(EnableAsync.class.getName(), false));
Assert.notNull(this.enableAsync,
"@EnableAsync is not present on importing class " +
importMetadata.getClassName());
"@EnableAsync is not present on importing class " + importMetadata.getClassName());
}
/**
@ -54,14 +56,12 @@ public abstract class AbstractAsyncConfiguration implements ImportAware { @@ -54,14 +56,12 @@ public abstract class AbstractAsyncConfiguration implements ImportAware {
*/
@Autowired(required = false)
void setConfigurers(Collection<AsyncConfigurer> configurers) {
if (configurers == null || configurers.isEmpty()) {
if (CollectionUtils.isEmpty(configurers)) {
return;
}
if (configurers.size() > 1) {
throw new IllegalStateException("only one AsyncConfigurer may exist");
throw new IllegalStateException("Only one AsyncConfigurer may exist");
}
AsyncConfigurer configurer = configurers.iterator().next();
this.executor = configurer.getAsyncExecutor();
}

12
spring-core/src/main/java/org/springframework/core/env/StandardEnvironment.java vendored

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2013 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,8 +24,8 @@ package org.springframework.core.env; @@ -24,8 +24,8 @@ package org.springframework.core.env;
* property resolution and profile-related operations, this implementation configures two
* default property sources, to be searched in the following order:
* <ul>
* <li>{@linkplain AbstractEnvironment#getSystemProperties() system properties}
* <li>{@linkplain AbstractEnvironment#getSystemEnvironment() system environment variables}
* <li>{@linkplain AbstractEnvironment#getSystemProperties() system properties}
* <li>{@linkplain AbstractEnvironment#getSystemEnvironment() system environment variables}
* </ul>
*
* That is, if the key "xyz" is present both in the JVM system properties as well as in
@ -41,7 +41,7 @@ package org.springframework.core.env; @@ -41,7 +41,7 @@ package org.springframework.core.env;
* instance available from {@link #getPropertySources()}. See
* {@link ConfigurableEnvironment} Javadoc for usage examples.
*
* <p>See {@link SystemEnvironmentPropertySource} Javadoc for details on special handling
* <p>See {@link SystemEnvironmentPropertySource} javadoc for details on special handling
* of property names in shell environments (e.g. Bash) that disallow period characters in
* variable names.
*
@ -61,8 +61,8 @@ public class StandardEnvironment extends AbstractEnvironment { @@ -61,8 +61,8 @@ public class StandardEnvironment extends AbstractEnvironment {
/**
* Customize the set of property sources with those appropriate for any standard Java
* environment:
* Customize the set of property sources with those appropriate for any standard
* Java environment:
* <ul>
* <li>{@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME}
* <li>{@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME}

14
spring-tx/src/main/java/org/springframework/transaction/annotation/AbstractTransactionManagementConfiguration.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2013 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,6 +25,7 @@ import org.springframework.core.annotation.AnnotationAttributes; @@ -25,6 +25,7 @@ import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/**
* Abstract base {@code @Configuration} class providing common structure for enabling
@ -38,27 +39,26 @@ import org.springframework.util.Assert; @@ -38,27 +39,26 @@ import org.springframework.util.Assert;
public abstract class AbstractTransactionManagementConfiguration implements ImportAware {
protected AnnotationAttributes enableTx;
protected PlatformTransactionManager txManager;
@Override
public void setImportMetadata(AnnotationMetadata importMetadata) {
this.enableTx = AnnotationAttributes.fromMap(
importMetadata.getAnnotationAttributes(EnableTransactionManagement.class.getName(), false));
Assert.notNull(this.enableTx,
"@EnableTransactionManagement is not present on importing class " +
importMetadata.getClassName());
"@EnableTransactionManagement is not present on importing class " + importMetadata.getClassName());
}
@Autowired(required=false)
void setConfigurers(Collection<TransactionManagementConfigurer> configurers) {
if (configurers == null || configurers.isEmpty()) {
if (CollectionUtils.isEmpty(configurers)) {
return;
}
if (configurers.size() > 1) {
throw new IllegalStateException("only one TransactionManagementConfigurer may exist");
throw new IllegalStateException("Only one TransactionManagementConfigurer may exist");
}
TransactionManagementConfigurer configurer = configurers.iterator().next();
this.txManager = configurer.annotationDrivenTransactionManager();
}

7
spring-tx/src/main/java/org/springframework/transaction/annotation/TransactionManagementConfigurationSelector.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2013 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.
@ -45,13 +45,12 @@ public class TransactionManagementConfigurationSelector @@ -45,13 +45,12 @@ public class TransactionManagementConfigurationSelector
protected String[] selectImports(AdviceMode adviceMode) {
switch (adviceMode) {
case PROXY:
return new String[] { AutoProxyRegistrar.class.getName(), ProxyTransactionManagementConfiguration.class.getName() };
return new String[] {AutoProxyRegistrar.class.getName(), ProxyTransactionManagementConfiguration.class.getName()};
case ASPECTJ:
return new String[] { TransactionManagementConfigUtils.TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME };
return new String[] {TransactionManagementConfigUtils.TRANSACTION_ASPECT_CONFIGURATION_CLASS_NAME};
default:
return null;
}
}
}

Loading…
Cancel
Save