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