/** * 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 whether the given bean definition indicates a full {@code @Configuration} * class, through checking {@link #checkConfigurationClassCandidate}'s metadata marker. */ public static boolean isFullConfigurationClass(BeanDefinition beanDef) { return CONFIGURATION_CLASS_FULL.equals(beanDef.getAttribute(CONFIGURATION_CLASS_ATTRIBUTE)); }
/** * Determine whether the given bean definition indicates a lite {@code @Configuration} * class, through checking {@link #checkConfigurationClassCandidate}'s metadata marker. */ public static boolean isLiteConfigurationClass(BeanDefinition beanDef) { return CONFIGURATION_CLASS_LITE.equals(beanDef.getAttribute(CONFIGURATION_CLASS_ATTRIBUTE)); }
/** * 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 whether the given bean definition indicates a full {@code @Configuration} * class, through checking {@link #checkConfigurationClassCandidate}'s metadata marker. */ public static boolean isFullConfigurationClass(BeanDefinition beanDef) { return CONFIGURATION_CLASS_FULL.equals(beanDef.getAttribute(CONFIGURATION_CLASS_ATTRIBUTE)); }
/** * Determine whether the given bean definition indicates a lite {@code @Configuration} * class, through checking {@link #checkConfigurationClassCandidate}'s metadata marker. */ public static boolean isLiteConfigurationClass(BeanDefinition beanDef) { return CONFIGURATION_CLASS_LITE.equals(beanDef.getAttribute(CONFIGURATION_CLASS_ATTRIBUTE)); }
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; }
/** * Determine whether the given bean should be proxied with its target * class rather than its interfaces. Checks the * {@link #PRESERVE_TARGET_CLASS_ATTRIBUTE "preserveTargetClass" attribute} * of the corresponding bean definition. * @param beanFactory the containing ConfigurableListableBeanFactory * @param beanName the name of the bean * @return whether the given bean should be proxied with its target class */ public static boolean shouldProxyTargetClass( ConfigurableListableBeanFactory beanFactory, @Nullable String beanName) { if (beanName != null && beanFactory.containsBeanDefinition(beanName)) { BeanDefinition bd = beanFactory.getBeanDefinition(beanName); return Boolean.TRUE.equals(bd.getAttribute(PRESERVE_TARGET_CLASS_ATTRIBUTE)); } return false; }
/** * 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; }
/** * 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(@Nullable 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()))); }
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; }
/** * @param beanName a bean definition name * @param attributeName the name of the property * @param beanFactory a {@link BeanFactory} * @return The value for the attribute of the bean. Search parent hierarchy * if necessary. Return null if none is found. */ public static Object getAttribute(String beanName, String attributeName, ConfigurableListableBeanFactory beanFactory) { return beanFactory.getMergedBeanDefinition(beanName).getAttribute(attributeName); } }
/** * 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); }
@Test public void metadataIsInherited() throws Exception { BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("testBean2"); assertEquals("Metadata not inherited", "bar", beanDefinition.getAttribute("foo")); assertEquals("Child metdata not attached", "123", beanDefinition.getAttribute("abc")); }
@Override public boolean isAutowireCandidate(BeanDefinitionHolder bdHolder, DependencyDescriptor descriptor) { if (!bdHolder.getBeanDefinition().isAutowireCandidate()) { return false; } if (!bdHolder.getBeanName().matches("[a-z-]+")) { return false; } if (bdHolder.getBeanDefinition().getAttribute("priority").equals("1")) { return true; } return false; }
@Test public void testDecorationViaAttribute() throws Exception { BeanDefinition beanDefinition = this.beanFactory.getBeanDefinition("decorateWithAttribute"); assertEquals("foo", beanDefinition.getAttribute("objectName")); }
@Test public void metadataAttachment() throws Exception { BeanDefinition beanDefinition1 = this.beanFactory.getMergedBeanDefinition("testBean1"); assertEquals("bar", beanDefinition1.getAttribute("foo")); }
getRegistry().getBeanDefinition(name).getAttribute(GroovyBeanDefinitionWrapper.class.getName()); if (beanDefinition != null) { return new GroovyRuntimeBeanReference(name, beanDefinition, false);
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(