KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Method JavaDoc;
20
21 import org.springframework.beans.BeanUtils;
22 import org.springframework.beans.factory.BeanFactory;
23 import org.springframework.beans.factory.BeanFactoryAware;
24 import org.springframework.beans.factory.FactoryBean;
25 import org.springframework.util.StringUtils;
26
27 /**
28  * {@link FactoryBean} implementation that locates a {@link Method} on a specified bean.
29  *
30  * @author Rob Harrop
31  * @since 2.0
32  */

33 public class MethodLocatingFactoryBean implements FactoryBean, BeanFactoryAware {
34
35     private String JavaDoc targetBeanName;
36
37     private String JavaDoc methodName;
38
39     private Method JavaDoc method;
40
41
42     /**
43      * Set the name of the bean to locate the {@link Method} on.
44      * <p>This property is required.
45      * @param targetBeanName the name of the bean to locate the {@link Method} on
46      */

47     public void setTargetBeanName(String JavaDoc targetBeanName) {
48         this.targetBeanName = targetBeanName;
49     }
50
51     /**
52      * Set the name of the {@link Method} to locate.
53      * <p>This property is required.
54      * @param methodName the name of the {@link Method} to locate
55      */

56     public void setMethodName(String JavaDoc methodName) {
57         this.methodName = methodName;
58     }
59
60     public void setBeanFactory(BeanFactory beanFactory) {
61         if (!StringUtils.hasText(this.targetBeanName)) {
62             throw new IllegalArgumentException JavaDoc("Property 'targetBeanName' is required");
63         }
64         if (!StringUtils.hasText(this.methodName)) {
65             throw new IllegalArgumentException JavaDoc("Property 'methodName' is required");
66         }
67
68         Class JavaDoc beanClass = beanFactory.getType(this.targetBeanName);
69         if (beanClass == null) {
70             throw new IllegalArgumentException JavaDoc("Can't determine type of bean with name '" + this.targetBeanName + "'");
71         }
72         this.method = BeanUtils.resolveSignature(this.methodName, beanClass);
73
74         if (this.method == null) {
75             throw new IllegalArgumentException JavaDoc("Unable to locate method [" + this.methodName +
76                     "] on bean [" + this.targetBeanName + "]");
77         }
78     }
79
80
81     public Object JavaDoc getObject() throws Exception JavaDoc {
82         return this.method;
83     }
84
85     public Class JavaDoc getObjectType() {
86         return Method JavaDoc.class;
87     }
88
89     public boolean isSingleton() {
90         return true;
91     }
92
93 }
94
Popular Tags