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.aspectj; 018 019import java.io.Serializable; 020 021import org.springframework.core.Ordered; 022import org.springframework.lang.Nullable; 023import org.springframework.util.Assert; 024 025/** 026 * Implementation of {@link AspectInstanceFactory} that is backed by a 027 * specified singleton object, returning the same instance for every 028 * {@link #getAspectInstance()} call. 029 * 030 * @author Rod Johnson 031 * @author Juergen Hoeller 032 * @since 2.0 033 * @see SimpleAspectInstanceFactory 034 */ 035@SuppressWarnings("serial") 036public class SingletonAspectInstanceFactory implements AspectInstanceFactory, Serializable { 037 038 private final Object aspectInstance; 039 040 041 /** 042 * Create a new SingletonAspectInstanceFactory for the given aspect instance. 043 * @param aspectInstance the singleton aspect instance 044 */ 045 public SingletonAspectInstanceFactory(Object aspectInstance) { 046 Assert.notNull(aspectInstance, "Aspect instance must not be null"); 047 this.aspectInstance = aspectInstance; 048 } 049 050 051 @Override 052 public final Object getAspectInstance() { 053 return this.aspectInstance; 054 } 055 056 @Override 057 @Nullable 058 public ClassLoader getAspectClassLoader() { 059 return this.aspectInstance.getClass().getClassLoader(); 060 } 061 062 /** 063 * Determine the order for this factory's aspect instance, 064 * either an instance-specific order expressed through implementing 065 * the {@link org.springframework.core.Ordered} interface, 066 * or a fallback order. 067 * @see org.springframework.core.Ordered 068 * @see #getOrderForAspectClass 069 */ 070 @Override 071 public int getOrder() { 072 if (this.aspectInstance instanceof Ordered) { 073 return ((Ordered) this.aspectInstance).getOrder(); 074 } 075 return getOrderForAspectClass(this.aspectInstance.getClass()); 076 } 077 078 /** 079 * Determine a fallback order for the case that the aspect instance 080 * does not express an instance-specific order through implementing 081 * the {@link org.springframework.core.Ordered} interface. 082 * <p>The default implementation simply returns {@code Ordered.LOWEST_PRECEDENCE}. 083 * @param aspectClass the aspect class 084 */ 085 protected int getOrderForAspectClass(Class<?> aspectClass) { 086 return Ordered.LOWEST_PRECEDENCE; 087 } 088 089}