Skip to main content

What do you think about writing an Web application almost without XML?

Posted by urubatan on July 27, 2006 at 7:34 PM PDT

Yes, it is possible, and this is the main goal of the project spring-annotation here at

After the initial setup of the application (configuring the database access, and some config at the web.xml), all you need to do is use the provided annotations to setup your beans, and even this way, you can use all the power of the spring framework.

The project has now support for development using Spring MVC and JSF, and even using JSF, if you accept to use "Configuration by exception", you do not need to touch your faces-config.xml file.

To create a Spring-MVC application, all the XML you need, for the entire development cycle of your application is:
an applicationContext.xml with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi="" xmlns:tx="" xmlns:aop=""
    default-autowire="byName" default-lazy-init="false">
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName">
        <property name="url">
        <property name="username">
        <property name="password">
    <bean id="sessionFactory" class="">
       <property name="configLocations" value="classpath*:hibernate.cfg.xml" />
       <property name="hibernateProperties">
             <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
       <property name="dataSource">
          <ref bean="dataSource" />
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" />
    <tx:annotation-driven />
    <!-- ************************ Configuration ******************************** -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"><value>org.springframework.web.servlet.view.JstlView</value></property>
        <property name="prefix"><value>/</value></property>
        <property name="suffix"><value>.jsp</value></property>
    <bean id="methodNameResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
        <property name="paramName"><value>action</value></property>
        <property name="defaultMethodName"><value>init</value></property>
    <bean id="messageSource" class="">
        <property name="basename"><value>messages</value></property>

setup your web.xml file with the following:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="" xmlns:xsi="" xsi:schemaLocation="">


an almost blank spring-servlet.xml (this is needed because of the spring servlet dependency on it)

<?xml version="1.0" encoding="UTF-8"?>


and after that, you do not need to touch any XML file, not even the hibernate.cfg.xml to register your persistent classes, because the spring-annotation will detect all of then to you.

create a blank in the root of your classpath, copy the needed jar files to your WEB-INF/lib, and start coding.

you can, for example, create a class like the following:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class TestController implements Controller {

    public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new ModelAndView("myJspName");

and access it using the URL:

without the need for all that XML stuff.

if you want to work with JSF, all the beans registered within the spring context will be available to the JSF EL.
You get an @DataModel, @DataModelSelection and @DataModelIndex like the ones in the JBoss Seam
and You get two extra scopes for your beans:
flash - the same as RoR, where yout objects live until the next reqest.
conversation - your beans live until you call a JSF Action method annotated with @ConvEnd (yes, this idea is from JBoss Seam too)

I think it already makes the web development a lot easier.

The pproject is almost reaching it`s 1.0 release, there is missing documentation, the examples are almost finished, and some features implemented as JSF only will be backported to the Spring-MVC too.

I hope you like this project.
There are already some companies at brazil using it in development and production environments.

If some of you are interested in start using it, just use the project mailing lists as a support channel.

Related Topics >>