KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > config > SimpleBeanFactoryAwareAspectInstanceFactory


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.config;
18
19 import org.springframework.aop.aspectj.AspectInstanceFactory;
20 import org.springframework.beans.factory.BeanFactory;
21 import org.springframework.beans.factory.BeanFactoryAware;
22 import org.springframework.core.Ordered;
23 import org.springframework.util.StringUtils;
24
25 /**
26  * Implementation of {@link AspectInstanceFactory} that locates the aspect from the
27  * {@link org.springframework.beans.factory.BeanFactory} using a configured bean name.
28  *
29  * @author Rob Harrop
30  * @author Juergen Hoeller
31  * @since 2.0
32  */

33 public class SimpleBeanFactoryAwareAspectInstanceFactory implements AspectInstanceFactory, BeanFactoryAware {
34
35     private String JavaDoc aspectBeanName;
36
37     private BeanFactory beanFactory;
38
39
40     /**
41      * Set the name of the aspect bean. This is the bean that is returned when calling
42      * {@link #getAspectInstance()}.
43      */

44     public void setAspectBeanName(String JavaDoc aspectBeanName) {
45         this.aspectBeanName = aspectBeanName;
46     }
47
48     public void setBeanFactory(BeanFactory beanFactory) {
49         this.beanFactory = beanFactory;
50         if (!StringUtils.hasText(this.aspectBeanName)) {
51             throw new IllegalArgumentException JavaDoc("aspectBeanName is required");
52         }
53     }
54
55
56     /**
57      * Look up the aspect bean from the {@link BeanFactory} and returns it.
58      * @see #setAspectBeanName
59      */

60     public Object JavaDoc getAspectInstance() {
61         return this.beanFactory.getBean(this.aspectBeanName);
62     }
63
64     public int getOrder() {
65         if (this.beanFactory.isSingleton(this.aspectBeanName) &&
66                 this.beanFactory.isTypeMatch(this.aspectBeanName, Ordered.class)) {
67             return ((Ordered) this.beanFactory.getBean(this.aspectBeanName)).getOrder();
68         }
69         return Ordered.LOWEST_PRECEDENCE;
70     }
71
72 }
73
Popular Tags