提交 e3153f70 编写于 作者: C Chris Beams

+ Renamed @Factory -> @FactoryMethod

+ callbackType -> interceptor
+ registrarType -> registrar
+ validatorTypes -> validators
上级 f9918f9b
......@@ -12,7 +12,7 @@ import org.springframework.beans.factory.support.BeanDefinitionRegistry;
* <h3>Constraints</h3> Implementations must have only a default constructor, or explicitly
* declare a no-arg constructor.
*
* @see Factory
* @see FactoryMethod
* @see ModelMethod
*
* @author Chris Beams
......
......@@ -21,35 +21,47 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.NoOp;
import net.sf.cglib.proxy.MethodInterceptor;
import org.springframework.config.java.ext.AbstractMethodInterceptor;
import org.springframework.config.java.ext.Bean;
/**
* Meta-annotation used to identify annotations as producers of beans and/or values.
* Meta-annotation used to identify method annotations as producers of beans and/or values.
* Provides a model that's open for extension. i.e.: The {@link Bean} annotation is
* annotated as a {@link FactoryMethod}. In this same fashion, any custom annotation can be
* devised with its own semantics. It need only provide a custom registrar, interceptor and
* optionally, validators.
*
* @see Bean
* @see BeanDefinitionRegistrar
* @see AbstractMethodInterceptor
* @see Validator
*
* @author Chris Beams
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
@Documented
public @interface Factory {
public @interface FactoryMethod {
/**
* Specifies which registrar (if any) should be used to register bean definitions for
* this {@link Factory} method.
* Specifies which registrar should be used to register bean definitions when processing
* this {@link FactoryMethod}.
*/
Class<? extends BeanDefinitionRegistrar> registrarType();
Class<? extends BeanDefinitionRegistrar> registrar();
/**
* Specifies what (if any) callback should be used when processing this {@link Factory}
* method. Defaults to CGLIB's {@link NoOp}, which does nothing. TODO: rename
* (interceptorType)? to keep with the -or|-ar nomenclature
* Specifies what interceptor should be used when processing this {@link FactoryMethod}.
* Defaults to {@link NoOpInterceptor} which does nothing.
*/
Class<? extends Callback> callbackType() default NoOp.class;
Class<? extends MethodInterceptor> interceptor() default NoOpInterceptor.class;
/**
* TODO: document TODO: rename
* Optionally specifies any {@link Validator} types capable of validating the syntax of
* this {@link FactoryMethod}. Usually used when a factory method may have multiple
* annotations such as {@link Bean} and {@link ScopedProxy}.
*/
Class<? extends Validator>[] validatorTypes() default {};
Class<? extends Validator>[] validators() default {};
}
......@@ -26,7 +26,6 @@ import java.util.List;
import java.util.Set;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.NoOp;
import org.springframework.util.Assert;
......@@ -40,7 +39,7 @@ public final class ModelMethod implements Validatable {
private final List<Annotation> annotations = new ArrayList<Annotation>();
private transient ConfigurationClass declaringClass;
private transient int lineNumber;
private transient Factory factoryAnno;
private transient FactoryMethod factoryAnno;
private transient final List<Validator> validators = new ArrayList<Validator>();
public ModelMethod(String name, int modifiers, ModelClass returnType, Annotation... annotations) {
......@@ -51,7 +50,7 @@ public final class ModelMethod implements Validatable {
for (Annotation annotation : annotations) {
this.annotations.add(annotation);
if (factoryAnno == null)
factoryAnno = annotation.annotationType().getAnnotation(Factory.class);
factoryAnno = annotation.annotationType().getAnnotation(FactoryMethod.class);
}
Assert.isTrue(modifiers >= 0, "modifiers must be non-negative: " + modifiers);
......@@ -144,23 +143,23 @@ public final class ModelMethod implements Validatable {
}
public BeanDefinitionRegistrar getRegistrar() {
return getInstance(factoryAnno.registrarType());
return getInstance(factoryAnno.registrar());
}
public Set<Validator> getValidators() {
HashSet<Validator> validator = new HashSet<Validator>();
for (Class<? extends Validator> validatorType : factoryAnno.validatorTypes())
for (Class<? extends Validator> validatorType : factoryAnno.validators())
validator.add(getInstance(validatorType));
return validator;
}
public Callback getCallback() {
Class<? extends Callback> callbackType = factoryAnno.callbackType();
Class<? extends Callback> callbackType = factoryAnno.interceptor();
if (callbackType.equals(NoOp.class))
return NoOp.INSTANCE;
if (callbackType.equals(NoOpInterceptor.class))
return NoOpInterceptor.INSTANCE;
return getInstance(callbackType);
}
......
/*
* Copyright 2002-2009 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.config.java;
import java.lang.reflect.Method;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class NoOpInterceptor implements MethodInterceptor {
public static final NoOpInterceptor INSTANCE = new NoOpInterceptor();
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
return null;
}
}
......@@ -106,7 +106,7 @@ public class Util {
* <p>
* ASM class reading is used throughout JavaConfig, but there are certain cases where
* classloading cannot be avoided - specifically in cases where users define their own
* {@link Extension} or {@link Factory} annotations. This method should therefore be
* {@link Extension} or {@link FactoryMethod} annotations. This method should therefore be
* used sparingly but consistently where required.
* <p>
* Because {@link ClassNotFoundException} is compensated for by returning null, callers
......
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2009 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.
......@@ -29,7 +29,7 @@ import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.config.java.Configuration;
import org.springframework.config.java.ConfigurationClass;
import org.springframework.config.java.ConfigurationModel;
import org.springframework.config.java.Factory;
import org.springframework.config.java.FactoryMethod;
import org.springframework.config.java.ModelMethod;
import org.springframework.config.java.Scopes;
import org.springframework.config.java.UsageError;
......@@ -62,7 +62,6 @@ import org.springframework.config.java.Validator;
* </p>
*
* @see Configuration
* @see BeanNamingStrategy
*
* @author Rod Johnson
* @author Costin Leau
......@@ -72,8 +71,9 @@ import org.springframework.config.java.Validator;
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@Factory(registrarType = BeanRegistrar.class, callbackType = BeanMethodInterceptor.class, validatorTypes = {
BeanValidator.class, IllegalBeanOverrideValidator.class })
@FactoryMethod(registrar = BeanRegistrar.class,
interceptor = BeanMethodInterceptor.class,
validators = { BeanValidator.class, IllegalBeanOverrideValidator.class })
public @interface Bean {
/**
......
......@@ -29,7 +29,6 @@ import net.sf.cglib.core.DefaultGeneratorStrategy;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.NoOp;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......@@ -44,6 +43,7 @@ import org.springframework.config.java.Configuration;
import org.springframework.config.java.ConfigurationClass;
import org.springframework.config.java.ConfigurationModel;
import org.springframework.config.java.ModelMethod;
import org.springframework.config.java.NoOpInterceptor;
/**
......@@ -122,7 +122,7 @@ public class ConfigurationEnhancer {
// no-op
}
});
callbackInstances.add(NoOp.INSTANCE);
callbackInstances.add(NoOpInterceptor.INSTANCE);
for (Callback callback : callbackInstances)
callbackTypes.add(callback.getClass());
......
......@@ -30,14 +30,14 @@ import org.objectweb.asm.MethodAdapter;
import org.objectweb.asm.Opcodes;
import org.springframework.config.java.Configuration;
import org.springframework.config.java.ConfigurationClass;
import org.springframework.config.java.Factory;
import org.springframework.config.java.FactoryMethod;
import org.springframework.config.java.ModelClass;
import org.springframework.config.java.ModelMethod;
/**
* Visits a single method declared in a given {@link Configuration} class. Determines
* whether the method is a {@link Factory} method and if so, adds it to the
* whether the method is a {@link FactoryMethod} method and if so, adds it to the
* {@link ConfigurationClass}.
*
* @author Chris Beams
......@@ -105,13 +105,13 @@ class ConfigurationClassMethodVisitor extends MethodAdapter {
/**
* Parses through all {@link #annotations} on this method in order to determine whether
* it is a {@link Factory} method or not and if so adds it to the enclosing
* it is a {@link FactoryMethod} method or not and if so adds it to the enclosing
* {@link #configClass}.
*/
@Override
public void visitEnd() {
for (Annotation anno : annotations) {
if (anno.annotationType().getAnnotation(Factory.class) != null) {
if (anno.annotationType().getAnnotation(FactoryMethod.class) != null) {
isModelMethod = true;
break;
}
......
......@@ -34,7 +34,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.config.java.Configuration;
import org.springframework.config.java.ConfigurationClass;
import org.springframework.config.java.ConfigurationModel;
import org.springframework.config.java.Factory;
import org.springframework.config.java.FactoryMethod;
import org.springframework.config.java.ModelMethod;
import org.springframework.config.java.plugin.Extension;
import org.springframework.config.java.plugin.ExtensionAnnotationBeanDefinitionRegistrar;
......@@ -84,7 +84,7 @@ class ConfigurationModelBeanDefinitionReader {
/**
* Reads a particular {@link ConfigurationClass}, registering bean definitions for the
* class itself, all its {@link Factory} methods and all its {@link Extension}
* class itself, all its {@link FactoryMethod} methods and all its {@link Extension}
* annotations.
*/
private void loadBeanDefinitionsForConfigurationClass(ConfigurationClass configClass) {
......@@ -145,7 +145,7 @@ class ConfigurationModelBeanDefinitionReader {
* Reads a particular {@link ModelMethod}, registering bean definitions with
* {@link #beanFactory} based on its contents.
*
* @see Factory
* @see FactoryMethod
*/
private void loadBeanDefinitionsForModelMethod(ModelMethod method) {
method.getRegistrar().register(method, beanFactory);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册