I posted a sweet little article on using Spring IoC and some annotation based Spring HttpSession management facilities over at TheServerSide.com. If you're running into any problems getting the example to work, this thread is a great place to ask a question or two on the topic.
At the top of the article there's a link for people to ask questions or post any problems they might be having trying to run the program in their own environment, and that link leads here. Hopefully if anyone has difficulties getting the program to run, they can post here and we can get their questions answered!
The tutorial itself just creates a little JSP that leverages the Spring API:
By the way, the real key to this whole thing is having the correct entries in the web.xml file:
In the end, it creates a cute little application that counts button clicks. Nothing earth shattering, but its simplicity is what makes it such a great learning tool. Here's what the finished product looks like:
So, here's hoping that if anyone runs into a problem getting the little application to work, we can field their questions here and get them up and running!
I wouldn't use the name "beanFactory" as the variable name. It would confuse people too much. A BeanFactory is too Spring 2.0 and before and not used directly in today's Spring application.
Also, The code is in a jsp, and today people will never or should never tie code directly in a JSP page.
On one other note. The better approach is still just a ContextLoaderListener, calling your one xml file ApplicationContext.xml and placing it in your WEB-INF directory.
You won't need a contextParam for the file. In the ApplicationContext.xml you can just include one tag <context:component-scan base-package=""/> This will scan for @Configuration and any @Components. Now you also wouldn't need to tell the web.xml to use the AnnotationConfigWebApplicationContext and enter it in the web.xml.
I think this removes a good 15-20 lines in the web.xml, but only the need for one line in the ApplicationContext.xml for the component-scan.
I find more people use my approach than what you used, and you wouldn't need the org.springframework.web.context.request.RequestContextListener
I use many beans that are scoped as Singleton on web pages, they can be used in Servlets and JSP.
I struggle with the 'beanFactory' name. The ApplicationContext does implement the BeanFactory interface, AnnotationConfigApplicationContext JavaDoc, so it is still a technically correct name, but I agree, it's very Spring 2.0. The reason why I tend to keep it is that I fear people who have prior Spring knowledge might look at it and think 'what the heck is that?' when they see an applicationContext. This way, people with prior Spring experience just look at it and right away think 'oh, that's just the beanFactory he's got there.' But I'm pretty on the fence about that.
Yeah, the code in the JSP is definitely not a best practice. I'm getting hammered for doing that over at TSS. But once you've got a Servlet in there, you need to edit the web.xml file and throw in a servlet definition and mapping, and for someone struggling to get things working, I worry that just might be an extra step they don't need. I'm hoping anyone that's advanced enough to be doing Spring and Java web development will appreciate the androgological benefit of this example, while realizing that it's not going to win anyone a programmer of the year award.
At the very end of the example I showed how to use an ApplicationContext.xml file, so it's not completely void of XML. I'm not a complete Annotations bigot!
Hmmm...I'm wondering if that's a spider that catches a fly? Is there an Annotation/Java based equivalent of this? I'd really like the first iteration of the application to work without using any XML file at all. Do I absolutely need an XML file to do a component scan? I wonder if there's a context param you can feed the AnnotationConfigWebApplicationContext in the web.xml file that will force the context to do the same scan? Something like this:
Maybe I just have to provide a package name for the contextConfigLocation:
"Unlike XmlWebApplicationContext, no default configuration class locations are assumed. Rather, it is a requirement to set the "contextConfigLocation" context-param for ContextLoader and/or "contextConfigLocation" init-param for FrameworkServlet. The param-value may contain both fully-qualified class names and base packages to scan for components."
By the way, the first comment I got at TSS was from SpringSource taking an exception to my complaint about a lack of good documentation. One of the articles they pointed to as proof of great documentation was yours.
I also don't think any users of pre-3.0 Spring would get confused by calling it 'applicationContext' instead of 'beanFactory' - ApplicationContext has existed at least Spring 1.1 (though I'm not sure how much it was used at that time...). I've been using Spring since 2.0/2.5 and I think I almost always used ApplicationContext (since this was what Spring MVC used) and used BeanFactory only in tests.
Maybe then you could use an @Controller annotated class to back the JSP and get the code out of the JSP (though, on second thought, I think you'd still have to set up a dispatcher servlet to handle this, and you mentioned you didn't want to).
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
subject: A Super Simple Servlet API Based Web Application using Spring 3.0 as the IoC Container