@Test public void testQualifiedByAttributesWithCustomQualifierRegistered() { StaticApplicationContext context = new StaticApplicationContext(); BeanDefinitionReader reader = new XmlBeanDefinitionReader(context); reader.loadBeanDefinitions(CONFIG_LOCATION); QualifierAnnotationAutowireCandidateResolver resolver = (QualifierAnnotationAutowireCandidateResolver) context.getDefaultListableBeanFactory().getAutowireCandidateResolver(); resolver.addQualifierType(MultipleAttributeQualifier.class); context.registerSingleton("testBean", MultiQualifierClient.class); context.refresh(); MultiQualifierClient testBean = (MultiQualifierClient) context.getBean("testBean"); assertNotNull( testBean.factoryTheta); assertNotNull( testBean.implTheta); }
@Before public void setUp() { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); QualifierAnnotationAutowireCandidateResolver acr = new QualifierAnnotationAutowireCandidateResolver(); acr.setBeanFactory(bf); bf.setAutowireCandidateResolver(acr); this.beanFactory = bf; }
/** * Creates a <code>DefaultListableBeanFactory</code> with some default * settings, i.e. * <ul> * <li>support of auto-wiring annotation</li> * <li>disabled bean-overriding</li> * </ul> * * @param enableAutoWiring * <code>true</code> if auto-wiring for the factory should be * enabled, otherwise <code>false</code> * @param allowBeanOverriding * <code>true</code> if a bean can override another bean with the * same id, otherwise <code>false</code> * * @return a <code>DefaultListableBeanFactory</code> with some default * settings * * @see AutowiredAnnotationBeanPostProcessor * @see DefaultListableBeanFactory#setAllowBeanDefinitionOverriding */ public static DefaultListableBeanFactory createBeanFactory( final boolean enableAutoWiring, final boolean allowBeanOverriding) { // create the factory final DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.setAllowBeanDefinitionOverriding(allowBeanOverriding); // enable auto-wiring if (enableAutoWiring) { // get the resolver used for autowiring, we want the qualifier to be // used // when resolving final AutowireCandidateResolver resolver = new QualifierAnnotationAutowireCandidateResolver(); factory.setAutowireCandidateResolver(resolver); // now create the post processor and set the factory and the // resolver final AutowiredAnnotationBeanPostProcessor autowiredPostProcessor = new AutowiredAnnotationBeanPostProcessor(); autowiredPostProcessor.setBeanFactory(factory); factory.addBeanPostProcessor(autowiredPostProcessor); } factory.addPropertyEditorRegistrar(new ConfiguratorPropertyEditorRegistrar()); return factory; }
/** * Customize the internal bean factory used by this context. * Called for each {@link #refresh()} attempt. * <p>The default implementation applies this context's * {@linkplain #setAllowBeanDefinitionOverriding "allowBeanDefinitionOverriding"} * and {@linkplain #setAllowCircularReferences "allowCircularReferences"} settings, * if specified. Can be overridden in subclasses to customize any of * {@link DefaultListableBeanFactory}'s settings. * @param beanFactory the newly created bean factory for this context * @see DefaultListableBeanFactory#setAllowBeanDefinitionOverriding * @see DefaultListableBeanFactory#setAllowCircularReferences * @see DefaultListableBeanFactory#setAllowRawInjectionDespiteWrapping * @see DefaultListableBeanFactory#setAllowEagerClassLoading */ protected void customizeBeanFactory(DefaultListableBeanFactory beanFactory) { if (this.allowBeanDefinitionOverriding != null) { beanFactory.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding); } if (this.allowCircularReferences != null) { beanFactory.setAllowCircularReferences(this.allowCircularReferences); } beanFactory.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); }
/** * Create a new GenericApplicationContext. * @see #registerBeanDefinition * @see #refresh */ public GenericApplicationContext() { this.beanFactory = new DefaultListableBeanFactory(); this.beanFactory.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); }