1 /*
2  * Copyright 2002-2009 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.springframework.aop.aspectj.annotation;
18 
19 import org.springframework.aop.aspectj.SingletonAspectInstanceFactory;
20 import org.springframework.core.Ordered;
21 import org.springframework.core.annotation.Order;
22 
23 /**
24  * Implementation of {@link MetadataAwareAspectInstanceFactory} that is backed
25  * by a specified singleton object, returning the same instance for every
26  * {@link #getAspectInstance()} call.
27  *
28  * @author Rod Johnson
29  * @author Juergen Hoeller
30  * @since 2.0
31  * @see SimpleMetadataAwareAspectInstanceFactory
32  */
33 public class SingletonMetadataAwareAspectInstanceFactory extends SingletonAspectInstanceFactory
34 		implements MetadataAwareAspectInstanceFactory {
35 
36 	private final AspectMetadata metadata;
37 
38 
39 	/**
40 	 * Create a new SingletonMetadataAwareAspectInstanceFactory for the given aspect.
41 	 * @param aspectInstance the singleton aspect instance
42 	 * @param aspectName the name of the aspect
43 	 */
SingletonMetadataAwareAspectInstanceFactory(Object aspectInstance, String aspectName)44 	public SingletonMetadataAwareAspectInstanceFactory(Object aspectInstance, String aspectName) {
45 		super(aspectInstance);
46 		this.metadata = new AspectMetadata(aspectInstance.getClass(), aspectName);
47 	}
48 
49 
getAspectMetadata()50 	public final AspectMetadata getAspectMetadata() {
51 		return this.metadata;
52 	}
53 
54 	/**
55 	 * Check whether the aspect class carries an
56 	 * {@link org.springframework.core.annotation.Order} annotation,
57 	 * falling back to <code>Ordered.LOWEST_PRECEDENCE</code>.
58 	 * @see org.springframework.core.annotation.Order
59 	 */
60 	@Override
getOrderForAspectClass(Class<?> aspectClass)61 	protected int getOrderForAspectClass(Class<?> aspectClass) {
62 		Order order = aspectClass.getAnnotation(Order.class);
63 		if (order != null) {
64 			return order.value();
65 		}
66 		return Ordered.LOWEST_PRECEDENCE;
67 	}
68 
69 }
70