0

I have a project that includes the spring xml configs from the parent project.

I don't need a particular bean defined in the xml files in the parent project , but I don't want to modify the parent project. I also don't want to rewrite a whole set of xml files.

HOw do I only exclude a particular bean in the child project?

Let me be more specific. The parent project has a bean (for example)

<bean id = "example" class = "blah />

In the child project, I want to be able to do something similar to this in my xml. Child project simply doesn't need this bean.

<exclude beanid="example" />
CuriousMind
  • 15,168
  • 20
  • 82
  • 120
  • Just curious, what would you achieve by excluding a bean in this way? Can you give an example bean that will help understand the reason behind its exclusion? Thanks! – Kilokahn Jun 11 '14 at 05:02
  • @Kilokahn I simply don't need it and that bean requires a lot of files to initialize. I don't need to provide it. The parent project shoudl have provided this flexibility, but they somehow made this bean kind of mandatory, unless I override all of their spring xml files. – CuriousMind Jun 11 '14 at 13:10
  • Ok, AFAIK, there is no way without additional child code that the bean may be excluded. – Kilokahn Jun 11 '14 at 15:21

1 Answers1

0

By exclude a bean in parent, you want it to not be initialized in the child context or you need it to be overwritten in the child context with something else?

If it is the former, you can implement a org.springframework.beans.factory.config.BeanFactoryPostProcessor, look up the bean definition and set lazy-init to true on that one.

If it is the latter, you can implement a org.springframework.beans.factory.config.BeanPostProcessor to mute the effect of the parent bean by proxying it to a no-op class. Another (non-deterministic) way of achieving this might be to name the child bean exactly the same as the parent one and then check the logs to confirm that the child bean definition has overwritten the parent one - however, this is prone to error when you move around context imports. Deterministic ordering of beans in spring is tracked in SPR-3948

Kilokahn
  • 2,281
  • 1
  • 25
  • 50
  • That doesn't work as both BeanPostProcessor and BeanFactoryPostProcessor are container-scoped (only work in the container they are defined). If he bean he wants is in the parent container, the BPP and BFPP wouldn't pick them up – Gabriel Ruiu Jun 11 '14 at 03:27
  • Is there a way to do this in a child project xml level, without writing more code? – CuriousMind Jun 11 '14 at 03:29
  • I think the op is not asking about different container contexts (maybe I am wrong) but purely about two (maven) modules where the child uses the parent as a library? @CodeNoob can confirm. – Kilokahn Jun 11 '14 at 03:30
  • @CodeNoob Can you confirm if it is #1 or #2 you are looking for? – Kilokahn Jun 11 '14 at 03:32