001/*
002 * Copyright 2002-2016 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 org.springframework.beans.factory.BeanNameAware;
020
021/**
022 * {@code BeanPostProcessor} implementation that creates AOP proxies based on all
023 * candidate {@code Advisor}s in the current {@code BeanFactory}. This class is
024 * completely generic; it contains no special code to handle any particular aspects,
025 * such as pooling aspects.
026 *
027 * <p>It's possible to filter out advisors - for example, to use multiple post processors
028 * of this type in the same factory - by setting the {@code usePrefix} property to true,
029 * in which case only advisors beginning with the DefaultAdvisorAutoProxyCreator's bean
030 * name followed by a dot (like "aapc.") will be used. This default prefix can be changed
031 * from the bean name by setting the {@code advisorBeanNamePrefix} property.
032 * The separator (.) will also be used in this case.
033 *
034 * @author Rod Johnson
035 * @author Rob Harrop
036 */
037@SuppressWarnings("serial")
038public class DefaultAdvisorAutoProxyCreator extends AbstractAdvisorAutoProxyCreator implements BeanNameAware {
039
040        /** Separator between prefix and remainder of bean name */
041        public final static String SEPARATOR = ".";
042
043
044        private boolean usePrefix = false;
045
046        private String advisorBeanNamePrefix;
047
048
049        /**
050         * Set whether to only include advisors with a certain prefix in the bean name.
051         * <p>Default is {@code false}, including all beans of type {@code Advisor}.
052         * @see #setAdvisorBeanNamePrefix
053         */
054        public void setUsePrefix(boolean usePrefix) {
055                this.usePrefix = usePrefix;
056        }
057
058        /**
059         * Return whether to only include advisors with a certain prefix in the bean name.
060         */
061        public boolean isUsePrefix() {
062                return this.usePrefix;
063        }
064
065        /**
066         * Set the prefix for bean names that will cause them to be included for
067         * auto-proxying by this object. This prefix should be set to avoid circular
068         * references. Default value is the bean name of this object + a dot.
069         * @param advisorBeanNamePrefix the exclusion prefix
070         */
071        public void setAdvisorBeanNamePrefix(String advisorBeanNamePrefix) {
072                this.advisorBeanNamePrefix = advisorBeanNamePrefix;
073        }
074
075        /**
076         * Return the prefix for bean names that will cause them to be included
077         * for auto-proxying by this object.
078         */
079        public String getAdvisorBeanNamePrefix() {
080                return this.advisorBeanNamePrefix;
081        }
082
083        @Override
084        public void setBeanName(String name) {
085                // If no infrastructure bean name prefix has been set, override it.
086                if (this.advisorBeanNamePrefix == null) {
087                        this.advisorBeanNamePrefix = name + SEPARATOR;
088                }
089        }
090
091
092        /**
093         * Consider {@code Advisor} beans with the specified prefix as eligible, if activated.
094         * @see #setUsePrefix
095         * @see #setAdvisorBeanNamePrefix
096         */
097        @Override
098        protected boolean isEligibleAdvisorBean(String beanName) {
099                return (!isUsePrefix() || beanName.startsWith(getAdvisorBeanNamePrefix()));
100        }
101
102}