You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by MG <mi...@gmail.com> on 2013/03/05 11:51:58 UTC

Loading different ApplicationContexts(Spring) for each WicketServlet

Excuse me, if anyone is reading this for the second time, because I already
tried with this question on stackoverflow (with no luck).

I'm developing a web application with Spring and Wicket.

I would like to achieve, that different paths get different
applicationContexts loaded. *How can I do that?*

For example:

   - http://myhost/ this in my entry page with some static data.
   - http://myhost/A/(and all subfolders) gets loaded
   applicationContext-A.xml
   - http://myhost/B/(and all subfolders) gets loaded
   applicationContext-B.xml

Imagine that I have 2 databases which have the same structure, but hold
contextually different data. My application logic is the same for both. I
just have to initialize different "dataSource" and a couple of other beans
initialized the-A-way or the-B-way.

I managed to define two *WicketServlets (servletA and servletB)* in *web.xml
* and I passed as a parameter the path to *applicationContext-A.xml* and *
applicationContext-B.xml* respectively.

The problem with this solution is, that I have to load the context in *
WebApplication* "by hand" and then get the beans out of it with *getBean(..)
* methods, instead of just using *@SpringBean* to wire the beans
automatically.

Best regards

Re: Loading different ApplicationContexts(Spring) for each WicketServlet

Posted by Martin Grigorov <mg...@apache.org>.
On Tue, Mar 5, 2013 at 2:18 PM, MG <mi...@gmail.com> wrote:

> >
> > You can try with different interfaces for the different beans, or by
> using
> > @
> > Qualifier
> >
>
> I don't think I understand what do you mean here.
> Do you propose I should have beanA and beanB in my application logic? If
> so, this is just what I was trying to avoid, because the beautiful part of
> the application is that I can have an implementation which isn't aware of A
> and B scenario and doesn't have to be as long as the beans are initialized
> correctly. Please correct me if I didn't understand you correctly.
>
>

> > or you can even split the app in two - two different .war files that
> share
> > a common .jar. The wars just have different web.xml and
> > applicationContext.xml
> >
> >
> OK, this solution would probably work. It was one of the options I
> considered before posting here, but I didn't like the overhead of packaging
> and deploying multiple wars.
>
> Is there a third(fourth) option?
>

You can use a FactoryBean that creates a new instance of A or B depending
on the context. That its #getObject() will create (or as you call it
"initialize") a different bean depending on the request path or whatever.


>
>
> >
> > On Tue, Mar 5, 2013 at 12:51 PM, MG <mi...@gmail.com> wrote:
> >
> > > Excuse me, if anyone is reading this for the second time, because I
> > already
> > > tried with this question on stackoverflow (with no luck).
> > >
> > > I'm developing a web application with Spring and Wicket.
> > >
> > > I would like to achieve, that different paths get different
> > > applicationContexts loaded. *How can I do that?*
> > >
> > > For example:
> > >
> > >    - http://myhost/ this in my entry page with some static data.
> > >    - http://myhost/A/(and all subfolders) gets loaded
> > >    applicationContext-A.xml
> > >    - http://myhost/B/(and all subfolders) gets loaded
> > >    applicationContext-B.xml
> > >
> > > Imagine that I have 2 databases which have the same structure, but hold
> > > contextually different data. My application logic is the same for
> both. I
> > > just have to initialize different "dataSource" and a couple of other
> > beans
> > > initialized the-A-way or the-B-way.
> > >
> > > I managed to define two *WicketServlets (servletA and servletB)* in
> > > *web.xml
> > > * and I passed as a parameter the path to *applicationContext-A.xml*
> and
> > *
> > > applicationContext-B.xml* respectively.
> > >
> > > The problem with this solution is, that I have to load the context in *
> > > WebApplication* "by hand" and then get the beans out of it with
> > > *getBean(..)
> > > * methods, instead of just using *@SpringBean* to wire the beans
> > > automatically.
> > >
> > > Best regards
> > >
> >
> >
> >
> > --
> > Martin Grigorov
> > jWeekend
> > Training, Consulting, Development
> > http://jWeekend.com <http://jweekend.com/>
> >
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com <http://jweekend.com/>

Re: Loading different ApplicationContexts(Spring) for each WicketServlet

Posted by Nick Pratt <nb...@gmail.com>.
You could embed Jetty inside your app (rather than deploying a war to
tomcat) and run multiple copies taking a couple of command line params -
namely port number and config file location.

N
On Mar 5, 2013 7:19 AM, "MG" <mi...@gmail.com> wrote:

