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