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

Following code examples demonstrate how to use org.springframework.beans.factory.support.MethodOverridesfrom 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.MethodOverridesand various code implementation of this class.

	public static void printBeanDefinition(BeanDefinition bdd){
		if(bdd instanceof GenericBeanDefinition){
			GenericBeanDefinition bd=(GenericBeanDefinition)bdd;
			String className=bd.getBeanClassName();
			boolean isAbstract=bd.isAbstract();
			boolean isAutowireCandidate=bd.isAutowireCandidate();
			boolean isEnforceDestroyMethod=bd.isEnforceDestroyMethod();
			boolean isEnforceInitMethod=bd.isEnforceInitMethod();
			boolean isLazyInit=bd.isLazyInit();
			boolean isLenientConstructorResolution=bd.isLenientConstructorResolution();
			boolean isNonPublicAccessAllowed=bd.isNonPublicAccessAllowed();
			boolean isPrimary=bd.isPrimary();
			boolean isPrototype=bd.isPrototype();
			boolean isSingleton=bd.isSingleton();
			boolean isSynthetic=bd.isSynthetic();
			f(", isAutowireCandidate="+isAutowireCandidate
					+", isEnforceDestroyMethod="+isEnforceDestroyMethod+", isEnforceInitMethod="+isEnforceInitMethod
					+", isSynthetic="+isSynthetic+",\n isLenientConstructorResolution="+isLenientConstructorResolution
					+", isNonPublicAccessAllowed="+isNonPublicAccessAllowed+", isPrimary="+isPrimary
					+", isLazyInit="+isLazyInit);
			f("是否是抽象bean:"+isAbstract);
			f("是否是单例bean:"+isSingleton);
			f("是否是原型bean:"+isPrototype);
			int autowireMode=bd.getAutowireMode();
			f("bean注入模式:"+autowireMode);
			
			int dependencyCheck=bd.getDependencyCheck();
			f("依赖检查模式:"+dependencyCheck);
			
			String[] dependsOn=bd.getDependsOn();
			f("依赖对象名称:"+StylerUtils.style(dependsOn));
			
			String description=bd.getDescription();
			f("bean的描述:"+description);
			
			String  factoryBeanName= bd.getFactoryBeanName();
			f("bean对应的工厂方法beanName:"+factoryBeanName);
			String factoryMethodName=bd.getFactoryMethodName();
			f("bean对应的工厂方法名:"+factoryMethodName);
			String parentName=bd.getParentName();
			f("引用父bean的名称,不是继承:"+parentName);
			int role=bd.getRole();
			f("bean的作用:"+role);
			String scope=bd.getScope();
			f("bean的范围:"+scope);
			String destroyMethodName=bd.getDestroyMethodName();
			f("bean的销毁方法名称:"+destroyMethodName);
			String initMethodName=bd.getInitMethodName();
			f("bean的初始化方法名称:"+initMethodName);
			Set<AutowireCandidateQualifier> qualifiers =bd.getQualifiers();
			f("限定父对象:"+StylerUtils.style(qualifiers));
			MethodOverrides methodOverrides =bd.getMethodOverrides();
			Set<MethodOverride> setMethodOverrides=methodOverrides.getOverrides();
			
			for(MethodOverride mo:setMethodOverrides){
				System.out.print(mo.getMethodName()+" ,");
			}
			f("重写的方法");
			ConstructorArgumentValues cavs=bd.getConstructorArgumentValues();
			f("构造参数:"+StylerUtils.style(cavs));
			MutablePropertyValues mpv=bd.getPropertyValues();
			f("属性:"+StylerUtils.style(mpv));
			BeanDefinition beanOriginating=bd.getOriginatingBeanDefinition();
			f("原始bean:"+beanOriginating);
			Resource resource=bd.getResource();
			f("资源对象:"+resource);
			String resourceDescription=bd.getResourceDescription();
			f("资源对象描述:"+resourceDescription);
			
		}
	} 


    public void skipOverridedMethod() throws NoSuchMethodException, ClassNotFoundException {
        final AbstractBeanDefinition parentBeanDefinition = EasyMock.createMock(AbstractBeanDefinition.class);

        beanFactory.getBeanDefinitionNames();
        EasyMock.expectLastCall().andReturn(new String[] { "mockClass", "mockinterface" });
        beanFactory.getBeanDefinition("mockClass");
        EasyMock.expectLastCall().andReturn(beanDefinitionA);
        beanDefinitionA.isAbstract();
        EasyMock.expectLastCall().andReturn(true);

        beanFactory.getBeanDefinition("mockinterface");
        EasyMock.expectLastCall().andReturn(beanDefinitionB);
        beanDefinitionB.isAbstract();
        EasyMock.expectLastCall().andReturn(false);
        beanDefinitionB.resolveBeanClass(EasyMock.isA(ClassLoader.class));
        EasyMock.expectLastCall().andReturn(null);

        beanDefinitionB.getParentName();
        EasyMock.expectLastCall().andReturn("parent");
        beanFactory.getBeanDefinition("parent");
        EasyMock.expectLastCall().andReturn(parentBeanDefinition);
        parentBeanDefinition.resolveBeanClass(EasyMock.isA(ClassLoader.class));
        EasyMock.expectLastCall().andReturn(MockInterface.class);
        beanFactory.containsBean("__UNSUPPORTED_METHOD_REPLACER");
        EasyMock.expectLastCall().andReturn(false);
        beanFactory.registerSingleton(EasyMock.eq("__UNSUPPORTED_METHOD_REPLACER"),
                EasyMock.isA(UnsupportedMethodReplacer.class));
        beanDefinitionB.getMethodOverrides();
        EasyMock.expectLastCall().andReturn(methodOverrides);
        methodOverrides.getOverride(MockInterface.class.getMethod("abstractMethod", new Class<?>[0]));
        EasyMock.expectLastCall().andReturn(EasyMock.createMock(MethodOverride.class));
        EasyMock.replay(beanFactory, beanDefinitionA, beanDefinitionB, parentBeanDefinition, methodOverrides);

        abstractMethodOverrideProcessor.postProcessBeanFactory(beanFactory);
        EasyMock.verify(beanFactory, beanDefinitionA, beanDefinitionB, parentBeanDefinition, methodOverrides);
    } 

    public void visit() throws NoSuchMethodException, ClassNotFoundException {
        beanFactory.getBeanDefinitionNames();
        EasyMock.expectLastCall().andReturn(new String[] { "mockClass", "mockinterface" });
        beanFactory.getBeanDefinition("mockClass");
        EasyMock.expectLastCall().andReturn(beanDefinitionA);
        beanDefinitionA.isAbstract();
        EasyMock.expectLastCall().andReturn(false);
        beanDefinitionA.resolveBeanClass(EasyMock.isA(ClassLoader.class));
        EasyMock.expectLastCall().andReturn(MockClass.class);
        beanFactory.getBeanDefinition("mockinterface");
        EasyMock.expectLastCall().andReturn(beanDefinitionB);
        beanDefinitionB.isAbstract();
        EasyMock.expectLastCall().andReturn(false);
        beanDefinitionB.resolveBeanClass(EasyMock.isA(ClassLoader.class));
        EasyMock.expectLastCall().andReturn(null);
        beanDefinitionB.getParentName();
        EasyMock.expectLastCall().andReturn(null);
        beanFactory.containsBean("__UNSUPPORTED_METHOD_REPLACER");
        EasyMock.expectLastCall().andReturn(true);
        beanDefinitionA.getMethodOverrides();
        EasyMock.expectLastCall().andReturn(methodOverrides);
        methodOverrides.getOverride(MockClass.class.getMethod("abstractMethod", new Class<?>[0]));
        EasyMock.expectLastCall().andReturn(null);
        methodOverrides.addOverride(EasyMock.isA(ReplaceOverride.class));
        EasyMock.replay(beanFactory, beanDefinitionA, beanDefinitionB, methodOverrides);

        abstractMethodOverrideProcessor.postProcessBeanFactory(beanFactory);
        EasyMock.verify(beanFactory, beanDefinitionA, beanDefinitionB, methodOverrides);
    } 

    public void setUp() {
        beanDefinitionA = EasyMock.createMock(AbstractBeanDefinition.class);
        beanDefinitionB = EasyMock.createMock(AbstractBeanDefinition.class);
        methodOverrides = EasyMock.createMock(MethodOverrides.class);
        beanFactory = EasyMock.createMock(ConfigurableListableBeanFactory.class);
        abstractMethodOverrideProcessor = new AbstractMethodOverrideProcessor();
    } 

	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;
	} 

    private void addMethodOverrides(final AbstractBeanDefinition beanDefinition, final Class<?> clazz) {
        final MethodOverrides methodOverrides = beanDefinition.getMethodOverrides();
        for (final Method method : clazz.getMethods()) {
            if (Modifier.isAbstract(method.getModifiers()) && methodOverrides.getOverride(method) == null) {
                LOGGER.info("Add override for {}.", method);
                methodOverrides.addOverride(new ReplaceOverride(method.getName(), UNSUPPORTED_METHOD_REPLACER_NAME));
            }
        }
    } 

	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 Set<IBeanMethodOverride> getMethodOverrides() {
		if (methodOverrides == null) {
			initBean();
		}
		return methodOverrides;
	} 

Advertisement
Javadoc
Set of method overrides, determining which, if any, methods on a managed object the Spring IoC conta

iner will override at runtime.

The currently supported MethodOverride variants are LookupOverride and ReplaceOverride. @author Rod Johnson @author Juergen Hoeller @since 1.1 @see MethodOverride

Read More
Advertisement