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.framework.autoproxy; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.springframework.aop.TargetSource; 023import org.springframework.beans.factory.BeanFactory; 024import org.springframework.beans.factory.FactoryBean; 025import org.springframework.lang.Nullable; 026import org.springframework.util.Assert; 027import org.springframework.util.PatternMatchUtils; 028import org.springframework.util.StringUtils; 029 030/** 031 * Auto proxy creator that identifies beans to proxy via a list of names. 032 * Checks for direct, "xxx*", and "*xxx" matches. 033 * 034 * <p>For configuration details, see the javadoc of the parent class 035 * AbstractAutoProxyCreator. Typically, you will specify a list of 036 * interceptor names to apply to all identified beans, via the 037 * "interceptorNames" property. 038 * 039 * @author Juergen Hoeller 040 * @since 10.10.2003 041 * @see #setBeanNames 042 * @see #isMatch 043 * @see #setInterceptorNames 044 * @see AbstractAutoProxyCreator 045 */ 046@SuppressWarnings("serial") 047public class BeanNameAutoProxyCreator extends AbstractAutoProxyCreator { 048 049 @Nullable 050 private List<String> beanNames; 051 052 053 /** 054 * Set the names of the beans that should automatically get wrapped with proxies. 055 * A name can specify a prefix to match by ending with "*", e.g. "myBean,tx*" 056 * will match the bean named "myBean" and all beans whose name start with "tx". 057 * <p><b>NOTE:</b> In case of a FactoryBean, only the objects created by the 058 * FactoryBean will get proxied. This default behavior applies as of Spring 2.0. 059 * If you intend to proxy a FactoryBean instance itself (a rare use case, but 060 * Spring 1.2's default behavior), specify the bean name of the FactoryBean 061 * including the factory-bean prefix "&": e.g. "&myFactoryBean". 062 * @see org.springframework.beans.factory.FactoryBean 063 * @see org.springframework.beans.factory.BeanFactory#FACTORY_BEAN_PREFIX 064 */ 065 public void setBeanNames(String... beanNames) { 066 Assert.notEmpty(beanNames, "'beanNames' must not be empty"); 067 this.beanNames = new ArrayList<>(beanNames.length); 068 for (String mappedName : beanNames) { 069 this.beanNames.add(StringUtils.trimWhitespace(mappedName)); 070 } 071 } 072 073 074 /** 075 * Identify as bean to proxy if the bean name is in the configured list of names. 076 */ 077 @Override 078 @Nullable 079 protected Object[] getAdvicesAndAdvisorsForBean( 080 Class<?> beanClass, String beanName, @Nullable TargetSource targetSource) { 081 082 if (this.beanNames != null) { 083 for (String mappedName : this.beanNames) { 084 if (FactoryBean.class.isAssignableFrom(beanClass)) { 085 if (!mappedName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) { 086 continue; 087 } 088 mappedName = mappedName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length()); 089 } 090 if (isMatch(beanName, mappedName)) { 091 return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS; 092 } 093 BeanFactory beanFactory = getBeanFactory(); 094 if (beanFactory != null) { 095 String[] aliases = beanFactory.getAliases(beanName); 096 for (String alias : aliases) { 097 if (isMatch(alias, mappedName)) { 098 return PROXY_WITHOUT_ADDITIONAL_INTERCEPTORS; 099 } 100 } 101 } 102 } 103 } 104 return DO_NOT_PROXY; 105 } 106 107 /** 108 * Return if the given bean name matches the mapped name. 109 * <p>The default implementation checks for "xxx*", "*xxx" and "*xxx*" matches, 110 * as well as direct equality. Can be overridden in subclasses. 111 * @param beanName the bean name to check 112 * @param mappedName the name in the configured list of names 113 * @return if the names match 114 * @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String) 115 */ 116 protected boolean isMatch(String beanName, String mappedName) { 117 return PatternMatchUtils.simpleMatch(mappedName, beanName); 118 } 119 120}