+ Further documentation and pruning.
+ Added testing-related TODOs where appropriate
This commit is contained in:
parent
3130351233
commit
f953fd5b22
|
|
@ -54,7 +54,6 @@ import org.springframework.context.annotation.Scope;
|
||||||
* that references between beans are strongly typed and navigable. So called 'inter-bean
|
* that references between beans are strongly typed and navigable. So called 'inter-bean
|
||||||
* references' are guaranteed to respect scoping and AOP semantics.
|
* references' are guaranteed to respect scoping and AOP semantics.
|
||||||
*
|
*
|
||||||
*
|
|
||||||
* @author Rod Johnson
|
* @author Rod Johnson
|
||||||
* @author Costin Leau
|
* @author Costin Leau
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
|
|
@ -97,3 +96,8 @@ public @interface Bean {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: test @Lazy @Bean
|
||||||
|
// TODO: test @Primary @Bean
|
||||||
|
// TODO: test Bean alias scenarios
|
||||||
|
// TODO: test init/destroy method scenarios
|
||||||
|
// TODO: test dependsOn
|
||||||
|
|
|
||||||
|
|
@ -48,9 +48,6 @@ import org.springframework.stereotype.Component;
|
||||||
* {@link Autowired} constructor
|
* {@link Autowired} constructor
|
||||||
* </ul>
|
* </ul>
|
||||||
*
|
*
|
||||||
* TODO: test constructor autowiring<br>
|
|
||||||
* TODO: test private Configuration classes<br>
|
|
||||||
* TODO: test @Lazy @Configuration<br>
|
|
||||||
*
|
*
|
||||||
* @author Rod Johnson
|
* @author Rod Johnson
|
||||||
* @author Chris Beams
|
* @author Chris Beams
|
||||||
|
|
@ -67,3 +64,7 @@ import org.springframework.stereotype.Component;
|
||||||
public @interface Configuration {
|
public @interface Configuration {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: test constructor autowiring<br>
|
||||||
|
// TODO: test private Configuration classes<br>
|
||||||
|
// TODO: test @Lazy @Configuration<br>
|
||||||
|
|
|
||||||
|
|
@ -39,3 +39,5 @@ public class StandardScopes {
|
||||||
public static final String SESSION = "session";
|
public static final String SESSION = "session";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: move StandardScopes to appropriate package
|
||||||
|
|
|
||||||
|
|
@ -1,48 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2002-2008 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.support;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
|
|
||||||
import net.sf.cglib.proxy.MethodInterceptor;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
|
||||||
import org.springframework.beans.factory.BeanFactoryAware;
|
|
||||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Base class for all {@link MethodInterceptor} implementations.
|
|
||||||
*
|
|
||||||
* @author Chris Beams
|
|
||||||
*/
|
|
||||||
abstract class AbstractMethodInterceptor implements BeanFactoryAware, MethodInterceptor {
|
|
||||||
protected final Log log = LogFactory.getLog(this.getClass());
|
|
||||||
protected DefaultListableBeanFactory beanFactory;
|
|
||||||
|
|
||||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
|
||||||
Assert.isInstanceOf(DefaultListableBeanFactory.class, beanFactory);
|
|
||||||
this.beanFactory = (DefaultListableBeanFactory) beanFactory;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected String getBeanName(Method method) {
|
|
||||||
return method.getName();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* 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");
|
* 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.
|
||||||
|
|
@ -19,41 +19,66 @@ import static java.lang.String.*;
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
|
import net.sf.cglib.proxy.MethodInterceptor;
|
||||||
import net.sf.cglib.proxy.MethodProxy;
|
import net.sf.cglib.proxy.MethodProxy;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
|
import org.springframework.beans.factory.BeanFactoryAware;
|
||||||
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
||||||
|
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||||
import org.springframework.config.java.Bean;
|
import org.springframework.config.java.Bean;
|
||||||
import org.springframework.context.annotation.Scope;
|
import org.springframework.context.annotation.Scope;
|
||||||
import org.springframework.context.annotation.ScopedProxyMode;
|
import org.springframework.context.annotation.ScopedProxyMode;
|
||||||
import org.springframework.core.annotation.AnnotationUtils;
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Intercepts the invocation of any {@link Bean}-annotated methods in order to ensure proper
|
* Intercepts the invocation of any {@link Bean}-annotated methods in order to ensure proper
|
||||||
* handling of bean semantics such as scoping and AOP proxying.
|
* handling of bean semantics such as scoping and AOP proxying.
|
||||||
*
|
*
|
||||||
|
* @author Chris Beams
|
||||||
|
* @since 3.0
|
||||||
* @see Bean
|
* @see Bean
|
||||||
* @see BeanRegistrar
|
* @see BeanRegistrar
|
||||||
*
|
|
||||||
* @author Chris Beams
|
|
||||||
*/
|
*/
|
||||||
class BeanMethodInterceptor extends AbstractMethodInterceptor {
|
class BeanMethodInterceptor implements BeanFactoryAware, MethodInterceptor {
|
||||||
|
protected final Log log = LogFactory.getLog(this.getClass());
|
||||||
|
protected DefaultListableBeanFactory beanFactory;
|
||||||
|
|
||||||
|
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
||||||
|
Assert.isInstanceOf(DefaultListableBeanFactory.class, beanFactory);
|
||||||
|
this.beanFactory = (DefaultListableBeanFactory) beanFactory;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enhances a {@link Bean @Bean} method to check the supplied BeanFactory for the
|
* Enhances a {@link Bean @Bean} method to check the supplied BeanFactory for the
|
||||||
* existence of this bean object.
|
* existence of this bean object.
|
||||||
*/
|
*/
|
||||||
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 {
|
||||||
String beanName = getBeanName(method);
|
|
||||||
|
|
||||||
|
// determine the name of the bean
|
||||||
|
String beanName;
|
||||||
|
// check to see if the user has explicitly set the bean name
|
||||||
|
Bean bean = method.getAnnotation(Bean.class);
|
||||||
|
if(bean != null && bean.name().length > 1)
|
||||||
|
beanName = bean.name()[0];
|
||||||
|
// if not, simply return the name of the method as the bean name
|
||||||
|
else
|
||||||
|
beanName = method.getName();
|
||||||
|
|
||||||
|
// determine whether this bean is a scoped-proxy
|
||||||
Scope scope = AnnotationUtils.findAnnotation(method, Scope.class);
|
Scope scope = AnnotationUtils.findAnnotation(method, Scope.class);
|
||||||
boolean isScopedProxy = (scope != null && scope.proxyMode() != ScopedProxyMode.NO);
|
boolean isScopedProxy = (scope != null && scope.proxyMode() != ScopedProxyMode.NO);
|
||||||
|
String scopedBeanName = BeanRegistrar.resolveHiddenScopedProxyBeanName(beanName);
|
||||||
String scopedBeanName =
|
|
||||||
BeanRegistrar.resolveHiddenScopedProxyBeanName(beanName);
|
|
||||||
if (isScopedProxy && beanFactory.isCurrentlyInCreation(scopedBeanName))
|
if (isScopedProxy && beanFactory.isCurrentlyInCreation(scopedBeanName))
|
||||||
beanName = scopedBeanName;
|
beanName = scopedBeanName;
|
||||||
|
|
||||||
|
// to handle the case of an inter-bean method reference, we must explicitly check the
|
||||||
|
// container for already cached instances
|
||||||
if (factoryContainsBean(beanName)) {
|
if (factoryContainsBean(beanName)) {
|
||||||
// we have an already existing cached instance of this bean -> retrieve it
|
// we have an already existing cached instance of this bean -> retrieve it
|
||||||
Object cachedBean = beanFactory.getBean(beanName);
|
Object cachedBean = beanFactory.getBean(beanName);
|
||||||
|
|
@ -64,6 +89,7 @@ class BeanMethodInterceptor extends AbstractMethodInterceptor {
|
||||||
return cachedBean;
|
return cachedBean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// actually create and return the bean
|
||||||
return proxy.invokeSuper(obj, args);
|
return proxy.invokeSuper(obj, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -87,5 +113,4 @@ class BeanMethodInterceptor extends AbstractMethodInterceptor {
|
||||||
return beanFactory.containsBean(beanName) && !beanFactory.isCurrentlyInCreation(beanName);
|
return beanFactory.containsBean(beanName) && !beanFactory.isCurrentlyInCreation(beanName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue