/** * Return if there are constructor argument values defined for this bean. * @since 5.0.2 */ default boolean hasConstructorArgumentValues() { return !getConstructorArgumentValues().isEmpty(); }
/** * Return if there are constructor argument values defined for this bean. * @since 5.0.2 */ default boolean hasConstructorArgumentValues() { return !getConstructorArgumentValues().isEmpty(); }
/** * Register a bean from the given bean class, deriving its metadata from * class-declared annotations, and optionally providing explicit constructor * arguments for consideration in the autowiring process. * @param beanName the name of the bean (may be {@code null}) * @param annotatedClass the class of the bean * @param constructorArguments argument values to be fed into Spring's * constructor resolution algorithm, resolving either all arguments or just * specific ones, with the rest to be resolved through regular autowiring * (may be {@code null} or empty) * @since 5.0 */ public <T> void registerBean(@Nullable String beanName, Class<T> annotatedClass, Object... constructorArguments) { this.reader.doRegisterBean(annotatedClass, null, beanName, null, bd -> { for (Object arg : constructorArguments) { bd.getConstructorArgumentValues().addGenericArgumentValue(arg); } }); }
public void addConstructorArgs(Object value, Class type, BeanDefinition bd) { ConstructorArgumentValues.ValueHolder vHolder = new ConstructorArgumentValues.ValueHolder(value, type.getName()); ConstructorArgumentValues args = bd.getConstructorArgumentValues(); args.addIndexedArgumentValue(args.getArgumentCount(), vHolder); }
public void addConstructorArgs(Object value, Class type, BeanDefinition bd) { ConstructorArgumentValues.ValueHolder vHolder = new ConstructorArgumentValues.ValueHolder(value, type.getName()); ConstructorArgumentValues args = bd.getConstructorArgumentValues(); args.addIndexedArgumentValue(args.getArgumentCount(), vHolder); }
/** * Create a ScriptFactory bean definition based on the given script definition, * extracting only the definition data that is relevant for the ScriptFactory * (that is, only bean class and constructor arguments). * @param bd the full script bean definition * @return the extracted ScriptFactory bean definition * @see org.springframework.scripting.ScriptFactory */ protected BeanDefinition createScriptFactoryBeanDefinition(BeanDefinition bd) { GenericBeanDefinition scriptBd = new GenericBeanDefinition(); scriptBd.setBeanClassName(bd.getBeanClassName()); scriptBd.getConstructorArgumentValues().addArgumentValues(bd.getConstructorArgumentValues()); return scriptBd; }
private void setJobPropertiesBean(Properties properties) { if (batchArtifactType.equals(BatchArtifactType.JOB)) { Map<String, String> jobProperties = new HashMap<>(); if (properties != null && !properties.isEmpty()) { for (String param : properties.stringPropertyNames()) { jobProperties.put(param, properties.getProperty(param)); } } BeanDefinition jobPropertiesBeanDefinition = parserContext.getRegistry().getBeanDefinition(JOB_PROPERTIES_BEAN_NAME); jobPropertiesBeanDefinition.getConstructorArgumentValues().addGenericArgumentValue(jobProperties); } }
/** * Register a bean from the given bean class, deriving its metadata from * class-declared annotations, and optionally providing explicit constructor * arguments for consideration in the autowiring process. * @param beanName the name of the bean (may be {@code null}) * @param annotatedClass the class of the bean * @param constructorArguments argument values to be fed into Spring's * constructor resolution algorithm, resolving either all arguments or just * specific ones, with the rest to be resolved through regular autowiring * (may be {@code null} or empty) * @since 5.0 */ public <T> void registerBean(@Nullable String beanName, Class<T> annotatedClass, Object... constructorArguments) { this.reader.doRegisterBean(annotatedClass, null, beanName, null, bd -> { for (Object arg : constructorArguments) { bd.getConstructorArgumentValues().addGenericArgumentValue(arg); } }); }
/** * Registers a {@code Map<String, CorsConfiguration>} (mapped {@code CorsConfiguration}s) * under a well-known name unless already registered. The bean definition may be updated * if a non-null CORS configuration is provided. * @return a RuntimeBeanReference to this {@code Map<String, CorsConfiguration>} instance */ public static RuntimeBeanReference registerCorsConfigurations( @Nullable Map<String, CorsConfiguration> corsConfigurations, ParserContext context, @Nullable Object source) { if (!context.getRegistry().containsBeanDefinition(CORS_CONFIGURATION_BEAN_NAME)) { RootBeanDefinition corsDef = new RootBeanDefinition(LinkedHashMap.class); corsDef.setSource(source); corsDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); if (corsConfigurations != null) { corsDef.getConstructorArgumentValues().addIndexedArgumentValue(0, corsConfigurations); } context.getReaderContext().getRegistry().registerBeanDefinition(CORS_CONFIGURATION_BEAN_NAME, corsDef); context.registerComponent(new BeanComponentDefinition(corsDef, CORS_CONFIGURATION_BEAN_NAME)); } else if (corsConfigurations != null) { BeanDefinition corsDef = context.getRegistry().getBeanDefinition(CORS_CONFIGURATION_BEAN_NAME); corsDef.getConstructorArgumentValues().addIndexedArgumentValue(0, corsConfigurations); } return new RuntimeBeanReference(CORS_CONFIGURATION_BEAN_NAME); }
@SuppressWarnings({"unchecked"}) public static List<BeanMetadataElement> findFilterChain(ParserContext parserContext, String explicitRef) { String filterChainRef = explicitRef; if (!StringUtils.hasText(filterChainRef)) { filterChainRef = findDefaultFilterChainBeanId(parserContext); } return (List<BeanMetadataElement>) parserContext.getRegistry().getBeanDefinition(filterChainRef).getConstructorArgumentValues().getArgumentValue(1,List.class).getValue(); }
@SuppressWarnings({"unchecked"}) public static List<BeanMetadataElement> findFilterChain(ParserContext parserContext, String explicitRef) { String filterChainRef = explicitRef; if (!StringUtils.hasText(filterChainRef)) { filterChainRef = findDefaultFilterChainBeanId(parserContext); } return (List<BeanMetadataElement>) parserContext.getRegistry().getBeanDefinition(filterChainRef).getConstructorArgumentValues().getArgumentValue(1,List.class).getValue(); }
/** * Create a ScriptFactory bean definition based on the given script definition, * extracting only the definition data that is relevant for the ScriptFactory * (that is, only bean class and constructor arguments). * @param bd the full script bean definition * @return the extracted ScriptFactory bean definition * @see org.springframework.scripting.ScriptFactory */ protected BeanDefinition createScriptFactoryBeanDefinition(BeanDefinition bd) { GenericBeanDefinition scriptBd = new GenericBeanDefinition(); scriptBd.setBeanClassName(bd.getBeanClassName()); scriptBd.getConstructorArgumentValues().addArgumentValues(bd.getConstructorArgumentValues()); return scriptBd; }
String argValue = StringUtils.trimWhitespace(attr.getValue()); ConstructorArgumentValues cvs = definition.getBeanDefinition().getConstructorArgumentValues(); boolean ref = false;
private void updatePostProcessor(BeanDefinitionRegistry registry, Set<String> packagesToScan) { BeanDefinition definition = registry.getBeanDefinition(BEAN_NAME); ValueHolder constructorArguments = definition.getConstructorArgumentValues() .getGenericArgumentValue(Set.class); @SuppressWarnings("unchecked") Set<String> mergedPackages = (Set<String>) constructorArguments.getValue(); mergedPackages.addAll(packagesToScan); constructorArguments.setValue(mergedPackages); }
private void registerMock(ConfigurableListableBeanFactory beanFactory, BeanDefinitionRegistry registry, MockDefinition definition) { BeanDefinition beanDefinition = createBeanDefinition(definition); String beanName = getBeanName(beanFactory, registry, definition, beanDefinition); String transformedBeanName = BeanFactoryUtils.transformedBeanName(beanName); beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(1, beanName); if (registry.containsBeanDefinition(transformedBeanName)) { registry.removeBeanDefinition(transformedBeanName); } registry.registerBeanDefinition(transformedBeanName, beanDefinition); Object mock = createMock(definition, beanName); beanFactory.registerSingleton(transformedBeanName, mock); registerAliases(beanFactory, definition, transformedBeanName); }
@Override public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { BeanDefinition inbound = registry.getBeanDefinition("clientInboundChannel"); inbound.getConstructorArgumentValues() .addIndexedArgumentValue(0, new RootBeanDefinition(SyncTaskExecutor.class)); }
private static StaticScriptSource getScriptSource(GenericApplicationContext ctx) throws Exception { ScriptFactoryPostProcessor processor = (ScriptFactoryPostProcessor) ctx.getBean(PROCESSOR_BEAN_NAME); BeanDefinition bd = processor.scriptBeanFactory.getBeanDefinition("scriptedObject.messenger"); return (StaticScriptSource) bd.getConstructorArgumentValues().getIndexedArgumentValue(0, StaticScriptSource.class).getValue(); }
/** * Traverse the given BeanDefinition object and the MutablePropertyValues * and ConstructorArgumentValues contained in them. * @param beanDefinition the BeanDefinition object to traverse * @see #resolveStringValue(String) */ public void visitBeanDefinition(BeanDefinition beanDefinition) { visitParentName(beanDefinition); visitBeanClassName(beanDefinition); visitFactoryBeanName(beanDefinition); visitFactoryMethodName(beanDefinition); visitScope(beanDefinition); if (beanDefinition.hasPropertyValues()) { visitPropertyValues(beanDefinition.getPropertyValues()); } if (beanDefinition.hasConstructorArgumentValues()) { ConstructorArgumentValues cas = beanDefinition.getConstructorArgumentValues(); visitIndexedArgumentValues(cas.getIndexedArgumentValues()); visitGenericArgumentValues(cas.getGenericArgumentValues()); } }
setConstructorArgumentValues(new ConstructorArgumentValues(original.getConstructorArgumentValues())); setConstructorArgumentValues(new ConstructorArgumentValues(original.getConstructorArgumentValues())); setPropertyValues(new MutablePropertyValues(original.getPropertyValues())); setResourceDescription(original.getResourceDescription());
@Test public void beanEventReceived() throws Exception { ComponentDefinition componentDefinition1 = this.eventListener.getComponentDefinition("testBean"); assertTrue(componentDefinition1 instanceof BeanComponentDefinition); assertEquals(1, componentDefinition1.getBeanDefinitions().length); BeanDefinition beanDefinition1 = componentDefinition1.getBeanDefinitions()[0]; assertEquals(new TypedStringValue("Rob Harrop"), beanDefinition1.getConstructorArgumentValues().getGenericArgumentValue(String.class).getValue()); assertEquals(1, componentDefinition1.getBeanReferences().length); assertEquals("testBean2", componentDefinition1.getBeanReferences()[0].getBeanName()); assertEquals(1, componentDefinition1.getInnerBeanDefinitions().length); BeanDefinition innerBd1 = componentDefinition1.getInnerBeanDefinitions()[0]; assertEquals(new TypedStringValue("ACME"), innerBd1.getConstructorArgumentValues().getGenericArgumentValue(String.class).getValue()); assertTrue(componentDefinition1.getSource() instanceof Element); ComponentDefinition componentDefinition2 = this.eventListener.getComponentDefinition("testBean2"); assertTrue(componentDefinition2 instanceof BeanComponentDefinition); assertEquals(1, componentDefinition1.getBeanDefinitions().length); BeanDefinition beanDefinition2 = componentDefinition2.getBeanDefinitions()[0]; assertEquals(new TypedStringValue("Juergen Hoeller"), beanDefinition2.getPropertyValues().getPropertyValue("name").getValue()); assertEquals(0, componentDefinition2.getBeanReferences().length); assertEquals(1, componentDefinition2.getInnerBeanDefinitions().length); BeanDefinition innerBd2 = componentDefinition2.getInnerBeanDefinitions()[0]; assertEquals(new TypedStringValue("Eva Schallmeiner"), innerBd2.getPropertyValues().getPropertyValue("name").getValue()); assertTrue(componentDefinition2.getSource() instanceof Element); }