KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > aspectj > annotation > BeanFactoryAspectInstanceFactory


1 /*
2  * Copyright 2002-2006 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.beans.factory.BeanFactory;
20 import org.springframework.core.Ordered;
21 import org.springframework.core.annotation.Order;
22
23 /**
24  * AspectInstanceFactory backed by a Spring
25  * {@link org.springframework.beans.factory.BeanFactory}.
26  *
27  * <p>Note that this may instantiate multiple times if using a prototype,
28  * which probably won't give the semantics you expect.
29  * Use a {@link LazySingletonAspectInstanceFactoryDecorator}
30  * to wrap this to ensure only one new aspect comes back.
31  *
32  * @author Rod Johnson
33  * @author Juergen Hoeller
34  * @since 2.0
35  * @see org.springframework.beans.factory.BeanFactory
36  * @see LazySingletonAspectInstanceFactoryDecorator
37  */

38 public class BeanFactoryAspectInstanceFactory implements MetadataAwareAspectInstanceFactory {
39
40     private final BeanFactory beanFactory;
41
42     private final String JavaDoc name;
43
44     private final AspectMetadata aspectMetadata;
45
46
47     /**
48      * Create a BeanFactoryAspectInstanceFactory. AspectJ will be called to
49      * introspect to create AJType metadata using the type returned for the
50      * given bean name from the BeanFactory.
51      * @param beanFactory BeanFactory to obtain instance(s) from
52      * @param name name of the bean
53      */

54     public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String JavaDoc name) {
55         this(beanFactory, name, beanFactory.getType(name));
56     }
57     
58     /**
59      * Create a BeanFactoryAspectInstanceFactory, providing a type that AspectJ should
60      * introspect to create AJType metadata. Use if the BeanFactory may consider the type
61      * to be a subclass (as when using CGLIB), and the information should relate to a superclass.
62      * @param beanFactory BeanFactory to obtain instance(s) from
63      * @param name the name of the bean
64      * @param type the type that should be introspected by AspectJ
65      */

66     public BeanFactoryAspectInstanceFactory(BeanFactory beanFactory, String JavaDoc name, Class JavaDoc type) {
67         this.beanFactory = beanFactory;
68         this.name = name;
69         this.aspectMetadata = new AspectMetadata(type, name);
70     }
71
72
73     public Object JavaDoc getAspectInstance() {
74         return this.beanFactory.getBean(this.name);
75     }
76
77     public AspectMetadata getAspectMetadata() {
78         return this.aspectMetadata;
79     }
80
81     /**
82      * Determine the order for this factory's target aspect, either
83      * an instance-specific order expressed through implementing the
84      * {@link org.springframework.core.Ordered} interface (only
85      * checked for singleton beans), or an order expressed through the
86      * {@link org.springframework.core.annotation.Order} annotation
87      * at the class level.
88      * @see org.springframework.core.Ordered
89      * @see org.springframework.core.annotation.Order
90      */

91     public int getOrder() {
92         Class JavaDoc type = this.beanFactory.getType(this.name);
93         if (type != null) {
94             if (Ordered.class.isAssignableFrom(type) && this.beanFactory.isSingleton(this.name)) {
95                 return ((Ordered) this.beanFactory.getBean(this.name)).getOrder();
96             }
97             Order order = (Order) type.getAnnotation(Order.class);
98             if (order != null) {
99                 return order.value();
100             }
101         }
102         return Ordered.LOWEST_PRECEDENCE;
103     }
104
105
106     @Override JavaDoc
107     public String JavaDoc toString() {
108         return getClass().getSimpleName() + ": bean name '" + this.name + "'";
109     }
110
111 }
112
Popular Tags