1 24 package org.riotfamily.common.beans.override; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.riotfamily.common.beans.override.OverrideNamespaceHandler.BeanReplacement; 29 import org.springframework.beans.BeansException; 30 import org.springframework.beans.MutablePropertyValues; 31 import org.springframework.beans.factory.config.BeanDefinition; 32 import org.springframework.beans.factory.config.BeanFactoryPostProcessor; 33 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 34 import org.springframework.beans.factory.config.ConstructorArgumentValues; 35 import org.springframework.core.Ordered; 36 37 47 public class BeanOverrideProcessor implements BeanFactoryPostProcessor, Ordered { 48 49 private static Log log = LogFactory.getLog(BeanOverrideProcessor.class); 50 51 private String ref; 52 53 private BeanDefinition beanDefinition; 54 55 private boolean merge; 56 57 private int order = Ordered.LOWEST_PRECEDENCE; 58 59 public void setRef(String ref) { 60 this.ref = ref; 61 } 62 63 public void setBeanDefinition(BeanDefinition beanDefinition) { 64 this.beanDefinition = beanDefinition; 65 } 66 67 public void setBeanReplacement(BeanReplacement replacement) { 68 setBeanDefinition(replacement.getBeanDefinition()); 69 } 70 71 public void setMerge(boolean merge) { 72 this.merge = merge; 73 } 74 75 public int getOrder() { 76 return this.order; 77 } 78 79 public void setOrder(int order) { 80 this.order = order; 81 } 82 83 public void postProcessBeanFactory( 84 ConfigurableListableBeanFactory beanFactory) 85 throws BeansException { 86 87 BeanDefinition target = beanFactory.getBeanDefinition(ref); 88 overwriteBeanDefinition(target, beanDefinition); 89 } 90 91 private void overwriteBeanDefinition(BeanDefinition target, BeanDefinition source) { 92 log.info("Replacing bean [" + ref + "] with a [" 93 + source.getBeanClassName() + "]"); 94 95 target.setBeanClassName(source.getBeanClassName()); 96 ConstructorArgumentValues cas = target.getConstructorArgumentValues(); 97 cas.clear(); 98 cas.addArgumentValues(source.getConstructorArgumentValues()); 99 100 MutablePropertyValues pvs = target.getPropertyValues(); 101 if (!merge) { 102 pvs.clear(); 103 } 104 pvs.addPropertyValues(source.getPropertyValues()); 105 } 106 107 } 108 | Popular Tags |