use of org.springframework.beans.factory.config.ConstructorArgumentValues in project spring-framework by spring-projects.
the class QualifierAnnotationAutowireContextTests method autowiredFieldResolvesWithMultipleQualifierValues.
@Test
public void autowiredFieldResolvesWithMultipleQualifierValues() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
cavs1.addGenericArgumentValue(JUERGEN);
RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
AutowireCandidateQualifier qualifier = new AutowireCandidateQualifier(TestQualifierWithMultipleAttributes.class);
qualifier.setAttribute("number", 456);
person1.addQualifier(qualifier);
ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
cavs2.addGenericArgumentValue(MARK);
RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
AutowireCandidateQualifier qualifier2 = new AutowireCandidateQualifier(TestQualifierWithMultipleAttributes.class);
qualifier2.setAttribute("number", 123);
person2.addQualifier(qualifier2);
context.registerBeanDefinition(JUERGEN, person1);
context.registerBeanDefinition(MARK, person2);
context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldWithMultipleAttributesTestBean.class));
AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
context.refresh();
QualifiedFieldWithMultipleAttributesTestBean bean = (QualifiedFieldWithMultipleAttributesTestBean) context.getBean("autowired");
assertEquals(MARK, bean.getPerson().getName());
}
use of org.springframework.beans.factory.config.ConstructorArgumentValues in project spring-framework by spring-projects.
the class QualifierAnnotationAutowireContextTests method autowiredFieldResolvesWithDefaultValueAndExplicitDefaultValueOnBeanDefinition.
@Test
public void autowiredFieldResolvesWithDefaultValueAndExplicitDefaultValueOnBeanDefinition() {
GenericApplicationContext context = new GenericApplicationContext();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
cavs1.addGenericArgumentValue(JUERGEN);
RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
// qualifier added, and value matches the default
person1.addQualifier(new AutowireCandidateQualifier(TestQualifierWithDefaultValue.class, "default"));
ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
cavs2.addGenericArgumentValue(MARK);
RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
context.registerBeanDefinition(JUERGEN, person1);
context.registerBeanDefinition(MARK, person2);
context.registerBeanDefinition("autowired", new RootBeanDefinition(QualifiedFieldWithDefaultValueTestBean.class));
AnnotationConfigUtils.registerAnnotationConfigProcessors(context);
context.refresh();
QualifiedFieldWithDefaultValueTestBean bean = (QualifiedFieldWithDefaultValueTestBean) context.getBean("autowired");
assertEquals(JUERGEN, bean.getPerson().getName());
}
use of org.springframework.beans.factory.config.ConstructorArgumentValues in project spring-framework by spring-projects.
the class ScriptBeanDefinitionParser method parseInternal.
/**
* Parses the dynamic object element and returns the resulting bean definition.
* Registers a {@link ScriptFactoryPostProcessor} if needed.
*/
@Override
@SuppressWarnings("deprecation")
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
// Engine attribute only supported for <lang:std>
String engine = element.getAttribute(ENGINE_ATTRIBUTE);
// Resolve the script source.
String value = resolveScriptSource(element, parserContext.getReaderContext());
if (value == null) {
return null;
}
// Set up infrastructure.
LangNamespaceUtils.registerScriptFactoryPostProcessorIfNecessary(parserContext.getRegistry());
// Create script factory bean definition.
GenericBeanDefinition bd = new GenericBeanDefinition();
bd.setBeanClassName(this.scriptFactoryClassName);
bd.setSource(parserContext.extractSource(element));
bd.setAttribute(ScriptFactoryPostProcessor.LANGUAGE_ATTRIBUTE, element.getLocalName());
// Determine bean scope.
String scope = element.getAttribute(SCOPE_ATTRIBUTE);
if (StringUtils.hasLength(scope)) {
bd.setScope(scope);
}
// Determine autowire mode.
String autowire = element.getAttribute(AUTOWIRE_ATTRIBUTE);
int autowireMode = parserContext.getDelegate().getAutowireMode(autowire);
// Only "byType" and "byName" supported, but maybe other default inherited...
if (autowireMode == GenericBeanDefinition.AUTOWIRE_AUTODETECT) {
autowireMode = GenericBeanDefinition.AUTOWIRE_BY_TYPE;
} else if (autowireMode == GenericBeanDefinition.AUTOWIRE_CONSTRUCTOR) {
autowireMode = GenericBeanDefinition.AUTOWIRE_NO;
}
bd.setAutowireMode(autowireMode);
// Parse depends-on list of bean names.
String dependsOn = element.getAttribute(DEPENDS_ON_ATTRIBUTE);
if (StringUtils.hasLength(dependsOn)) {
bd.setDependsOn(StringUtils.tokenizeToStringArray(dependsOn, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS));
}
// Retrieve the defaults for bean definitions within this parser context
BeanDefinitionDefaults beanDefinitionDefaults = parserContext.getDelegate().getBeanDefinitionDefaults();
// Determine init method and destroy method.
String initMethod = element.getAttribute(INIT_METHOD_ATTRIBUTE);
if (StringUtils.hasLength(initMethod)) {
bd.setInitMethodName(initMethod);
} else if (beanDefinitionDefaults.getInitMethodName() != null) {
bd.setInitMethodName(beanDefinitionDefaults.getInitMethodName());
}
if (element.hasAttribute(DESTROY_METHOD_ATTRIBUTE)) {
String destroyMethod = element.getAttribute(DESTROY_METHOD_ATTRIBUTE);
bd.setDestroyMethodName(destroyMethod);
} else if (beanDefinitionDefaults.getDestroyMethodName() != null) {
bd.setDestroyMethodName(beanDefinitionDefaults.getDestroyMethodName());
}
// Attach any refresh metadata.
String refreshCheckDelay = element.getAttribute(REFRESH_CHECK_DELAY_ATTRIBUTE);
if (StringUtils.hasText(refreshCheckDelay)) {
bd.setAttribute(ScriptFactoryPostProcessor.REFRESH_CHECK_DELAY_ATTRIBUTE, Long.valueOf(refreshCheckDelay));
}
// Attach any proxy target class metadata.
String proxyTargetClass = element.getAttribute(PROXY_TARGET_CLASS_ATTRIBUTE);
if (StringUtils.hasText(proxyTargetClass)) {
bd.setAttribute(ScriptFactoryPostProcessor.PROXY_TARGET_CLASS_ATTRIBUTE, Boolean.valueOf(proxyTargetClass));
}
// Add constructor arguments.
ConstructorArgumentValues cav = bd.getConstructorArgumentValues();
int constructorArgNum = 0;
if (StringUtils.hasLength(engine)) {
cav.addIndexedArgumentValue(constructorArgNum++, engine);
}
cav.addIndexedArgumentValue(constructorArgNum++, value);
if (element.hasAttribute(SCRIPT_INTERFACES_ATTRIBUTE)) {
cav.addIndexedArgumentValue(constructorArgNum++, element.getAttribute(SCRIPT_INTERFACES_ATTRIBUTE), "java.lang.Class[]");
}
// This is used for Groovy. It's a bean reference to a customizer bean.
if (element.hasAttribute(CUSTOMIZER_REF_ATTRIBUTE)) {
String customizerBeanName = element.getAttribute(CUSTOMIZER_REF_ATTRIBUTE);
if (!StringUtils.hasText(customizerBeanName)) {
parserContext.getReaderContext().error("Attribute 'customizer-ref' has empty value", element);
} else {
cav.addIndexedArgumentValue(constructorArgNum++, new RuntimeBeanReference(customizerBeanName));
}
}
// Add any property definitions that need adding.
parserContext.getDelegate().parsePropertyElements(element, bd);
return bd;
}
use of org.springframework.beans.factory.config.ConstructorArgumentValues in project spring-boot by spring-projects.
the class EntityScanPackages method register.
/**
* Register the specified entity scan packages with the system.
* @param registry the source registry
* @param packageNames the package names to register
*/
public static void register(BeanDefinitionRegistry registry, Collection<String> packageNames) {
Assert.notNull(registry, "Registry must not be null");
Assert.notNull(packageNames, "PackageNames must not be null");
if (registry.containsBeanDefinition(BEAN)) {
BeanDefinition beanDefinition = registry.getBeanDefinition(BEAN);
ConstructorArgumentValues constructorArguments = beanDefinition.getConstructorArgumentValues();
constructorArguments.addIndexedArgumentValue(0, addPackageNames(constructorArguments, packageNames));
} else {
GenericBeanDefinition beanDefinition = new GenericBeanDefinition();
beanDefinition.setBeanClass(EntityScanPackages.class);
beanDefinition.getConstructorArgumentValues().addIndexedArgumentValue(0, packageNames.toArray(new String[packageNames.size()]));
beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
registry.registerBeanDefinition(BEAN, beanDefinition);
}
}
use of org.springframework.beans.factory.config.ConstructorArgumentValues in project spring-framework by spring-projects.
the class QualifierAnnotationAutowireBeanFactoryTests method testAutowireCandidateWithFieldDescriptor.
@Ignore
@Test
public void testAutowireCandidateWithFieldDescriptor() throws Exception {
DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
ConstructorArgumentValues cavs1 = new ConstructorArgumentValues();
cavs1.addGenericArgumentValue(JUERGEN);
RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null);
person1.addQualifier(new AutowireCandidateQualifier(TestQualifier.class));
lbf.registerBeanDefinition(JUERGEN, person1);
ConstructorArgumentValues cavs2 = new ConstructorArgumentValues();
cavs2.addGenericArgumentValue(MARK);
RootBeanDefinition person2 = new RootBeanDefinition(Person.class, cavs2, null);
lbf.registerBeanDefinition(MARK, person2);
DependencyDescriptor qualifiedDescriptor = new DependencyDescriptor(QualifiedTestBean.class.getDeclaredField("qualified"), false);
DependencyDescriptor nonqualifiedDescriptor = new DependencyDescriptor(QualifiedTestBean.class.getDeclaredField("nonqualified"), false);
assertTrue(lbf.isAutowireCandidate(JUERGEN, null));
assertTrue(lbf.isAutowireCandidate(JUERGEN, nonqualifiedDescriptor));
assertTrue(lbf.isAutowireCandidate(JUERGEN, qualifiedDescriptor));
assertTrue(lbf.isAutowireCandidate(MARK, null));
assertTrue(lbf.isAutowireCandidate(MARK, nonqualifiedDescriptor));
assertFalse(lbf.isAutowireCandidate(MARK, qualifiedDescriptor));
}
Aggregations