001/*
002 * Copyright 2002-2015 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.aop.framework;
018
019import java.io.Serializable;
020import java.lang.reflect.Proxy;
021
022import org.springframework.aop.SpringProxy;
023
024/**
025 * Default {@link AopProxyFactory} implementation, creating either a CGLIB proxy
026 * or a JDK dynamic proxy.
027 *
028 * <p>Creates a CGLIB proxy if one the following is true for a given
029 * {@link AdvisedSupport} instance:
030 * <ul>
031 * <li>the {@code optimize} flag is set
032 * <li>the {@code proxyTargetClass} flag is set
033 * <li>no proxy interfaces have been specified
034 * </ul>
035 *
036 * <p>In general, specify {@code proxyTargetClass} to enforce a CGLIB proxy,
037 * or specify one or more interfaces to use a JDK dynamic proxy.
038 *
039 * @author Rod Johnson
040 * @author Juergen Hoeller
041 * @since 12.03.2004
042 * @see AdvisedSupport#setOptimize
043 * @see AdvisedSupport#setProxyTargetClass
044 * @see AdvisedSupport#setInterfaces
045 */
046@SuppressWarnings("serial")
047public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
048
049        @Override
050        public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
051                if (config.isOptimize() || config.isProxyTargetClass() || hasNoUserSuppliedProxyInterfaces(config)) {
052                        Class<?> targetClass = config.getTargetClass();
053                        if (targetClass == null) {
054                                throw new AopConfigException("TargetSource cannot determine target class: " +
055                                                "Either an interface or a target is required for proxy creation.");
056                        }
057                        if (targetClass.isInterface() || Proxy.isProxyClass(targetClass)) {
058                                return new JdkDynamicAopProxy(config);
059                        }
060                        return new ObjenesisCglibAopProxy(config);
061                }
062                else {
063                        return new JdkDynamicAopProxy(config);
064                }
065        }
066
067        /**
068         * Determine whether the supplied {@link AdvisedSupport} has only the
069         * {@link org.springframework.aop.SpringProxy} interface specified
070         * (or no proxy interfaces specified at all).
071         */
072        private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) {
073                Class<?>[] ifcs = config.getProxiedInterfaces();
074                return (ifcs.length == 0 || (ifcs.length == 1 && SpringProxy.class.isAssignableFrom(ifcs[0])));
075        }
076
077}