Java Code Examples for org.springframework.beans.factory.support.LookupOverride

Following code examples demonstrate how to use org.springframework.beans.factory.support.LookupOverridefrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.beans.factory.support.LookupOverrideand various code implementation of this class.

                                if (targetBeanNames.length > 0) {
                                    injectedBeanName = targetBeanNames[0];
                                    if (!"".equals(qualifier) && targetBeanNames.length > 1) {
                                        LOOP: for (int i = 0; i < targetBeanNames.length; i++) {
                                            try {
                                                BeanDefinition targetBeanDefinitoin = beanFactory.getBeanDefinition(targetBeanNames[i]);
                                                Class<?> targetBeanClass = Class.forName(targetBeanDefinitoin.getBeanClassName());
                                                if (targetBeanClass.isAnnotationPresent(Qualifier.class)) {
                                                    String currentQualifier = targetBeanClass.getAnnotation(Qualifier.class).value();
                                                    if (currentQualifier.equals(qualifier)) {
                                                        injectedBeanName = targetBeanNames[i];
                                                        break LOOP;
                                                    }
                                                }
                                            } catch (ClassNotFoundException e) {
                                                LOGGER.warn(e.getMessage(), e);
                                            }
                                        }
                                    }
                                } 


        if (override instanceof LookupOverride) {
            LookupOverride lookupOverride = (LookupOverride) override;
            builder.addChild(LOOKUP_METHOD_ELEMENT);
            String methodName = lookupOverride.getMethodName();
            builder.addAttribute(NAME_ATTRIBUTE, methodName);
            String targetBeanName = lookupOverride.getBeanName();
            builder.addAttribute(BEAN_REF_ATTRIBUTE, targetBeanName);
            builder.up();
        } 

	private BeanDefinition createProvider (Class<? extends Object> type){
		try {
			
		GenericBeanDefinition bd = new GenericBeanDefinition();
		bd.setBeanClass(Provider.class);
		LookupOverride over = new LookupOverride("getObject", type.getSimpleName());
		MethodOverrides overrides = new MethodOverrides();
		overrides.addOverride(over);
		bd.setMethodOverrides(overrides);
		}catch(Exception e) {
			throw new RuntimeException(e);
		}
		return null;
	} 

        public void instantiate(String[] args) {
            Constructor<CommandManager> ctor = null;
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(CommandManager.class);
            enhancer.setCallbackFilter(new CallbackFilterImpl());
            enhancer.setCallbacks(new Callback[] { NoOp.INSTANCE, new LookupOverrideMethodInterceptor(),
                    new ReplaceOverrideMethodInterceptor() });

            Object cm = (ctor == null) ? enhancer.create() : enhancer.create(ctor.getParameterTypes(), args);
        } 

            public int accept(Method method) {
                MethodOverride methodOverride = beanDefinition.getMethodOverrides().getOverride(method);
                if (logger.isTraceEnabled()) {
                    logger.trace("Override for '" + method.getName() + "' is [" + methodOverride + "]");
                }
                if (methodOverride == null) {
                    return PASSTHROUGH;
                } else if (methodOverride instanceof LookupOverride) {
                    return LOOKUP_OVERRIDE;
                } else if (methodOverride instanceof ReplaceOverride) {
                    return METHOD_REPLACER;
                }
                throw new UnsupportedOperationException("Unexpected MethodOverride subclass: " + methodOverride.getClass().getName());
            } 

            public Object intercept(Object obj, Method method, Object[] args, MethodProxy mp) throws Throwable {
                LookupOverride lo = (LookupOverride) beanDefinition.getMethodOverrides().getOverride(method);
                return owner.getBean(lo.getBeanName());
            } 

	public String getLabel() {
		Object[] args = new Object[] { getPattern(),
				getScope().getDescription() };
		return MessageUtils.format(
				BeansSearchMessages.SearchQuery_searchFor_reference, args);
	} 

	private void initBean() {
		synchronized(this) {

			constructorArguments = new LinkedHashSet<IBeanConstructorArgument>();
			ConstructorArgumentValues cargValues = definition.getConstructorArgumentValues();
			for (Object cargValue : cargValues.getGenericArgumentValues()) {
				IBeanConstructorArgument carg = new BeanConstructorArgument(this, (ValueHolder) cargValue);
				constructorArguments.add(carg);
			}
			Map<?, ?> indexedCargValues = cargValues.getIndexedArgumentValues();
			for (Object key : indexedCargValues.keySet()) {
				ValueHolder vHolder = (ValueHolder) indexedCargValues.get(key);
				IBeanConstructorArgument carg = new BeanConstructorArgument(this, ((Integer) key).intValue(), vHolder);
				constructorArguments.add(carg);
			}
	
			properties = new LinkedHashMap<String, IBeanProperty>();
			for (PropertyValue propValue : definition.getPropertyValues().getPropertyValues()) {
				IBeanProperty property = new BeanProperty(this, propValue);
				properties.put(property.getElementName(), property);
			}
	
			if (definition instanceof AbstractBeanDefinition) {
				methodOverrides = new LinkedHashSet<IBeanMethodOverride>();
				MethodOverrides mos = ((AbstractBeanDefinition) definition).getMethodOverrides();
				if (mos != null) {
					for (Object mo : mos.getOverrides()) {
						if (mo instanceof LookupOverride) {
							methodOverrides.add(new BeanLookupMethodOverride(this, (LookupOverride) mo));
						}
						else if (mo instanceof ReplaceOverride) {
							methodOverrides.add(new BeanReplaceMethodOverride(this, (ReplaceOverride) mo));
						}
					}
				}
			}
		}
	} 

	public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
		InjectionMetadata metadata = findAutowiringMetadata(beanName, beanType, null);
		metadata.checkConfigMembers(beanDefinition);
	} 

            if (override instanceof LookupOverride) {
                Element lookupMethodElement = beanElement.newSubElement("lookup-method");
                String bean = ((LookupOverride) override).getBeanName();
                String name = override.getMethodName();

                if (bean != null) {
                    lookupMethodElement.addAttribute("bean", new RefValue(bean));
                }

                if (name != null) {
                    lookupMethodElement.addAttribute("name", name);
                }

                continue;
            } 

Advertisement
Javadoc
Represents an override of a method that looks up an object in the same IoC context.

Metho

ds eligible for lookup override must not have arguments. @author Rod Johnson @author Juergen Hoeller @since 1.1

Read More
Advertisement