Annotations vs. xml configuration

The purpose of this article is to sum up pros and cons of java annotation and xml configurations.

Spring is the most popular application development framework for enterprise Java™. Millions of developers use Spring to create high performing, easily testable, reusable code without any lock-in. So does Lexaden!

Java-based container configuration

The central artifact in Spring's new Java-configuration support is the @Configuration-annotated class. These classes consist principally of @Bean-annotated methods that define instantiation, configuration, and initialization logic for objects to be managed by the Spring IoC container.

Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. You can find a full Spring 3 reference documentation here

There are some pros and cons of annotation configuration:

  • When it comes to JPA configuration since it is done once and usually are not changed quite often we stick to annotation configuration. You may have a concern regarding possibility to see a bigger picture of configuration - in this case we use MSQLWorkbench diagrams.
  • Xml configuration is very good to get a bigger picture of application but it maybe cumbersome to find some errors until runtime. In this case Spring @Configuration annotation sounds as a better choice since it let us see a bigger picture as well and also allows to validate configuration on compile time.
  • As for Spring configuration we combine both approaches: use @Configuration annotation with Services and Query interfaces and xml configuration for dataSource and spring configuration stuff like context:component-scan base-package="com.lexaden.platform"
  • But xml configuration bits java annotations when it comes to flow configuration configuration since it is extremely important to see a bigger picture of the whole business process. And it sounds cumbersome to have it implemented with annotations approach.

We combined both approaches - java annotations and essential xml minimum that gave us benefits of both without shortcomings. 


@Configuration
public class AccountServiceContext {
    @Bean
    public UserAccountService accountService() {
        return new UserAccountServiceImpl();
    }

    @Bean
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    public RegisterAccountForm registerAccountForm() {
        return new RegisterAccountForm();
    }

    @Bean
    @State(REGISTER_ACCOUNT)
    @Placeholder(CONTENT)
    @Scope(BeanDefinition.SCOPE_PROTOTYPE)
    public RegisterAccountController registerAccountController() {
        return new RegisterAccountController();
    }

}


	  <flow xmlns="...">

	    <module id="authentication" initial="login" extends="module">
	
        	<controller id="login" extends="controller">
            		<on event="account.details" to="account.details"/>
		        <on event="register.account" to="register.account"/>
        	</controller>
        	<controller id="account.details" extends="controller">
            		<on event="cancel" to="login"/>
        	</controller>
        	<controller id="register.account" extends="controller" >
            		<view id="displayView" extends="controller/displayView">
                		<on event="register" to="register"/>
            		</view>
            		<action id="register" extends="action">
                		<on event="error" to="displayView"/>
            		</action>
            		<final id="register" extends="action"/>
        	</controller>

        	<on event="login" to="login"/>
        	<on event="cancel" to="platform"/>
    	   </module>   </flow>

Lexaden
Easy to keep focus on business needs