I have a small Scala/Spring MVC project that at the moment uses two DispatcherServlets, one for each controller. There was a historical reason for this (I needed two different Velocity configurations) and while I can now probably get away with a single DispatcherServlet, what I'm going to try to do is move one of the servlets over to a Scala based configuration and leave one using xml based configuration.
Of the two configurations, I'm going to move the simplest one over. It does nothing more than define a root controller bean. So the Scala config should be as simple as
@Configuration
class CalendarConfig {
@Bean def rootController = new CalendarController()
}
And a quick change to my web.xml
...
<servlet>
<servlet-name>calendars</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<!-- Configure DispatcherServlet to use JavaConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<!-- Again, config locations must consist of one or more comma- or space-delimited
and fully-qualified @Configuration classes -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.maethorechannen.calendars.CalendarConfig</param-value>
</init-param>
</servlet>
...
I started my app up in my local development Google App Engine server and it failed to start up, thanks to "java.lang.IllegalStateException: CGLIB is required to process @Configuration classes. Either add CGLIB to the classpath or remove the following @Configuration bean definitions: [calendarConfig]". I added cglib-nodep-2.2 (available from here if you're not using something like maven or ivy) to my library of jars and it started to work.
In case anyone's interested - I pushed it to App Engine and it still worked (occasionally, what works locally doesn't work when on the Big G's servers). One thing I'm really interested in seeing is what (if any) sort of impact on startup times programmatic configuration has compared to XML configuration. When using GAE every millisecond of startup time counts and I would think Scala based configuration should be quicker.
What's interesting to note (at least to me) is that I didn't need to create a global WebApplicationContext or move everything over to Java(Scala) based configuration, which is something I wasn't sure about after reading the documentation from SpringSource.
2 comments:
Hi Scot,
I did the same basic thing here, using FreeMarker, but I'll be replacing it with Velocity so I can deploy into GAE later.
Your Uberspect implementation is awesome - do you mind if I use it?
Thanks!
@Rob
Feel free to use it.
Post a Comment