> >
> > You can try with different interfaces for the different beans, or by
> using
> > @
> > Qualifier
> >
>
> I don't think I understand what do you mean here.
> Do you propose I should have beanA and beanB in my application logic? If
> so, this is just what I was trying to avoid, because the beautiful part of
> the application is that I can have an implementation which isn't aware of A
> and B scenario and doesn't have to be as long as the beans are initialized
> correctly. Please correct me if I didn't understand you correctly.
>
>
> > or you can even split the app in two - two different .war files that
> share
> > a common .jar. The wars just have different web.xml and
> > applicationContext.xml
> >
> >
> OK, this solution would probably work. It was one of the options I
> considered before posting here, but I didn't like the overhead of packaging
> and deploying multiple wars.
>
> Is there a third(fourth) option?
>
>
> >
> > On Tue, Mar 5, 2013 at 12:51 PM, MG <mi...@gmail.com> wrote:
> >
> > > Excuse me, if anyone is reading this for the second time, because I
> > already
> > > tried with this question on stackoverflow (with no luck).
> > >
> > > I'm developing a web application with Spring and Wicket.
> > >
> > > I would like to achieve, that different paths get different
> > > applicationContexts loaded. *How can I do that?*
> > >
> > > For example:
> > >
> > >    - http://myhost/ this in my entry page with some static data.
> > >    - http://myhost/A/(and all subfolders) gets loaded
> > >    applicationContext-A.xml
> > >    - http://myhost/B/(and all subfolders) gets loaded
> > >    applicationContext-B.xml
> > >
> > > Imagine that I have 2 databases which have the same structure, but hold
> > > contextually different data. My application logic is the same for
> both. I
> > > just have to initialize different "dataSource" and a couple of other
> > beans
> > > initialized the-A-way or the-B-way.
> > >
> > > I managed to define two *WicketServlets (servletA and servletB)* in
> > > *web.xml
> > > * and I passed as a parameter the path to *applicationContext-A.xml*
> and
> > *
> > > applicationContext-B.xml* respectively.
> > >
> > > The problem with this solution is, that I have to load the context in *
> > > WebApplication* "by hand" and then get the beans out of it with
> > > *getBean(..)
> > > * methods, instead of just using *@SpringBean* to wire the beans
> > > automatically.
> > >
> > > Best regards
> > >
> >
> >
> >
> > --
> > Martin Grigorov
> > jWeekend
> > Training, Consulting, Development
> > http://jWeekend.com <http://jweekend.com/>
> >
>

Re: Loading different ApplicationContexts(Spring) for each WicketServlet

Posted by MG <mi...@gmail.com>.
> *Sven:*
>
SpringWebApplicationFactory already supports an additional application
> context to create the application from.
>
> All you have to do is to pass this additional context to your
> SpringComponentInjector - I'm not sure how but it should be possible.
>
> Then you can still use one war: two filters for two applications, each
> having their own context and both sharing a common web context.
>

Sven, this is EXACTLY what I needed! I was 1 line of code away from your
solution. I tested it and it works like a charm!
I guess I didn't read the documentation of SpringComponentInjector thoroughly
enough in the first place. Thanks again for pointing that out to me.

*Nick:*
> You could embed Jetty inside your app (rather than deploying a war to
> tomcat) and run multiple copies taking a couple of command line params -
> namely port number and config file location.


This I guess would also be rather acceptable solution, since I already use
jetty for test-cases and I could have a server configuration already done
the way I need it in the code.
Also I'll host this on my own server, so I can run the application any way
I want to.

*Martin* thanks for your effort too.

Re: Loading different ApplicationContexts(Spring) for each WicketServlet

Posted by Martin Grigorov <mg...@apache.org>.
Check http://blog.springsource.org/2011/08/09/whats-a-factorybean/ and
http://docs.oracle.com/javaee/6/api/javax/inject/Provider.html


On Tue, Mar 5, 2013 at 2:47 PM, Sven Meier <sv...@meiers.net> wrote:

