2014-02-18 114 views
0

我想从一个弹簧豆删除属性, 该属性可以是对象或Java原始上下的类型(字符串,整数,地图..)如何从弹簧豆删除属性

<bean id="test" class="me.test.xxxx"> 
     <property name="user" ref="me.test.user"/> 
     <property name="other" ref="me.test.other"/> 
</bean> 

创建实现BeanFactoryPostProcessor并覆盖PostProcessBeanFactory

我会删除所有的bean我不想在这个方法一个新的类,但我不知道如何从一个bean属性中删除一个对象的属性。

谢谢。

+0

删除bean和删除对象属性似乎不够清楚,您可以在不想使用注入属性的方法中重新定义对象属性 – bjhaid

+0

“删除”bean属性是什么意思?将其值设置为'null' /默认值或实际从bean中删除属性(字节码操作?) –

+0

看起来像[XY问题](http://meta.stackexchange.com/questions/66377/what-is-在-XY-问题)。请澄清你在做什么的目的。 –

回答

1

目前尚不清楚你想要达到什么样的,但如果你想删除,例如,

<property name="other" ref="me.test.other"/> 

的bean初始化之前,在你的上下文中有以下实现声明BeanFactoryPostProcessor

@Override 
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { 
    BeanDefinition beanDefinition = beanFactory.getBeanDefinition("test"); // get the bean definition in some way 
    if (beanDefinition != null) { 
     beanDefinition.getPropertyValues().removePropertyValue("other"); // remove the property value based on some criterion 
    } 
}