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