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.config;
018
019import org.w3c.dom.Element;
020
021import org.springframework.beans.factory.config.BeanDefinition;
022import org.springframework.beans.factory.parsing.BeanComponentDefinition;
023import org.springframework.beans.factory.support.BeanDefinitionRegistry;
024import org.springframework.beans.factory.xml.ParserContext;
025
026/**
027 * Utility class for handling registration of auto-proxy creators used internally
028 * by the '{@code aop}' namespace tags.
029 *
030 * <p>Only a single auto-proxy creator should be registered and multiple configuration
031 * elements may wish to register different concrete implementations. As such this class
032 * delegates to {@link AopConfigUtils} which provides a simple escalation protocol.
033 * Callers may request a particular auto-proxy creator and know that creator,
034 * <i>or a more capable variant thereof</i>, will be registered as a post-processor.
035 *
036 * @author Rob Harrop
037 * @author Juergen Hoeller
038 * @author Mark Fisher
039 * @since 2.0
040 * @see AopConfigUtils
041 */
042public abstract class AopNamespaceUtils {
043
044        /**
045         * The {@code proxy-target-class} attribute as found on AOP-related XML tags.
046         */
047        public static final String PROXY_TARGET_CLASS_ATTRIBUTE = "proxy-target-class";
048
049        /**
050         * The {@code expose-proxy} attribute as found on AOP-related XML tags.
051         */
052        private static final String EXPOSE_PROXY_ATTRIBUTE = "expose-proxy";
053
054
055        public static void registerAutoProxyCreatorIfNecessary(
056                        ParserContext parserContext, Element sourceElement) {
057
058                BeanDefinition beanDefinition = AopConfigUtils.registerAutoProxyCreatorIfNecessary(
059                                parserContext.getRegistry(), parserContext.extractSource(sourceElement));
060                useClassProxyingIfNecessary(parserContext.getRegistry(), sourceElement);
061                registerComponentIfNecessary(beanDefinition, parserContext);
062        }
063
064        public static void registerAspectJAutoProxyCreatorIfNecessary(
065                        ParserContext parserContext, Element sourceElement) {
066
067                BeanDefinition beanDefinition = AopConfigUtils.registerAspectJAutoProxyCreatorIfNecessary(
068                                parserContext.getRegistry(), parserContext.extractSource(sourceElement));
069                useClassProxyingIfNecessary(parserContext.getRegistry(), sourceElement);
070                registerComponentIfNecessary(beanDefinition, parserContext);
071        }
072
073        public static void registerAspectJAnnotationAutoProxyCreatorIfNecessary(
074                        ParserContext parserContext, Element sourceElement) {
075
076                BeanDefinition beanDefinition = AopConfigUtils.registerAspectJAnnotationAutoProxyCreatorIfNecessary(
077                                parserContext.getRegistry(), parserContext.extractSource(sourceElement));
078                useClassProxyingIfNecessary(parserContext.getRegistry(), sourceElement);
079                registerComponentIfNecessary(beanDefinition, parserContext);
080        }
081
082        private static void useClassProxyingIfNecessary(BeanDefinitionRegistry registry, Element sourceElement) {
083                if (sourceElement != null) {
084                        boolean proxyTargetClass = Boolean.parseBoolean(sourceElement.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE));
085                        if (proxyTargetClass) {
086                                AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
087                        }
088                        boolean exposeProxy = Boolean.parseBoolean(sourceElement.getAttribute(EXPOSE_PROXY_ATTRIBUTE));
089                        if (exposeProxy) {
090                                AopConfigUtils.forceAutoProxyCreatorToExposeProxy(registry);
091                        }
092                }
093        }
094
095        private static void registerComponentIfNecessary(BeanDefinition beanDefinition, ParserContext parserContext) {
096                if (beanDefinition != null) {
097                        parserContext.registerComponent(
098                                        new BeanComponentDefinition(beanDefinition, AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME));
099                }
100        }
101
102}