001/* 002 * Copyright 2002-2020 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.support; 018 019import org.springframework.lang.Nullable; 020import org.springframework.util.StringUtils; 021 022/** 023 * A simple holder for {@code BeanDefinition} property defaults. 024 * 025 * @author Mark Fisher 026 * @author Juergen Hoeller 027 * @since 2.5 028 * @see AbstractBeanDefinition#applyDefaults 029 */ 030public class BeanDefinitionDefaults { 031 032 @Nullable 033 private Boolean lazyInit; 034 035 private int autowireMode = AbstractBeanDefinition.AUTOWIRE_NO; 036 037 private int dependencyCheck = AbstractBeanDefinition.DEPENDENCY_CHECK_NONE; 038 039 @Nullable 040 private String initMethodName; 041 042 @Nullable 043 private String destroyMethodName; 044 045 046 /** 047 * Set whether beans should be lazily initialized by default. 048 * <p>If {@code false}, the bean will get instantiated on startup by bean 049 * factories that perform eager initialization of singletons. 050 * @see AbstractBeanDefinition#setLazyInit 051 */ 052 public void setLazyInit(boolean lazyInit) { 053 this.lazyInit = lazyInit; 054 } 055 056 /** 057 * Return whether beans should be lazily initialized by default, i.e. not 058 * eagerly instantiated on startup. Only applicable to singleton beans. 059 * @return whether to apply lazy-init semantics ({@code false} by default) 060 */ 061 public boolean isLazyInit() { 062 return (this.lazyInit != null && this.lazyInit.booleanValue()); 063 } 064 065 /** 066 * Return whether beans should be lazily initialized by default, i.e. not 067 * eagerly instantiated on startup. Only applicable to singleton beans. 068 * @return the lazy-init flag if explicitly set, or {@code null} otherwise 069 * @since 5.2 070 */ 071 @Nullable 072 public Boolean getLazyInit() { 073 return this.lazyInit; 074 } 075 076 /** 077 * Set the autowire mode. This determines whether any automagical detection 078 * and setting of bean references will happen. Default is AUTOWIRE_NO 079 * which means there won't be convention-based autowiring by name or type 080 * (however, there may still be explicit annotation-driven autowiring). 081 * @param autowireMode the autowire mode to set. 082 * Must be one of the constants defined in {@link AbstractBeanDefinition}. 083 * @see AbstractBeanDefinition#setAutowireMode 084 */ 085 public void setAutowireMode(int autowireMode) { 086 this.autowireMode = autowireMode; 087 } 088 089 /** 090 * Return the default autowire mode. 091 */ 092 public int getAutowireMode() { 093 return this.autowireMode; 094 } 095 096 /** 097 * Set the dependency check code. 098 * @param dependencyCheck the code to set. 099 * Must be one of the constants defined in {@link AbstractBeanDefinition}. 100 * @see AbstractBeanDefinition#setDependencyCheck 101 */ 102 public void setDependencyCheck(int dependencyCheck) { 103 this.dependencyCheck = dependencyCheck; 104 } 105 106 /** 107 * Return the default dependency check code. 108 */ 109 public int getDependencyCheck() { 110 return this.dependencyCheck; 111 } 112 113 /** 114 * Set the name of the default initializer method. 115 * <p>Note that this method is not enforced on all affected bean definitions 116 * but rather taken as an optional callback, to be invoked if actually present. 117 * @see AbstractBeanDefinition#setInitMethodName 118 * @see AbstractBeanDefinition#setEnforceInitMethod 119 */ 120 public void setInitMethodName(@Nullable String initMethodName) { 121 this.initMethodName = (StringUtils.hasText(initMethodName) ? initMethodName : null); 122 } 123 124 /** 125 * Return the name of the default initializer method. 126 */ 127 @Nullable 128 public String getInitMethodName() { 129 return this.initMethodName; 130 } 131 132 /** 133 * Set the name of the default destroy method. 134 * <p>Note that this method is not enforced on all affected bean definitions 135 * but rather taken as an optional callback, to be invoked if actually present. 136 * @see AbstractBeanDefinition#setDestroyMethodName 137 * @see AbstractBeanDefinition#setEnforceDestroyMethod 138 */ 139 public void setDestroyMethodName(@Nullable String destroyMethodName) { 140 this.destroyMethodName = (StringUtils.hasText(destroyMethodName) ? destroyMethodName : null); 141 } 142 143 /** 144 * Return the name of the default destroy method. 145 */ 146 @Nullable 147 public String getDestroyMethodName() { 148 return this.destroyMethodName; 149 } 150 151}