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.aop.aspectj;
018
019import java.lang.reflect.InvocationTargetException;
020
021import org.springframework.aop.framework.AopConfigException;
022import org.springframework.core.Ordered;
023import org.springframework.lang.Nullable;
024import org.springframework.util.Assert;
025import org.springframework.util.ReflectionUtils;
026
027/**
028 * Implementation of {@link AspectInstanceFactory} that creates a new instance
029 * of the specified aspect class for every {@link #getAspectInstance()} call.
030 *
031 * @author Juergen Hoeller
032 * @since 2.0.4
033 */
034public class SimpleAspectInstanceFactory implements AspectInstanceFactory {
035
036        private final Class<?> aspectClass;
037
038
039        /**
040         * Create a new SimpleAspectInstanceFactory for the given aspect class.
041         * @param aspectClass the aspect class
042         */
043        public SimpleAspectInstanceFactory(Class<?> aspectClass) {
044                Assert.notNull(aspectClass, "Aspect class must not be null");
045                this.aspectClass = aspectClass;
046        }
047
048
049        /**
050         * Return the specified aspect class (never {@code null}).
051         */
052        public final Class<?> getAspectClass() {
053                return this.aspectClass;
054        }
055
056        @Override
057        public final Object getAspectInstance() {
058                try {
059                        return ReflectionUtils.accessibleConstructor(this.aspectClass).newInstance();
060                }
061                catch (NoSuchMethodException ex) {
062                        throw new AopConfigException(
063                                        "No default constructor on aspect class: " + this.aspectClass.getName(), ex);
064                }
065                catch (InstantiationException ex) {
066                        throw new AopConfigException(
067                                        "Unable to instantiate aspect class: " + this.aspectClass.getName(), ex);
068                }
069                catch (IllegalAccessException ex) {
070                        throw new AopConfigException(
071                                        "Could not access aspect constructor: " + this.aspectClass.getName(), ex);
072                }
073                catch (InvocationTargetException ex) {
074                        throw new AopConfigException(
075                                        "Failed to invoke aspect constructor: " + this.aspectClass.getName(), ex.getTargetException());
076                }
077        }
078
079        @Override
080        @Nullable
081        public ClassLoader getAspectClassLoader() {
082                return this.aspectClass.getClassLoader();
083        }
084
085        /**
086         * Determine the order for this factory's aspect instance,
087         * either an instance-specific order expressed through implementing
088         * the {@link org.springframework.core.Ordered} interface,
089         * or a fallback order.
090         * @see org.springframework.core.Ordered
091         * @see #getOrderForAspectClass
092         */
093        @Override
094        public int getOrder() {
095                return getOrderForAspectClass(this.aspectClass);
096        }
097
098        /**
099         * Determine a fallback order for the case that the aspect instance
100         * does not express an instance-specific order through implementing
101         * the {@link org.springframework.core.Ordered} interface.
102         * <p>The default implementation simply returns {@code Ordered.LOWEST_PRECEDENCE}.
103         * @param aspectClass the aspect class
104         */
105        protected int getOrderForAspectClass(Class<?> aspectClass) {
106                return Ordered.LOWEST_PRECEDENCE;
107        }
108
109}