01: /*
02: * Copyright 2002-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
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,
34: BeanFactoryAware {
35:
36: private String targetBeanName;
37:
38: private String methodName;
39:
40: private Method method;
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 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 methodName) {
57: this .methodName = methodName;
58: }
59:
60: public void setBeanFactory(BeanFactory beanFactory) {
61: if (!StringUtils.hasText(this .targetBeanName)) {
62: throw new IllegalArgumentException(
63: "Property 'targetBeanName' is required");
64: }
65: if (!StringUtils.hasText(this .methodName)) {
66: throw new IllegalArgumentException(
67: "Property 'methodName' is required");
68: }
69:
70: Class beanClass = beanFactory.getType(this .targetBeanName);
71: if (beanClass == null) {
72: throw new IllegalArgumentException(
73: "Can't determine type of bean with name '"
74: + this .targetBeanName + "'");
75: }
76: this .method = BeanUtils.resolveSignature(this .methodName,
77: beanClass);
78:
79: if (this .method == null) {
80: throw new IllegalArgumentException(
81: "Unable to locate method [" + this .methodName
82: + "] on bean [" + this .targetBeanName + "]");
83: }
84: }
85:
86: public Object getObject() throws Exception {
87: return this .method;
88: }
89:
90: public Class getObjectType() {
91: return Method.class;
92: }
93:
94: public boolean isSingleton() {
95: return true;
96: }
97:
98: }
|