How to create hierarchical context
Hi to everybody,
I am new to Spring. Right now I am trying to determine the best way to use it.
What I want to achive is the following:
1. Every class I have will have a bean factory (ApplicationContext or BeanFactory, doesn't matter, let's keep the discussion abstract). I am not saying that I will create a new factory for each class – I may have one factory for all, layers or indeed a factory for each class.
When each class tries to get its own factory I need to determine if I should create a new factory or use a parent one. The parent may contain the beans that are used by most of the classes (for example logger definition, some common text resources, like YES/NO button text) and each class may have some special bean which may be needed only by such class or by its children.
I see that ApplicationContext and BeanFactory have definition for parent, but in order to use the parent I have to know the parent. I need to supply it in the configuration somehow. And somehow I need to know if I have the configuration for the given class. If I have no configuration file the parent must be used (which in turn also may not have the configuration), if I do have a file it must be used.
The second problem with the ApplicationContext and BeanFactory that if I possibly have one of those for each class I have they will use too much resource. Will they??
2. When I create a bean from bean factory I will need to provide certain dynamic information (example getLogger(<class name>) factory method has to receive a name of the class that calls it, I can’t configure it for each class – I will need to create a bean definition for each class this way). I need something like getBean(id, Object[] <constructor pareameters>)
Thank you for your thoughts,
I hope that I didn't ask stupid questions :)
Anyway I will need to know about Spring somewhere
In any case, somewhere I will need to write something like:
appCtx.getBean("myBean")
I can then pass the result to some class that will not know about Spring. But it looks like selfdeception - my class will be dependant on another class that provides it all of its parameters. It's not coupled, but still...
Another thing is that I may need a lot of parameters to be passed to my class this way - my class will no be incapsulated well - why does a caller of the class needs to know that I use a logger?
Re: Anyway I will need to know about Spring somewhere
Quote:
Originally Posted by Lock51
In any case, somewhere I will need to write something like:
appCtx.getBean("myBean")
I don`t think you understood my example. In most Spring applications I write, there is not a single appCtx.getBean(...). (Only our Maverick controllers use such a reference) The beans are configured by Spring but they don`t know about Spring.