Java半路出家——编程细节
* Interface中能够定义常量 *:
public interface BeanFactory {
String FACTORY_BEAN_PREFIX = "&";
...
}
如果你在阅读别人的代码过程中发现如上代码(来自spring-beans/src/main/java/org/springframework/beans/factory/BeanFactory.java)。请不要惊讶,这里是合法的,并且jvm默认会为其加上final的修饰。
在编写framework接口方法的时候最好返回空列表或空数组,而不是null
理论依据:
微软Framework Design Guidelines->Guidelines for Collections
实战代码依据:
# by Rod Johnson
# org.springframework.beans.factory.support.DefaultListableBeanFactory
private String[] doGetBeanNamesForType(ResolvableType type, boolean includeNonSingletons, boolean allowEagerInit) {
List<String> result = new ArrayList<String>();
...
}
# by Rod Johnson
# org.springframework.beans.factory.support.AbstractBeanFactory
public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {
...
/** Custom PropertyEditorRegistrars to apply to the beans of this factory */
private final Set<PropertyEditorRegistrar> propertyEditorRegistrars =
new LinkedHashSet<PropertyEditorRegistrar>(4);
...
/** Custom PropertyEditors to apply to the beans of this factory */
private final Map<Class<?>, Class<? extends PropertyEditor>> customEditors =
new HashMap<Class<?>, Class<? extends PropertyEditor>>(4);
/** String resolvers to apply e.g. to annotation attribute values */
private final List<StringValueResolver> embeddedValueResolvers = new LinkedList<StringValueResolver>();
/** BeanPostProcessors to apply in createBean */
private final List<BeanPostProcessor> beanPostProcessors = new ArrayList<BeanPostProcessor>();
...
/**
* Return the list of BeanPostProcessors that will get applied
* to beans created with this factory.
*/
public List<BeanPostProcessor> getBeanPostProcessors() {
return this.beanPostProcessors;
}
...