> Hi,
>
> SpringWebApplicationFactory already supports an additional application
> context to create the application from.
>
> All you have to do is to pass this additional context to your
> SpringComponentInjector - I'm not sure how but it should be possible.
>
> Then you can still use one war: two filters for two applications, each
> having their own context and both sharing a common web context.
>
> Sven
>
>
> On 03/05/2013 01:18 PM, MG wrote:
>
>> You can try with different interfaces for the different beans, or by using
>>> @
>>> Qualifier
>>>
>>>  I don't think I understand what do you mean here.
>> Do you propose I should have beanA and beanB in my application logic? If
>> so, this is just what I was trying to avoid, because the beautiful part of
>> the application is that I can have an implementation which isn't aware of
>> A
>> and B scenario and doesn't have to be as long as the beans are initialized
>> correctly. Please correct me if I didn't understand you correctly.
>>
>>
>>  or you can even split the app in two - two different .war files that
>>> share
>>> a common .jar. The wars just have different web.xml and
>>> applicationContext.xml
>>>
>>>
>>>  OK, this solution would probably work. It was one of the options I
>> considered before posting here, but I didn't like the overhead of
>> packaging
>> and deploying multiple wars.
>>
>> Is there a third(fourth) option?
>>
>>
>>  On Tue, Mar 5, 2013 at 12:51 PM, MG <mi...@gmail.com> wrote:
>>>
>>>  Excuse me, if anyone is reading this for the second time, because I
>>>>
>>> already
>>>
>>>> tried with this question on stackoverflow (with no luck).
>>>>
>>>> I'm developing a web application with Spring and Wicket.
>>>>
>>>> I would like to achieve, that different paths get different
>>>> applicationContexts loaded. *How can I do that?*
>>>>
>>>> For example:
>>>>
>>>>     - http://myhost/ this in my entry page with some static data.
>>>>     - http://myhost/A/(and all subfolders) gets loaded
>>>>     applicationContext-A.xml
>>>>     - http://myhost/B/(and all subfolders) gets loaded
>>>>     applicationContext-B.xml
>>>>
>>>> Imagine that I have 2 databases which have the same structure, but hold
>>>> contextually different data. My application logic is the same for both.
>>>> I
>>>> just have to initialize different "dataSource" and a couple of other
>>>>
>>> beans
>>>
>>>> initialized the-A-way or the-B-way.
>>>>
>>>> I managed to define two *WicketServlets (servletA and servletB)* in
>>>> *web.xml
>>>> * and I passed as a parameter the path to *applicationContext-A.xml* and
>>>>
>>> *
>>>
>>>> applicationContext-B.xml* respectively.
>>>>
>>>> The problem with this solution is, that I have to load the context in *
>>>> WebApplication* "by hand" and then get the beans out of it with
>>>> *getBean(..)
>>>> * methods, instead of just using *@SpringBean* to wire the beans
>>>> automatically.
>>>>
>>>> Best regards
>>>>
>>>>
>>>
>>> --
>>> Martin Grigorov
>>> jWeekend
>>> Training, Consulting, Development
>>> http://jWeekend.com <http://jweekend.com/>
>>>
>>>
>
> ------------------------------**------------------------------**---------
> To unsubscribe, e-mail: users-unsubscribe@wicket.**apache.org<us...@wicket.apache.org>
> For additional commands, e-mail: users-help@wicket.apache.org
>
>


-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com <http://jweekend.com/>

Re: Loading different ApplicationContexts(Spring) for each WicketServlet

Posted by Sven Meier <sv...@meiers.net>.
Hi,

SpringWebApplicationFactory already supports an additional application 
context to create the application from.

All you have to do is to pass this additional context to your 
SpringComponentInjector - I'm not sure how but it should be possible.

Then you can still use one war: two filters for two applications, each 
having their own context and both sharing a common web context.

Sven

On 03/05/2013 01:18 PM, MG wrote:
>> You can try with different interfaces for the different beans, or by using
>> @
>> Qualifier
>>
> I don't think I understand what do you mean here.
> Do you propose I should have beanA and beanB in my application logic? If
> so, this is just what I was trying to avoid, because the beautiful part of
> the application is that I can have an implementation which isn't aware of A
> and B scenario and doesn't have to be as long as the beans are initialized
> correctly. Please correct me if I didn't understand you correctly.
>
>
>> or you can even split the app in two - two different .war files that share
>> a common .jar. The wars just have different web.xml and
>> applicationContext.xml
>>
>>
> OK, this solution would probably work. It was one of the options I
> considered before posting here, but I didn't like the overhead of packaging
> and deploying multiple wars.
>
> Is there a third(fourth) option?
>
>
>> On Tue, Mar 5, 2013 at 12:51 PM, MG <mi...@gmail.com> wrote:
>>
>>> Excuse me, if anyone is reading this for the second time, because I
>> already
>>> tried with this question on stackoverflow (with no luck).
>>>
>>> I'm developing a web application with Spring and Wicket.
>>>
>>> I would like to achieve, that different paths get different
>>> applicationContexts loaded. *How can I do that?*
>>>
>>> For example:
>>>
>>>     - http://myhost/ this in my entry page with some static data.
>>>     - http://myhost/A/(and all subfolders) gets loaded
>>>     applicationContext-A.xml
>>>     - http://myhost/B/(and all subfolders) gets loaded
>>>     applicationContext-B.xml
>>>
>>> Imagine that I have 2 databases which have the same structure, but hold
>>> contextually different data. My application logic is the same for both. I
>>> just have to initialize different "dataSource" and a couple of other
>> beans
>>> initialized the-A-way or the-B-way.
>>>
>>> I managed to define two *WicketServlets (servletA and servletB)* in
>>> *web.xml
>>> * and I passed as a parameter the path to *applicationContext-A.xml* and
>> *
>>> applicationContext-B.xml* respectively.
>>>
>>> The problem with this solution is, that I have to load the context in *
>>> WebApplication* "by hand" and then get the beans out of it with
>>> *getBean(..)
>>> * methods, instead of just using *@SpringBean* to wire the beans
>>> automatically.
>>>
>>> Best regards
>>>
>>
>>
>> --
>> Martin Grigorov
>> jWeekend
>> Training, Consulting, Development
>> http://jWeekend.com <http://jweekend.com/>
>>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Loading different ApplicationContexts(Spring) for each WicketServlet

