001/* 002 * Copyright 2002-2017 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.lang.Nullable; 026import org.springframework.util.StringUtils; 027 028/** 029 * {@link FactoryBean} implementation that locates a {@link Method} on a specified bean. 030 * 031 * @author Rob Harrop 032 * @since 2.0 033 */ 034public class MethodLocatingFactoryBean implements FactoryBean<Method>, BeanFactoryAware { 035 036 @Nullable 037 private String targetBeanName; 038 039 @Nullable 040 private String methodName; 041 042 @Nullable 043 private Method method; 044 045 046 /** 047 * Set the name of the bean to locate the {@link Method} on. 048 * <p>This property is required. 049 * @param targetBeanName the name of the bean to locate the {@link Method} on 050 */ 051 public void setTargetBeanName(String targetBeanName) { 052 this.targetBeanName = targetBeanName; 053 } 054 055 /** 056 * Set the name of the {@link Method} to locate. 057 * <p>This property is required. 058 * @param methodName the name of the {@link Method} to locate 059 */ 060 public void setMethodName(String methodName) { 061 this.methodName = methodName; 062 } 063 064 @Override 065 public void setBeanFactory(BeanFactory beanFactory) { 066 if (!StringUtils.hasText(this.targetBeanName)) { 067 throw new IllegalArgumentException("Property 'targetBeanName' is required"); 068 } 069 if (!StringUtils.hasText(this.methodName)) { 070 throw new IllegalArgumentException("Property 'methodName' is required"); 071 } 072 073 Class<?> beanClass = beanFactory.getType(this.targetBeanName); 074 if (beanClass == null) { 075 throw new IllegalArgumentException("Can't determine type of bean with name '" + this.targetBeanName + "'"); 076 } 077 this.method = BeanUtils.resolveSignature(this.methodName, beanClass); 078 079 if (this.method == null) { 080 throw new IllegalArgumentException("Unable to locate method [" + this.methodName + 081 "] on bean [" + this.targetBeanName + "]"); 082 } 083 } 084 085 086 @Override 087 @Nullable 088 public Method getObject() throws Exception { 089 return this.method; 090 } 091 092 @Override 093 public Class<Method> getObjectType() { 094 return Method.class; 095 } 096 097 @Override 098 public boolean isSingleton() { 099 return true; 100 } 101 102}