Note: Before you decide to use Spring in your web applications, you should note that Mentawai has a very powerful, flexible and simple IoC container, that can handle most of your IoC (Inversion of Control) and DI (Dependency Injection) needs automatically and transparetly. Please check the IoC and DI sections before making up your mind to using Spring.
Spring (http://www.springframework.org) is an IoC container and an integration framework that has acquired a good popularity recently. Therefore, to allow those that want or need to use Spring with their web applications, Mentawai now offers two easy ways to integrate Spring with Mentawai, so you can choose the best one for your taste and needs.
The first version of Spring was written by Rod Johnson which is released with his book in 2002. It was then released the following year under the Apache 2.0 license. It then really took off and is often the framework used for the Java platform, so it may be used in anything from bingo games you play online, to film websites with sliders on the homepage. It is popular in the Java community as an alternative for or even in place of the Enterprise JavaBean (EJB) model.
In this approach, the actions are treated as Spring beans, in other words, the action as well as its dependencies are managed by Spring and defined in the beans.xml file:
<beans> <bean id="helloBean" class="examples.spring.HelloBean"/> <bean id="helloAction" class="examples.spring.HelloAction" singleton="false"> <property name="helloBean"><ref local="helloBean"/></property> </bean> </beans>
The class HelloAction above is a regular action that extends BaseAction. Note that it has a dependency called "helloBean", which will be treated/injected by Spring. To configure our ApplicationManager to use Spring to manage the HelloAction, all we have to do is make use of the SpringActionConfig, which is a subclass of ActionConfig:
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("beans.xml")); SpringActionConfig.setBeanFactory(beanFactory); // Ruby style spring("/hello", "helloAction") .on(SUCCESS, fwd("/index.jsp")); // Java style SpringActionConfig ac = new SpringActionConfig("/hello", "helloAction"); ac.addConsequence(SUCCESS, new Forward("index.jsp")); addActionConfig(ac);
Note that we must inform to the SpringActionConfig which Spring's BeanFactory it will use, and we can do that through the static method setBeanFactory. Also note that we did not reference the action class in the constructor as we do with a regular ActionConfig, but this time we used the name that was configured inside the Spring's beans.xml file, in this case "helloAction". For the action dependency, as long as the action has a setter method setHelloBean, Spring will take care of injecting this dependency inside the action, as it is configured in the beans.xml file.
The second approach is based on a Mentawai filter (org.mentawai.core.Filter) that creates a bridge between the Spring and the action input, so that whenever you ask for an object from the action input, the filter will first ask Spring if it knows about it:
BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("beans.xml")); // Ruby style filter(new SpringFilter(beanFactory)); // global filter action("/springFilterAction", SpringFilterAction.class) .on(SUCCESS, fwd("/index.jsp")) // Java style addGlobalFilter(new SpringFilter(beanFactory)); // global filter ActionConfig ac = new ActionConfig("/myAction", SpringAction.class); ac.addConsequence(SUCCESS, new Forward("/index.jsp"); addActionConfig(ac);
Considering that the Spring's beans.xml file has a bean definition called "helloBean", all you have to do in your action is call input.getValue("helloBean") to get a bean straight from the Spring container.
You can choose whichever approach is better for your needs and taste. The second approach is somewhat lighter than the first, as not your whole action is managed by Spring but only some of its dependencies.
In the examples above, we have created the BeanFactory manually. However you can also use the Spring's WebApplicationContext, an interface that inherits from BeanFactory, by configuring it in the web.xml file. Here is an example:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
By doing that, Spring will automatically look for the file WEB-INF/applicationContext.xml to create the BeanFactory. This is the recommended apporach by Spring to do integration with other frameworks. For more details you can access the Spring documentation here.