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