001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.dao.support;
018
019import org.apache.commons.logging.Log;
020import org.apache.commons.logging.LogFactory;
021
022import org.springframework.beans.factory.BeanInitializationException;
023import org.springframework.beans.factory.InitializingBean;
024
025/**
026 * Generic base class for DAOs, defining template methods for DAO initialization.
027 *
028 * <p>Extended by Spring's specific DAO support classes, such as:
029 * JdbcDaoSupport, JdoDaoSupport, etc.
030 *
031 * @author Juergen Hoeller
032 * @since 1.2.2
033 * @see org.springframework.jdbc.core.support.JdbcDaoSupport
034 */
035public abstract class DaoSupport implements InitializingBean {
036
037        /** Logger available to subclasses. */
038        protected final Log logger = LogFactory.getLog(getClass());
039
040
041        @Override
042        public final void afterPropertiesSet() throws IllegalArgumentException, BeanInitializationException {
043                // Let abstract subclasses check their configuration.
044                checkDaoConfig();
045
046                // Let concrete implementations initialize themselves.
047                try {
048                        initDao();
049                }
050                catch (Exception ex) {
051                        throw new BeanInitializationException("Initialization of DAO failed", ex);
052                }
053        }
054
055        /**
056         * Abstract subclasses must override this to check their configuration.
057         * <p>Implementors should be marked as {@code final} if concrete subclasses
058         * are not supposed to override this template method themselves.
059         * @throws IllegalArgumentException in case of illegal configuration
060         */
061        protected abstract void checkDaoConfig() throws IllegalArgumentException;
062
063        /**
064         * Concrete subclasses can override this for custom initialization behavior.
065         * Gets called after population of this instance's bean properties.
066         * @throws Exception if DAO initialization fails
067         * (will be rethrown as a BeanInitializationException)
068         * @see org.springframework.beans.factory.BeanInitializationException
069         */
070        protected void initDao() throws Exception {
071        }
072
073}