下面列出了org.springframework.beans.factory.config.BeanDefinition#getAttribute ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Get the refresh check delay for the given {@link ScriptFactory} {@link BeanDefinition}.
* If the {@link BeanDefinition} has a
* {@link org.springframework.core.AttributeAccessor metadata attribute}
* under the key {@link #REFRESH_CHECK_DELAY_ATTRIBUTE} which is a valid {@link Number}
* type, then this value is used. Otherwise, the the {@link #defaultRefreshCheckDelay}
* value is used.
* @param beanDefinition the BeanDefinition to check
* @return the refresh check delay
*/
protected long resolveRefreshCheckDelay(BeanDefinition beanDefinition) {
long refreshCheckDelay = this.defaultRefreshCheckDelay;
Object attributeValue = beanDefinition.getAttribute(REFRESH_CHECK_DELAY_ATTRIBUTE);
if (attributeValue instanceof Number) {
refreshCheckDelay = ((Number) attributeValue).longValue();
}
else if (attributeValue instanceof String) {
refreshCheckDelay = Long.parseLong((String) attributeValue);
}
else if (attributeValue != null) {
throw new BeanDefinitionStoreException("Invalid refresh check delay attribute [" +
REFRESH_CHECK_DELAY_ATTRIBUTE + "] with value '" + attributeValue +
"': needs to be of type Number or String");
}
return refreshCheckDelay;
}
/**
* Get the refresh check delay for the given {@link ScriptFactory} {@link BeanDefinition}.
* If the {@link BeanDefinition} has a
* {@link org.springframework.core.AttributeAccessor metadata attribute}
* under the key {@link #REFRESH_CHECK_DELAY_ATTRIBUTE} which is a valid {@link Number}
* type, then this value is used. Otherwise, the {@link #defaultRefreshCheckDelay}
* value is used.
* @param beanDefinition the BeanDefinition to check
* @return the refresh check delay
*/
protected long resolveRefreshCheckDelay(BeanDefinition beanDefinition) {
long refreshCheckDelay = this.defaultRefreshCheckDelay;
Object attributeValue = beanDefinition.getAttribute(REFRESH_CHECK_DELAY_ATTRIBUTE);
if (attributeValue instanceof Number) {
refreshCheckDelay = ((Number) attributeValue).longValue();
}
else if (attributeValue instanceof String) {
refreshCheckDelay = Long.parseLong((String) attributeValue);
}
else if (attributeValue != null) {
throw new BeanDefinitionStoreException("Invalid refresh check delay attribute [" +
REFRESH_CHECK_DELAY_ATTRIBUTE + "] with value '" + attributeValue +
"': needs to be of type Number or String");
}
return refreshCheckDelay;
}
/**
* Determine the original target class for the specified bean, if possible,
* otherwise falling back to a regular {@code getType} lookup.
* @param beanFactory the containing ConfigurableListableBeanFactory
* @param beanName the name of the bean
* @return the original target class as stored in the bean definition, if any
* @since 4.2.3
* @see org.springframework.beans.factory.BeanFactory#getType(String)
*/
@Nullable
public static Class<?> determineTargetClass(
ConfigurableListableBeanFactory beanFactory, @Nullable String beanName) {
if (beanName == null) {
return null;
}
if (beanFactory.containsBeanDefinition(beanName)) {
BeanDefinition bd = beanFactory.getMergedBeanDefinition(beanName);
Class<?> targetClass = (Class<?>) bd.getAttribute(ORIGINAL_TARGET_CLASS_ATTRIBUTE);
if (targetClass != null) {
return targetClass;
}
}
return beanFactory.getType(beanName);
}
/**
* Get the refresh check delay for the given {@link ScriptFactory} {@link BeanDefinition}.
* If the {@link BeanDefinition} has a
* {@link org.springframework.core.AttributeAccessor metadata attribute}
* under the key {@link #REFRESH_CHECK_DELAY_ATTRIBUTE} which is a valid {@link Number}
* type, then this value is used. Otherwise, the {@link #defaultRefreshCheckDelay}
* value is used.
* @param beanDefinition the BeanDefinition to check
* @return the refresh check delay
*/
protected long resolveRefreshCheckDelay(BeanDefinition beanDefinition) {
long refreshCheckDelay = this.defaultRefreshCheckDelay;
Object attributeValue = beanDefinition.getAttribute(REFRESH_CHECK_DELAY_ATTRIBUTE);
if (attributeValue instanceof Number) {
refreshCheckDelay = ((Number) attributeValue).longValue();
}
else if (attributeValue instanceof String) {
refreshCheckDelay = Long.parseLong((String) attributeValue);
}
else if (attributeValue != null) {
throw new BeanDefinitionStoreException("Invalid refresh check delay attribute [" +
REFRESH_CHECK_DELAY_ATTRIBUTE + "] with value '" + attributeValue +
"': needs to be of type Number or String");
}
return refreshCheckDelay;
}
protected boolean resolveProxyTargetClass(BeanDefinition beanDefinition) {
boolean proxyTargetClass = this.defaultProxyTargetClass;
Object attributeValue = beanDefinition.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE);
if (attributeValue instanceof Boolean) {
proxyTargetClass = (Boolean) attributeValue;
}
else if (attributeValue instanceof String) {
proxyTargetClass = Boolean.valueOf((String) attributeValue);
}
else if (attributeValue != null) {
throw new BeanDefinitionStoreException("Invalid proxy target class attribute [" +
PROXY_TARGET_CLASS_ATTRIBUTE + "] with value '" + attributeValue +
"': needs to be of type Boolean or String");
}
return proxyTargetClass;
}
/**
* Get the refresh check delay for the given {@link ScriptFactory} {@link BeanDefinition}.
* If the {@link BeanDefinition} has a
* {@link org.springframework.core.AttributeAccessor metadata attribute}
* under the key {@link #REFRESH_CHECK_DELAY_ATTRIBUTE} which is a valid {@link Number}
* type, then this value is used. Otherwise, the {@link #defaultRefreshCheckDelay}
* value is used.
* @param beanDefinition the BeanDefinition to check
* @return the refresh check delay
*/
protected long resolveRefreshCheckDelay(BeanDefinition beanDefinition) {
long refreshCheckDelay = this.defaultRefreshCheckDelay;
Object attributeValue = beanDefinition.getAttribute(REFRESH_CHECK_DELAY_ATTRIBUTE);
if (attributeValue instanceof Number) {
refreshCheckDelay = ((Number) attributeValue).longValue();
}
else if (attributeValue instanceof String) {
refreshCheckDelay = Long.parseLong((String) attributeValue);
}
else if (attributeValue != null) {
throw new BeanDefinitionStoreException("Invalid refresh check delay attribute [" +
REFRESH_CHECK_DELAY_ATTRIBUTE + "] with value '" + attributeValue +
"': needs to be of type Number or String");
}
return refreshCheckDelay;
}
protected boolean resolveProxyTargetClass(BeanDefinition beanDefinition) {
boolean proxyTargetClass = this.defaultProxyTargetClass;
Object attributeValue = beanDefinition.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE);
if (attributeValue instanceof Boolean) {
proxyTargetClass = (Boolean) attributeValue;
}
else if (attributeValue instanceof String) {
proxyTargetClass = Boolean.valueOf((String) attributeValue);
}
else if (attributeValue != null) {
throw new BeanDefinitionStoreException("Invalid proxy target class attribute [" +
PROXY_TARGET_CLASS_ATTRIBUTE + "] with value '" + attributeValue +
"': needs to be of type Boolean or String");
}
return proxyTargetClass;
}
/**
* Check whether the given bean definition is not subject to the annotation-based
* required property check as performed by this post-processor.
* <p>The default implementations check for the presence of the
* {@link #SKIP_REQUIRED_CHECK_ATTRIBUTE} attribute in the bean definition, if any.
* It also suggests skipping in case of a bean definition with a "factory-bean"
* reference set, assuming that instance-based factories pre-populate the bean.
* @param beanFactory the BeanFactory to check against
* @param beanName the name of the bean to check against
* @return {@code true} to skip the bean; {@code false} to process it
*/
protected boolean shouldSkip(ConfigurableListableBeanFactory beanFactory, String beanName) {
if (beanFactory == null || !beanFactory.containsBeanDefinition(beanName)) {
return false;
}
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
if (beanDefinition.getFactoryBeanName() != null) {
return true;
}
Object value = beanDefinition.getAttribute(SKIP_REQUIRED_CHECK_ATTRIBUTE);
return (value != null && (Boolean.TRUE.equals(value) || Boolean.valueOf(value.toString())));
}
/**
* Determine the original target class for the specified bean, if possible,
* otherwise falling back to a regular {@code getType} lookup.
* @param beanFactory the containing ConfigurableListableBeanFactory
* @param beanName the name of the bean
* @return the original target class as stored in the bean definition, if any
* @since 4.2.3
* @see org.springframework.beans.factory.BeanFactory#getType(String)
*/
public static Class<?> determineTargetClass(ConfigurableListableBeanFactory beanFactory, String beanName) {
if (beanName == null) {
return null;
}
if (beanFactory.containsBeanDefinition(beanName)) {
BeanDefinition bd = beanFactory.getMergedBeanDefinition(beanName);
Class<?> targetClass = (Class<?>) bd.getAttribute(ORIGINAL_TARGET_CLASS_ATTRIBUTE);
if (targetClass != null) {
return targetClass;
}
}
return beanFactory.getType(beanName);
}
/**
* Check whether the given bean definition is not subject to the annotation-based
* required property check as performed by this post-processor.
* <p>The default implementations check for the presence of the
* {@link #SKIP_REQUIRED_CHECK_ATTRIBUTE} attribute in the bean definition, if any.
* It also suggests skipping in case of a bean definition with a "factory-bean"
* reference set, assuming that instance-based factories pre-populate the bean.
* @param beanFactory the BeanFactory to check against
* @param beanName the name of the bean to check against
* @return {@code true} to skip the bean; {@code false} to process it
*/
protected boolean shouldSkip(ConfigurableListableBeanFactory beanFactory, String beanName) {
if (beanFactory == null || !beanFactory.containsBeanDefinition(beanName)) {
return false;
}
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
if (beanDefinition.getFactoryBeanName() != null) {
return true;
}
Object value = beanDefinition.getAttribute(SKIP_REQUIRED_CHECK_ATTRIBUTE);
return (value != null && (Boolean.TRUE.equals(value) || Boolean.valueOf(value.toString())));
}
/**
* Check whether the given bean definition is not subject to the annotation-based
* required property check as performed by this post-processor.
* <p>The default implementations check for the presence of the
* {@link #SKIP_REQUIRED_CHECK_ATTRIBUTE} attribute in the bean definition, if any.
* It also suggests skipping in case of a bean definition with a "factory-bean"
* reference set, assuming that instance-based factories pre-populate the bean.
* @param beanFactory the BeanFactory to check against
* @param beanName the name of the bean to check against
* @return {@code true} to skip the bean; {@code false} to process it
*/
protected boolean shouldSkip(ConfigurableListableBeanFactory beanFactory, String beanName) {
if (beanFactory == null || !beanFactory.containsBeanDefinition(beanName)) {
return false;
}
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
if (beanDefinition.getFactoryBeanName() != null) {
return true;
}
Object value = beanDefinition.getAttribute(SKIP_REQUIRED_CHECK_ATTRIBUTE);
return (value != null && (Boolean.TRUE.equals(value) || Boolean.valueOf(value.toString())));
}
public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) {
Object inject = bus;
if (inject == null) {
inject = getBusForName(Bus.DEFAULT_BUS_ID, factory, true, null);
} else {
if (!factory.containsBeanDefinition(Bus.DEFAULT_BUS_ID)
&& !factory.containsSingleton(Bus.DEFAULT_BUS_ID)) {
factory.registerSingleton(Bus.DEFAULT_BUS_ID, bus);
}
}
for (String beanName : factory.getBeanDefinitionNames()) {
BeanDefinition beanDefinition = factory.getBeanDefinition(beanName);
BusWiringType type
= (BusWiringType)beanDefinition.getAttribute(AbstractBeanDefinitionParser.WIRE_BUS_ATTRIBUTE);
if (type == null) {
continue;
}
String busname = (String)beanDefinition.getAttribute(AbstractBeanDefinitionParser.WIRE_BUS_NAME);
String create = (String)beanDefinition
.getAttribute(AbstractBeanDefinitionParser.WIRE_BUS_CREATE);
Object inj = inject;
if (busname != null) {
if (bus != null) {
continue;
}
inj = getBusForName(busname, factory, create != null, create);
}
beanDefinition.removeAttribute(AbstractBeanDefinitionParser.WIRE_BUS_NAME);
beanDefinition.removeAttribute(AbstractBeanDefinitionParser.WIRE_BUS_ATTRIBUTE);
beanDefinition.removeAttribute(AbstractBeanDefinitionParser.WIRE_BUS_CREATE);
if (create == null) {
if (BusWiringType.PROPERTY == type) {
beanDefinition.getPropertyValues()
.addPropertyValue("bus", inj);
} else if (BusWiringType.CONSTRUCTOR == type) {
ConstructorArgumentValues constructorArgs = beanDefinition.getConstructorArgumentValues();
insertConstructorArg(constructorArgs, inj);
}
}
}
}
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
// We only apply special treatment to ScriptFactory implementations here.
if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
return null;
}
Assert.state(this.beanFactory != null, "No BeanFactory set");
BeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName);
String scriptFactoryBeanName = SCRIPT_FACTORY_NAME_PREFIX + beanName;
String scriptedObjectBeanName = SCRIPTED_OBJECT_NAME_PREFIX + beanName;
prepareScriptBeans(bd, scriptFactoryBeanName, scriptedObjectBeanName);
ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
boolean isFactoryBean = false;
try {
Class<?> scriptedObjectType = scriptFactory.getScriptedObjectType(scriptSource);
// Returned type may be null if the factory is unable to determine the type.
if (scriptedObjectType != null) {
isFactoryBean = FactoryBean.class.isAssignableFrom(scriptedObjectType);
}
}
catch (Exception ex) {
throw new BeanCreationException(beanName,
"Could not determine scripted object type for " + scriptFactory, ex);
}
long refreshCheckDelay = resolveRefreshCheckDelay(bd);
if (refreshCheckDelay >= 0) {
Class<?>[] interfaces = scriptFactory.getScriptInterfaces();
RefreshableScriptTargetSource ts = new RefreshableScriptTargetSource(this.scriptBeanFactory,
scriptedObjectBeanName, scriptFactory, scriptSource, isFactoryBean);
boolean proxyTargetClass = resolveProxyTargetClass(bd);
String language = (String) bd.getAttribute(LANGUAGE_ATTRIBUTE);
if (proxyTargetClass && (language == null || !language.equals("groovy"))) {
throw new BeanDefinitionValidationException(
"Cannot use proxyTargetClass=true with script beans where language is not 'groovy': '" +
language + "'");
}
ts.setRefreshCheckDelay(refreshCheckDelay);
return createRefreshableProxy(ts, interfaces, proxyTargetClass);
}
if (isFactoryBean) {
scriptedObjectBeanName = BeanFactory.FACTORY_BEAN_PREFIX + scriptedObjectBeanName;
}
return this.scriptBeanFactory.getBean(scriptedObjectBeanName);
}
private static <T> T getAttribute(BeanDefinition beanDefinition, String attributeName) {
return (T) beanDefinition.getAttribute(attributeName);
}
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
// We only apply special treatment to ScriptFactory implementations here.
if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
return null;
}
BeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName);
String scriptFactoryBeanName = SCRIPT_FACTORY_NAME_PREFIX + beanName;
String scriptedObjectBeanName = SCRIPTED_OBJECT_NAME_PREFIX + beanName;
prepareScriptBeans(bd, scriptFactoryBeanName, scriptedObjectBeanName);
ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
boolean isFactoryBean = false;
try {
Class<?> scriptedObjectType = scriptFactory.getScriptedObjectType(scriptSource);
// Returned type may be null if the factory is unable to determine the type.
if (scriptedObjectType != null) {
isFactoryBean = FactoryBean.class.isAssignableFrom(scriptedObjectType);
}
}
catch (Exception ex) {
throw new BeanCreationException(beanName,
"Could not determine scripted object type for " + scriptFactory, ex);
}
long refreshCheckDelay = resolveRefreshCheckDelay(bd);
if (refreshCheckDelay >= 0) {
Class<?>[] interfaces = scriptFactory.getScriptInterfaces();
RefreshableScriptTargetSource ts = new RefreshableScriptTargetSource(this.scriptBeanFactory,
scriptedObjectBeanName, scriptFactory, scriptSource, isFactoryBean);
boolean proxyTargetClass = resolveProxyTargetClass(bd);
String language = (String) bd.getAttribute(LANGUAGE_ATTRIBUTE);
if (proxyTargetClass && (language == null || !language.equals("groovy"))) {
throw new BeanDefinitionValidationException(
"Cannot use proxyTargetClass=true with script beans where language is not 'groovy': '" +
language + "'");
}
ts.setRefreshCheckDelay(refreshCheckDelay);
return createRefreshableProxy(ts, interfaces, proxyTargetClass);
}
if (isFactoryBean) {
scriptedObjectBeanName = BeanFactory.FACTORY_BEAN_PREFIX + scriptedObjectBeanName;
}
return this.scriptBeanFactory.getBean(scriptedObjectBeanName);
}
@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) {
// We only apply special treatment to ScriptFactory implementations here.
if (!ScriptFactory.class.isAssignableFrom(beanClass)) {
return null;
}
BeanDefinition bd = this.beanFactory.getMergedBeanDefinition(beanName);
String scriptFactoryBeanName = SCRIPT_FACTORY_NAME_PREFIX + beanName;
String scriptedObjectBeanName = SCRIPTED_OBJECT_NAME_PREFIX + beanName;
prepareScriptBeans(bd, scriptFactoryBeanName, scriptedObjectBeanName);
ScriptFactory scriptFactory = this.scriptBeanFactory.getBean(scriptFactoryBeanName, ScriptFactory.class);
ScriptSource scriptSource = getScriptSource(scriptFactoryBeanName, scriptFactory.getScriptSourceLocator());
boolean isFactoryBean = false;
try {
Class<?> scriptedObjectType = scriptFactory.getScriptedObjectType(scriptSource);
// Returned type may be null if the factory is unable to determine the type.
if (scriptedObjectType != null) {
isFactoryBean = FactoryBean.class.isAssignableFrom(scriptedObjectType);
}
}
catch (Exception ex) {
throw new BeanCreationException(beanName,
"Could not determine scripted object type for " + scriptFactory, ex);
}
long refreshCheckDelay = resolveRefreshCheckDelay(bd);
if (refreshCheckDelay >= 0) {
Class<?>[] interfaces = scriptFactory.getScriptInterfaces();
RefreshableScriptTargetSource ts = new RefreshableScriptTargetSource(this.scriptBeanFactory,
scriptedObjectBeanName, scriptFactory, scriptSource, isFactoryBean);
boolean proxyTargetClass = resolveProxyTargetClass(bd);
String language = (String) bd.getAttribute(LANGUAGE_ATTRIBUTE);
if (proxyTargetClass && (language == null || !language.equals("groovy"))) {
throw new BeanDefinitionValidationException(
"Cannot use proxyTargetClass=true with script beans where language is not 'groovy': '" +
language + "'");
}
ts.setRefreshCheckDelay(refreshCheckDelay);
return createRefreshableProxy(ts, interfaces, proxyTargetClass);
}
if (isFactoryBean) {
scriptedObjectBeanName = BeanFactory.FACTORY_BEAN_PREFIX + scriptedObjectBeanName;
}
return this.scriptBeanFactory.getBean(scriptedObjectBeanName);
}
/**
* Determine the order for the given configuration class bean definition,
* as set by {@link #checkConfigurationClassCandidate}.
* @param beanDef the bean definition to check
* @return the {@link Order @Order} annotation value on the configuration class,
* or {@link Ordered#LOWEST_PRECEDENCE} if none declared
* @since 4.2
*/
public static int getOrder(BeanDefinition beanDef) {
Integer order = (Integer) beanDef.getAttribute(ORDER_ATTRIBUTE);
return (order != null ? order : Ordered.LOWEST_PRECEDENCE);
}
/**
* Determine the order for the given configuration class bean definition,
* as set by {@link #checkConfigurationClassCandidate}.
* @param beanDef the bean definition to check
* @return the {@link Order @Order} annotation value on the configuration class,
* or {@link Ordered#LOWEST_PRECEDENCE} if none declared
* @since 4.2
*/
public static int getOrder(BeanDefinition beanDef) {
Integer order = (Integer) beanDef.getAttribute(ORDER_ATTRIBUTE);
return (order != null ? order : Ordered.LOWEST_PRECEDENCE);
}
/**
* Determine the order for the given configuration class bean definition,
* as set by {@link #checkConfigurationClassCandidate}.
* @param beanDef the bean definition to check
* @return the {@link @Order} annotation value on the configuration class,
* or {@link Ordered#LOWEST_PRECEDENCE} if none declared
* @since 4.2
*/
public static int getOrder(BeanDefinition beanDef) {
Integer order = (Integer) beanDef.getAttribute(ORDER_ATTRIBUTE);
return (order != null ? order : Ordered.LOWEST_PRECEDENCE);
}
/**
* Determine the order for the given configuration class bean definition,
* as set by {@link #checkConfigurationClassCandidate}.
* @param beanDef the bean definition to check
* @return the {@link @Order} annotation value on the configuration class,
* or {@link Ordered#LOWEST_PRECEDENCE} if none declared
* @since 4.2
*/
public static int getOrder(BeanDefinition beanDef) {
Integer order = (Integer) beanDef.getAttribute(ORDER_ATTRIBUTE);
return (order != null ? order : Ordered.LOWEST_PRECEDENCE);
}