001/* 002 * Copyright 2002-2017 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.scope; 018 019import org.springframework.aop.framework.autoproxy.AutoProxyUtils; 020import org.springframework.beans.factory.config.BeanDefinition; 021import org.springframework.beans.factory.config.BeanDefinitionHolder; 022import org.springframework.beans.factory.support.AbstractBeanDefinition; 023import org.springframework.beans.factory.support.BeanDefinitionRegistry; 024import org.springframework.beans.factory.support.RootBeanDefinition; 025 026/** 027 * Utility class for creating a scoped proxy. 028 * Used by ScopedProxyBeanDefinitionDecorator and ClassPathBeanDefinitionScanner. 029 * 030 * @author Mark Fisher 031 * @author Juergen Hoeller 032 * @author Rob Harrop 033 * @since 2.5 034 */ 035public abstract class ScopedProxyUtils { 036 037 private static final String TARGET_NAME_PREFIX = "scopedTarget."; 038 039 040 /** 041 * Generate a scoped proxy for the supplied target bean, registering the target 042 * bean with an internal name and setting 'targetBeanName' on the scoped proxy. 043 * @param definition the original bean definition 044 * @param registry the bean definition registry 045 * @param proxyTargetClass whether to create a target class proxy 046 * @return the scoped proxy definition 047 */ 048 public static BeanDefinitionHolder createScopedProxy(BeanDefinitionHolder definition, 049 BeanDefinitionRegistry registry, boolean proxyTargetClass) { 050 051 String originalBeanName = definition.getBeanName(); 052 BeanDefinition targetDefinition = definition.getBeanDefinition(); 053 String targetBeanName = getTargetBeanName(originalBeanName); 054 055 // Create a scoped proxy definition for the original bean name, 056 // "hiding" the target bean in an internal target definition. 057 RootBeanDefinition proxyDefinition = new RootBeanDefinition(ScopedProxyFactoryBean.class); 058 proxyDefinition.setDecoratedDefinition(new BeanDefinitionHolder(targetDefinition, targetBeanName)); 059 proxyDefinition.setOriginatingBeanDefinition(targetDefinition); 060 proxyDefinition.setSource(definition.getSource()); 061 proxyDefinition.setRole(targetDefinition.getRole()); 062 063 proxyDefinition.getPropertyValues().add("targetBeanName", targetBeanName); 064 if (proxyTargetClass) { 065 targetDefinition.setAttribute(AutoProxyUtils.PRESERVE_TARGET_CLASS_ATTRIBUTE, Boolean.TRUE); 066 // ScopedProxyFactoryBean's "proxyTargetClass" default is TRUE, so we don't need to set it explicitly here. 067 } 068 else { 069 proxyDefinition.getPropertyValues().add("proxyTargetClass", Boolean.FALSE); 070 } 071 072 // Copy autowire settings from original bean definition. 073 proxyDefinition.setAutowireCandidate(targetDefinition.isAutowireCandidate()); 074 proxyDefinition.setPrimary(targetDefinition.isPrimary()); 075 if (targetDefinition instanceof AbstractBeanDefinition) { 076 proxyDefinition.copyQualifiersFrom((AbstractBeanDefinition) targetDefinition); 077 } 078 079 // The target bean should be ignored in favor of the scoped proxy. 080 targetDefinition.setAutowireCandidate(false); 081 targetDefinition.setPrimary(false); 082 083 // Register the target bean as separate bean in the factory. 084 registry.registerBeanDefinition(targetBeanName, targetDefinition); 085 086 // Return the scoped proxy definition as primary bean definition 087 // (potentially an inner bean). 088 return new BeanDefinitionHolder(proxyDefinition, originalBeanName, definition.getAliases()); 089 } 090 091 /** 092 * Generate the bean name that is used within the scoped proxy to reference the target bean. 093 * @param originalBeanName the original name of bean 094 * @return the generated bean to be used to reference the target bean 095 */ 096 public static String getTargetBeanName(String originalBeanName) { 097 return TARGET_NAME_PREFIX + originalBeanName; 098 } 099 100 /** 101 * Specify if the {@code beanName} is the name of a bean that references the target 102 * bean within a scoped proxy. 103 * @since 4.1.4 104 */ 105 public static boolean isScopedTarget(String beanName) { 106 return (beanName != null && beanName.startsWith(TARGET_NAME_PREFIX)); 107 } 108 109}