1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.configuration.beanutils; 18 19 /** 20 * <p> 21 * The default implementation of the {@code BeanFactory} interface. 22 * </p> 23 * <p> 24 * This class creates beans of arbitrary types using reflection. Each time the 25 * {@code createBean()} method is invoked, a new bean instance is 26 * created. A default bean class is not supported. 27 * </p> 28 * <p> 29 * An instance of this factory class will be set as the default bean factory for 30 * the {@link BeanHelper} class. This means that if not bean 31 * factory is specified in a {@link BeanDeclaration}, this 32 * default instance will be used. 33 * </p> 34 * 35 * @since 1.3 36 * @author <a 37 * href="http://commons.apache.org/configuration/team-list.html">Commons 38 * Configuration team</a> 39 * @version $Id: DefaultBeanFactory.java 1208758 2011-11-30 20:38:59Z oheger $ 40 */ 41 public class DefaultBeanFactory implements BeanFactory 42 { 43 /** Stores the default instance of this class. */ 44 public static final DefaultBeanFactory INSTANCE = new DefaultBeanFactory(); 45 46 /** 47 * Creates a new bean instance. This implementation delegates to the 48 * protected methods {@code createBeanInstance()} and 49 * {@code initBeanInstance()} for creating and initializing the bean. 50 * This makes it easier for derived classes that need to change specific 51 * functionality of the base class. 52 * 53 * @param beanClass the class of the bean, from which an instance is to be 54 * created 55 * @param data the bean declaration object 56 * @param parameter an additional parameter (ignored by this implementation) 57 * @return the new bean instance 58 * @throws Exception if an error occurs 59 */ 60 public Object createBean(Class<?> beanClass, BeanDeclaration data, 61 Object parameter) throws Exception 62 { 63 Object result = createBeanInstance(beanClass, data); 64 initBeanInstance(result, data); 65 return result; 66 } 67 68 /** 69 * Returns the default bean class used by this factory. This is always 70 * <b>null</b> for this implementation. 71 * 72 * @return the default bean class 73 */ 74 public Class<?> getDefaultBeanClass() 75 { 76 return null; 77 } 78 79 /** 80 * Creates the bean instance. This method is called by 81 * {@code createBean()}. It uses reflection to create a new instance 82 * of the specified class. 83 * 84 * @param beanClass the class of the bean to be created 85 * @param data the bean declaration 86 * @return the new bean instance 87 * @throws Exception if an error occurs 88 */ 89 protected Object createBeanInstance(Class<?> beanClass, BeanDeclaration data) 90 throws Exception 91 { 92 return beanClass.newInstance(); 93 } 94 95 /** 96 * Initializes the newly created bean instance. This method is called by 97 * {@code createBean()}. It calls the 98 * {@link BeanHelper#initBean(Object, BeanDeclaration) initBean()} 99 * of {@link BeanHelper} for performing the initialization. 100 * 101 * @param bean the newly created bean instance 102 * @param data the bean declaration object 103 * @throws Exception if an error occurs 104 */ 105 protected void initBeanInstance(Object bean, BeanDeclaration data) 106 throws Exception 107 { 108 BeanHelper.initBean(bean, data); 109 } 110 }