org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator.java Source code

Java tutorial

Introduction

Here is the source code for org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator.java

Source

/*
 * Copyright 2002-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.aop.framework.autoproxy;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.lang.Nullable;

/**
 * {@code BeanPostProcessor} implementation that creates AOP proxies based on all
 * candidate {@code Advisor}s in the current {@code BeanFactory}. This class is
 * completely generic; it contains no special code to handle any particular aspects,
 * such as pooling aspects.
 *
 * <p>It's possible to filter out advisors - for example, to use multiple post processors
 * of this type in the same factory - by setting the {@code usePrefix} property to true,
 * in which case only advisors beginning with the DefaultAdvisorAutoProxyCreator's bean
 * name followed by a dot (like "aapc.") will be used. This default prefix can be changed
 * from the bean name by setting the {@code advisorBeanNamePrefix} property.
 * The separator (.) will also be used in this case.
 *
 * @author Rod Johnson
 * @author Rob Harrop
 */
@SuppressWarnings("serial")
public class DefaultAdvisorAutoProxyCreator extends AbstractAdvisorAutoProxyCreator implements BeanNameAware {

    /** Separator between prefix and remainder of bean name. */
    public static final String SEPARATOR = ".";

    private boolean usePrefix = false;

    @Nullable
    private String advisorBeanNamePrefix;

    /**
     * Set whether to only include advisors with a certain prefix in the bean name.
     * <p>Default is {@code false}, including all beans of type {@code Advisor}.
     * @see #setAdvisorBeanNamePrefix
     */
    public void setUsePrefix(boolean usePrefix) {
        this.usePrefix = usePrefix;
    }

    /**
     * Return whether to only include advisors with a certain prefix in the bean name.
     */
    public boolean isUsePrefix() {
        return this.usePrefix;
    }

    /**
     * Set the prefix for bean names that will cause them to be included for
     * auto-proxying by this object. This prefix should be set to avoid circular
     * references. Default value is the bean name of this object + a dot.
     * @param advisorBeanNamePrefix the exclusion prefix
     */
    public void setAdvisorBeanNamePrefix(@Nullable String advisorBeanNamePrefix) {
        this.advisorBeanNamePrefix = advisorBeanNamePrefix;
    }

    /**
     * Return the prefix for bean names that will cause them to be included
     * for auto-proxying by this object.
     */
    @Nullable
    public String getAdvisorBeanNamePrefix() {
        return this.advisorBeanNamePrefix;
    }

    @Override
    public void setBeanName(String name) {
        // If no infrastructure bean name prefix has been set, override it.
        if (this.advisorBeanNamePrefix == null) {
            this.advisorBeanNamePrefix = name + SEPARATOR;
        }
    }

    /**
     * Consider {@code Advisor} beans with the specified prefix as eligible, if activated.
     * @see #setUsePrefix
     * @see #setAdvisorBeanNamePrefix
     */
    @Override
    protected boolean isEligibleAdvisorBean(String beanName) {
        if (!isUsePrefix()) {
            return true;
        }
        String prefix = getAdvisorBeanNamePrefix();
        return (prefix != null && beanName.startsWith(prefix));
    }

}