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.beans.factory.config; 018 019import java.util.LinkedHashMap; 020import java.util.Map; 021 022import org.springframework.beans.BeanUtils; 023import org.springframework.beans.BeansException; 024import org.springframework.beans.factory.BeanClassLoaderAware; 025import org.springframework.core.Ordered; 026import org.springframework.lang.Nullable; 027import org.springframework.util.Assert; 028import org.springframework.util.ClassUtils; 029 030/** 031 * Simple {@link BeanFactoryPostProcessor} implementation that registers 032 * custom {@link Scope Scope(s)} with the containing {@link ConfigurableBeanFactory}. 033 * 034 * <p>Will register all of the supplied {@link #setScopes(java.util.Map) scopes} 035 * with the {@link ConfigurableListableBeanFactory} that is passed to the 036 * {@link #postProcessBeanFactory(ConfigurableListableBeanFactory)} method. 037 * 038 * <p>This class allows for <i>declarative</i> registration of custom scopes. 039 * Alternatively, consider implementing a custom {@link BeanFactoryPostProcessor} 040 * that calls {@link ConfigurableBeanFactory#registerScope} programmatically. 041 * 042 * @author Juergen Hoeller 043 * @author Rick Evans 044 * @since 2.0 045 * @see ConfigurableBeanFactory#registerScope 046 */ 047public class CustomScopeConfigurer implements BeanFactoryPostProcessor, BeanClassLoaderAware, Ordered { 048 049 @Nullable 050 private Map<String, Object> scopes; 051 052 private int order = Ordered.LOWEST_PRECEDENCE; 053 054 @Nullable 055 private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader(); 056 057 058 /** 059 * Specify the custom scopes that are to be registered. 060 * <p>The keys indicate the scope names (of type String); each value 061 * is expected to be the corresponding custom {@link Scope} instance 062 * or class name. 063 */ 064 public void setScopes(Map<String, Object> scopes) { 065 this.scopes = scopes; 066 } 067 068 /** 069 * Add the given scope to this configurer's map of scopes. 070 * @param scopeName the name of the scope 071 * @param scope the scope implementation 072 * @since 4.1.1 073 */ 074 public void addScope(String scopeName, Scope scope) { 075 if (this.scopes == null) { 076 this.scopes = new LinkedHashMap<>(1); 077 } 078 this.scopes.put(scopeName, scope); 079 } 080 081 082 public void setOrder(int order) { 083 this.order = order; 084 } 085 086 @Override 087 public int getOrder() { 088 return this.order; 089 } 090 091 @Override 092 public void setBeanClassLoader(@Nullable ClassLoader beanClassLoader) { 093 this.beanClassLoader = beanClassLoader; 094 } 095 096 097 @Override 098 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { 099 if (this.scopes != null) { 100 this.scopes.forEach((scopeKey, value) -> { 101 if (value instanceof Scope) { 102 beanFactory.registerScope(scopeKey, (Scope) value); 103 } 104 else if (value instanceof Class) { 105 Class<?> scopeClass = (Class<?>) value; 106 Assert.isAssignable(Scope.class, scopeClass, "Invalid scope class"); 107 beanFactory.registerScope(scopeKey, (Scope) BeanUtils.instantiateClass(scopeClass)); 108 } 109 else if (value instanceof String) { 110 Class<?> scopeClass = ClassUtils.resolveClassName((String) value, this.beanClassLoader); 111 Assert.isAssignable(Scope.class, scopeClass, "Invalid scope class"); 112 beanFactory.registerScope(scopeKey, (Scope) BeanUtils.instantiateClass(scopeClass)); 113 } 114 else { 115 throw new IllegalArgumentException("Mapped value [" + value + "] for scope key [" + 116 scopeKey + "] is not an instance of required type [" + Scope.class.getName() + 117 "] or a corresponding Class or String value indicating a Scope implementation"); 118 } 119 }); 120 } 121 } 122 123}