Posted by MG <mi...@gmail.com>.
>
> You can try with different interfaces for the different beans, or by using
> @
> Qualifier
>

I don't think I understand what do you mean here.
Do you propose I should have beanA and beanB in my application logic? If
so, this is just what I was trying to avoid, because the beautiful part of
the application is that I can have an implementation which isn't aware of A
and B scenario and doesn't have to be as long as the beans are initialized
correctly. Please correct me if I didn't understand you correctly.


> or you can even split the app in two - two different .war files that share
> a common .jar. The wars just have different web.xml and
> applicationContext.xml
>
>
OK, this solution would probably work. It was one of the options I
considered before posting here, but I didn't like the overhead of packaging
and deploying multiple wars.

Is there a third(fourth) option?


>
> On Tue, Mar 5, 2013 at 12:51 PM, MG <mi...@gmail.com> wrote:
>
> > Excuse me, if anyone is reading this for the second time, because I
> already
> > tried with this question on stackoverflow (with no luck).
> >
> > I'm developing a web application with Spring and Wicket.
> >
> > I would like to achieve, that different paths get different
> > applicationContexts loaded. *How can I do that?*
> >
> > For example:
> >
> >    - http://myhost/ this in my entry page with some static data.
> >    - http://myhost/A/(and all subfolders) gets loaded
> >    applicationContext-A.xml
> >    - http://myhost/B/(and all subfolders) gets loaded
> >    applicationContext-B.xml
> >
> > Imagine that I have 2 databases which have the same structure, but hold
> > contextually different data. My application logic is the same for both. I
> > just have to initialize different "dataSource" and a couple of other
> beans
> > initialized the-A-way or the-B-way.
> >
> > I managed to define two *WicketServlets (servletA and servletB)* in
> > *web.xml
> > * and I passed as a parameter the path to *applicationContext-A.xml* and
> *
> > applicationContext-B.xml* respectively.
> >
> > The problem with this solution is, that I have to load the context in *
> > WebApplication* "by hand" and then get the beans out of it with
> > *getBean(..)
> > * methods, instead of just using *@SpringBean* to wire the beans
> > automatically.
> >
> > Best regards
> >
>
>
>
> --
> Martin Grigorov
> jWeekend
> Training, Consulting, Development
> http://jWeekend.com <http://jweekend.com/>
>

Re: Loading different ApplicationContexts(Spring) for each WicketServlet

Posted by Martin Grigorov <mg...@apache.org>.
Hi,

You can try with different interfaces for the different beans, or by using @
Qualifier
or you can even split the app in two - two different .war files that share
a common .jar. The wars just have different web.xml and
applicationContext.xml


On Tue, Mar 5, 2013 at 12:51 PM, MG <mi...@gmail.com> wrote:

> Excuse me, if anyone is reading this for the second time, because I already
> tried with this question on stackoverflow (with no luck).
>
> I'm developing a web application with Spring and Wicket.
>
> I would like to achieve, that different paths get different
> applicationContexts loaded. *How can I do that?*
>
> For example:
>
>    - http://myhost/ this in my entry page with some static data.
>    - http://myhost/A/(and all subfolders) gets loaded
>    applicationContext-A.xml
>    - http://myhost/B/(and all subfolders) gets loaded
>    applicationContext-B.xml
>
> Imagine that I have 2 databases which have the same structure, but hold
> contextually different data. My application logic is the same for both. I
> just have to initialize different "dataSource" and a couple of other beans
> initialized the-A-way or the-B-way.
>
> I managed to define two *WicketServlets (servletA and servletB)* in
> *web.xml
> * and I passed as a parameter the path to *applicationContext-A.xml* and *
> applicationContext-B.xml* respectively.
>
> The problem with this solution is, that I have to load the context in *
> WebApplication* "by hand" and then get the beans out of it with
> *getBean(..)
> * methods, instead of just using *@SpringBean* to wire the beans
> automatically.
>
> Best regards
>



-- 
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com <http://jweekend.com/>