PDA

View Full Version : Initializing an IApplicationContext in a web application


Dennis Homann
12-14-2004, 10:32 AM
Section 3.2.1 of the very readable documentation refers to some sort of ASP.NET web layer support to load an application context:

For example, the ASP.NET web layer provides support code to load a Spring.NET IApplicationContext automatically as part of the normal startup process of an ASP.NET web application.

Unfortunately, I could not find any further information on this in the API documentation. Of course, it is trivial to implement IHttpModule to this, but I would like to avoid writing code with direct dependencies on Spring.

Is there more information on this topic available or is this a feature of a future release?

Thanks,
Dennis

Rick Evans
12-14-2004, 05:58 PM
Hi Dennis

Apologies... the axe deserves to fall on my head for that one :oops: . That's some documentation that's slipped in from code that has yet to be released publicly.

You may want to check out the latest CVS HEAD and refer to the WebApplicationContextModule in the Spring.Web.Support namespace... this class provides the functionality that you require.

The web support is scheduled to be released in the release right after the release that is scheduled for this weekend.

Ciao
Rick

jjx
12-15-2004, 07:11 AM
web.config

<configuration>
<configSections>
<sectionGroup name="spring">
<section name="context" type="Spring.Context.WebApplicationContextHandler,Spring .Web"/>
</sectionGroup>
</configSections>
<spring>
<context resource="~/applicationcontext.xml"/>
</spring>
<system.web>
<httpModules>
<add type="Spring.Web.Support.WebApplicationContextModule,Spr ing.Web" name="WebApplicationContextModule"/>
</httpModules>
...


using

IYellowPageService yellowPageService=(IYellowPageService)WebApplicati onContext.Current["yellowPageService"];

Aleks Seovic
12-27-2004, 09:50 AM
Ideally, there should be no need for the code from the usage example above -- you can simply use Spring IoC to configure YellowPageService property.

Configuration itself looks ok, although if you want to inject dependencies into .aspx pages using Spring you need the following as well:


<httpHandlers>
<add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/>
</httpHandlers>


-- Aleks

Anonymous
01-07-2005, 05:58 AM
In your app context you can then define pages something like this

<object class="WebForm.aspx">
<property name="Dummy"><value>hello world</value></property>
</object>

One question I do have is, in an existing application there may be alot of dynamic pages which dont need to be managed by spring. My experience has been that all aspc pages need to be defined in the app context. Can someone confirm this. Its a little annoying especially if your migrating an existing webapp with a large number of aspx pages. If anyone knows of a way around this without making changes to the code please let me know.

If there isnt a way around it I'm thinking making some modifcations to allow for a default page definition to be returned if the page isnt defined in the app context rather then an exception being thrown.

Let me know what you think.

Anonymous
01-07-2005, 09:16 AM
There is a simple way to accomplish what you want right now.

Example that I sent to developer list maps both .aspx and .spring extensions to Spring's PageHandlerFactory. However, you don't have to do that -- you can simply map only .spring extension to our handler factory (or any other extension you'd like to use -- just make sure that you configure it in IIS as well).

That way pages with custom extension will be instantiated and configured using Spring IoC container, while regular .aspx pages will be processed using standard .Net page handler factory.

Again, the idea is not that Spring should replace existing .Net features and impose requirements of its own. We built a way to make pages configurable by Spring IoC container, but it is entirely up to you whether you want to use it for all the pages, some or none. You can use other features of Spring.Web, such as data binding and master pages, even if you don't use IoC to configure them (although, master pages are much more flexible when you configure them using IoC).

Hope this helps,

Aleks

Anonymous
01-09-2005, 11:57 PM
Yes, I thought of simply defining a different extension, however looking in the code I can see some hard coded references to ".aspx". How will the container know that something with a ".spring" extension really a page that can only be instantiated by the asp.net framework using the page compilation classes that come with asp.net.

Aleks Seovic
01-22-2005, 12:56 AM
Whatever extension you define will be a "virtual" extension -- used in URLs but without matching files on the filesystem.

Extension only determines which PageHandlerFactory to use, no more and no less. Once extension is mapped to Spring's PageHandlerFactory we simply extract name without the extension and look up appropriate object from the application context using that name.

Object definition decides what we are going to use to handle the request. In most cases, it will map to an existing physical .aspx page that you want to render, but you could also substitute that with any class that implements IHttpHandler interface.

Hope this helps,

Aleks