001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.aop.config; 018 019import java.lang.reflect.Method; 020 021import org.springframework.beans.BeanUtils; 022import org.springframework.beans.factory.BeanFactory; 023import org.springframework.beans.factory.BeanFactoryAware; 024import org.springframework.beans.factory.FactoryBean; 025import org.springframework.util.StringUtils; 026 027/** 028 * {@link FactoryBean} implementation that locates a {@link Method} on a specified bean. 029 * 030 * @author Rob Harrop 031 * @since 2.0 032 */ 033public class MethodLocatingFactoryBean implements FactoryBean<Method>, BeanFactoryAware { 034 035 private String targetBeanName; 036 037 private String methodName; 038 039 private Method method; 040 041 042 /** 043 * Set the name of the bean to locate the {@link Method} on. 044 * <p>This property is required. 045 * @param targetBeanName the name of the bean to locate the {@link Method} on 046 */ 047 public void setTargetBeanName(String targetBeanName) { 048 this.targetBeanName = targetBeanName; 049 } 050 051 /** 052 * Set the name of the {@link Method} to locate. 053 * <p>This property is required. 054 * @param methodName the name of the {@link Method} to locate 055 */ 056 public void setMethodName(String methodName) { 057 this.methodName = methodName; 058 } 059 060 @Override 061 public void setBeanFactory(BeanFactory beanFactory) { 062 if (!StringUtils.hasText(this.targetBeanName)) { 063 throw new IllegalArgumentException("Property 'targetBeanName' is required"); 064 } 065 if (!StringUtils.hasText(this.methodName)) { 066 throw new IllegalArgumentException("Property 'methodName' is required"); 067 } 068 069 Class<?> beanClass = beanFactory.getType(this.targetBeanName); 070 if (beanClass == null) { 071 throw new IllegalArgumentException("Can't determine type of bean with name '" + this.targetBeanName + "'"); 072 } 073 this.method = BeanUtils.resolveSignature(this.methodName, beanClass); 074 075 if (this.method == null) { 076 throw new IllegalArgumentException("Unable to locate method [" + this.methodName + 077 "] on bean [" + this.targetBeanName + "]"); 078 } 079 } 080 081 082 @Override 083 public Method getObject() throws Exception { 084 return this.method; 085 } 086 087 @Override 088 public Class<Method> getObjectType() { 089 return Method.class; 090 } 091 092 @Override 093 public boolean isSingleton() { 094 return true; 095 } 096 097}