<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7189709087462033576</id><updated>2011-10-15T18:11:22.436+08:00</updated><category term='maven'/><category term='hibernate'/><category term='griffon'/><category term='aspectj'/><category term='general'/><category term='groovy'/><category term='swing'/><category term='freemarker'/><category term='spring'/><category term='modular'/><category term='validation'/><title type='text'>Infinite Goodies</title><subtitle type='html'>creativity, imagination, innovation, knowledge, possibility</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://uberwolf.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7189709087462033576/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://uberwolf.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>dodol</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>6</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7189709087462033576.post-4951662599000036415</id><published>2010-08-30T21:07:00.006+08:00</published><updated>2010-11-21T05:14:47.971+08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='swing'/><category scheme='http://www.blogger.com/atom/ns#' term='groovy'/><title type='text'>Make SwingBuilder more friendly with ExpandoMetaClass</title><content type='html'>SwingBuilder is a nice utility comes with Groovy. It allows you to build basic swing components in more readable way. It supports an excellent bean binding utility and multithreading support. It also allows you to add your custom components. Unfortunately, it's a bit troublesome to create a SwingBuilder instance every time you want to use it. So what I usually do is to create a SwingBuilder object and make it accessible anywhere through ExpandoMetaClass.&lt;br /&gt;&lt;br /&gt;In one of the class, create a SwingBuilder object and return it through a metaClass method:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java"&gt;&lt;br /&gt;def swing = new SwingBuilder();&lt;br /&gt;&lt;br /&gt;Object.metaClass."getSwing" = { -&gt; swing }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;This class must be loaded before any class that need to access this 'getSwing' method. Then you can access the SwingBuilder in any of your Groovy scripts:&lt;br /&gt;&lt;br /&gt;&lt;pre class="brush:java"&gt;&lt;br /&gt;def label = swing.label(text: 'hello world');&lt;br /&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7189709087462033576-4951662599000036415?l=uberwolf.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://uberwolf.blogspot.com/feeds/4951662599000036415/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7189709087462033576&amp;postID=4951662599000036415' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7189709087462033576/posts/default/4951662599000036415'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7189709087462033576/posts/default/4951662599000036415'/><link rel='alternate' type='text/html' href='http://uberwolf.blogspot.com/2010/08/swing-around-ep-2-make-swingbuilder.html' title='Make SwingBuilder more friendly with ExpandoMetaClass'/><author><name>dodol</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7189709087462033576.post-7580057198787937167</id><published>2010-08-29T23:00:00.033+08:00</published><updated>2010-11-21T05:13:30.589+08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='groovy'/><category scheme='http://www.blogger.com/atom/ns#' term='griffon'/><title type='text'>Creating Custom Swing Components with Groovy</title><content type='html'>&lt;div&gt;In the last couple of months, I've been assigned to develop a super complex desktop application using Swing. So you can imagine how frustrating it is for me, who is a web developer. Luckily, I did have experience with it, 3 years ago.&lt;/div&gt;&lt;div&gt;I did some research and found that there were frameworks like Netbeans Platform, Eclipse RCP, and Griffon for RCP development with Java. Unfortunately, the management didn't give me enough time to explore them (I like Griffon but the documentation was not good enough for me during my first month.) My employer only gave me 1-2 weeks to prototype the UI with whatever resources I could find. So I sticked to old plain Swing, using JFrame as the base of everything. I wrote all of the codes in Java, with some help from Spring to maintain the reusability of the components, both widgets or non-widgets. The more functionalities added to the application, the more difficult it was to maintain the dependency between components. My codes became uglier everyday. &lt;/div&gt;&lt;div&gt;After the first sprint of the development, I decided to use Groovy to rewrite all of the codes. I was surprised to find that Swing development became a lot easier with Groovy. The SwingBuilder makes my code more readable. The ExpandoMetaClass makes some of my components like popup menu and dialog easily reusable across all of the widgets that heavily use them.&lt;/div&gt;&lt;div&gt;Below are an example of custom JPanel, for you who are curious on how Groovy can reduce your pains in Swing development:&lt;/div&gt;&lt;pre class="brush:java"&gt;class MyPanel extends JPanel {&lt;br /&gt;  SwingBuilder swing = new SwingBuilder();&lt;br /&gt;&lt;br /&gt;  public MyPanel() {&lt;br /&gt;     name = 'myPanel'&lt;br /&gt;     layout = new BorderLayout();&lt;br /&gt;&lt;br /&gt;     maximumSize = [1024, 768]&lt;br /&gt;     minimumSize = [800, 600]&lt;br /&gt;     preferredSize = [800, 600]&lt;br /&gt;&lt;br /&gt;     def myLabel = swing.label(name: 'myLabel', text: 'hello world')&lt;br /&gt;     add(myLabel, BorderLayout.CENTER)&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Notice that I declared &lt;b&gt;SwingBuilder&lt;/b&gt; on top. In Java, if you need to create a JLabel, you have to do that in at least two to three lines especially when you need to add some fancy background and foreground colors. With &lt;b&gt;SwingBuilder&lt;/b&gt;, you don't need that. You can just used &lt;b&gt;label &lt;/b&gt;factory to create a label in one line. In Java, you have to set the name of component using &lt;b&gt;setName&lt;/b&gt; method and set the layout of the container using &lt;b&gt;setLayout&lt;/b&gt; method. If you need to change the size, you need to use &lt;b&gt;setSize&lt;/b&gt;, &lt;b&gt;setMaximumSize&lt;/b&gt;, &lt;b&gt;setMinimumSize&lt;/b&gt;, and &lt;b&gt;setPreferredSize&lt;/b&gt;.  Now you notice that the custom JPanel in the example looks like configurations. In Groovy you can access the setter methods just like a normal variable assignment. If a setter method accept two parameters you can use square brackets to wrap the parameters (The square brackets are shortcut for List in Groovy).&lt;br /&gt;&lt;div&gt;It's cool, isn't it? If you want, you can also register this custom panel in SwingBuilder as a factory using &lt;b&gt;registerBeanFactory&lt;/b&gt; method. &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7189709087462033576-7580057198787937167?l=uberwolf.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://uberwolf.blogspot.com/feeds/7580057198787937167/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7189709087462033576&amp;postID=7580057198787937167' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7189709087462033576/posts/default/7580057198787937167'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7189709087462033576/posts/default/7580057198787937167'/><link rel='alternate' type='text/html' href='http://uberwolf.blogspot.com/2010/08/swing-around-ep-1-creating-custom-swing.html' title='Creating Custom Swing Components with Groovy'/><author><name>dodol</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7189709087462033576.post-9094540969248412504</id><published>2010-04-27T11:33:00.030+08:00</published><updated>2010-11-21T05:21:47.850+08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='hibernate'/><category scheme='http://www.blogger.com/atom/ns#' term='freemarker'/><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='modular'/><category scheme='http://www.blogger.com/atom/ns#' term='maven'/><title type='text'>Modular Spring Web Application using Spring MVC (without SpringSource Slice)</title><content type='html'>In the past few months, I saw a number of posts questioning on how to create a Modular Spring Web Application with Spring MVC. So I decided to write this post to show you how to write a modular web application for yourself. There are two ways to achieve this: With or Without SpringSource Slice&lt;div&gt;&lt;br /&gt;&lt;div&gt;SpringSource Slice is only available on Spring DM Server (aka Eclipse Virgo) and you need to deploy your application on the DM Server. The approach is very neat. It allows you to deploy your application modules in multiple WAR. But this is probably not the solution most people want because it tie the developers to the DM Server. (I noticed there is an independent project on SpringSource JIRA reserved for SpringSource Slice. Maybe they're going to publish it as a separate framework/application, so we can use it on any project without Spring DM Server)&lt;br /&gt;&lt;br /&gt;It's also possible to create Modular Spring Web App without SpringSource Slice (sort of). The application will be in a form of WAR consists of a number of modules in JAR(s). Each Module JAR will contain views, JavaScript, CSS, images, classes (from controller to data access), and other resources. With this approach, if you need to add a new module, simply drop the JAR into the WEB-INF/lib folder and restart the application/server to test the effect. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;This is different from the old approach of modularizing your application where you partition each layer of your application (DAOs, services, controllers, etc) into multiple JAR(s) and the UI layer into a WAR.&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;All you need is just three tools:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Spring-JS to load static resources like images, css, javascripts from any JAR.&lt;/li&gt;&lt;li&gt;A template engine like FreeMarker or Velocity, that can load templates from any JAR. I don't think JSP files can be loaded from JAR. Maybe if you compile them into classes, it can work.&lt;/li&gt;&lt;li&gt;Hibernate (optional). This is the best persistence framework if you need to load entities from separate JAR(s). Spring and Native Hibernate combo does this thing very nice. I'm not sure if Spring and JPA/Hibernate can do the same magic. &lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;The sample of modular webapp can be downloaded from: &lt;a href="http://www.4shared.com/file/dxNWNlb1/modular-spring.html"&gt;here&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7189709087462033576-9094540969248412504?l=uberwolf.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://uberwolf.blogspot.com/feeds/9094540969248412504/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7189709087462033576&amp;postID=9094540969248412504' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7189709087462033576/posts/default/9094540969248412504'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7189709087462033576/posts/default/9094540969248412504'/><link rel='alternate' type='text/html' href='http://uberwolf.blogspot.com/2010/04/multi-module-spring-web-application.html' title='Modular Spring Web Application using Spring MVC (without SpringSource Slice)'/><author><name>dodol</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7189709087462033576.post-6578315359778702594</id><published>2009-07-06T23:47:00.030+08:00</published><updated>2009-07-07T18:54:12.701+08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='validation'/><category scheme='http://www.blogger.com/atom/ns#' term='aspectj'/><title type='text'>Make our AspectJ Validation more Awesome</title><content type='html'>In the &lt;a href="http://uberwolf.blogspot.com/2009/07/aspectj-validation-for-spring-annotated.html"&gt;previous post&lt;/a&gt;, I had discussion with Stevo. He gave me some pointers for improving the validation aspect. So I did some modifications to make the validation aspect cooler. Thanks Stevo!!&lt;br /&gt;&lt;br /&gt;Here is the modified @Validateable annotation:&lt;br /&gt;&lt;pre class="brush:java"&gt;&lt;br /&gt;package com.mycompany.myapp.aspect.bind;&lt;br /&gt;&lt;br /&gt;import java.lang.annotation.Documented;&lt;br /&gt;import java.lang.annotation.ElementType;&lt;br /&gt;import java.lang.annotation.Inherited;&lt;br /&gt;import java.lang.annotation.Retention;&lt;br /&gt;import java.lang.annotation.RetentionPolicy;&lt;br /&gt;import java.lang.annotation.Target;&lt;br /&gt;&lt;br /&gt;@Documented&lt;br /&gt;@Inherited&lt;br /&gt;@Target(ElementType.METHOD)&lt;br /&gt;@Retention(RetentionPolicy.RUNTIME)&lt;br /&gt;public @interface Validateable {&lt;br /&gt; String[] validators() default {};&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;With this modification, we don't need to provide class type to @Validateable. The validators value is optional. If we leave the validators value empty, our validation aspect will use &lt;span style="font-weight: bold;"&gt;uncapitalize(beanName) + "Validator"&lt;/span&gt; as the default validator value.&lt;br /&gt;&lt;br /&gt;Below is the modified Validation aspect:&lt;br /&gt;&lt;pre class="brush:java"&gt;&lt;br /&gt;package com.mycompany.myapp.aspect.bind;&lt;br /&gt;&lt;br /&gt;import java.lang.annotation.Annotation;&lt;br /&gt;&lt;br /&gt;import org.aspectj.lang.JoinPoint;&lt;br /&gt;import org.aspectj.lang.annotation.Aspect;&lt;br /&gt;import org.aspectj.lang.annotation.Before;&lt;br /&gt;import org.aspectj.lang.reflect.MethodSignature;&lt;br /&gt;import org.apache.commons.lang.StringUtils&lt;br /&gt;import org.springframework.beans.BeansException;&lt;br /&gt;import org.springframework.context.ApplicationContext;&lt;br /&gt;import org.springframework.context.ApplicationContextAware;&lt;br /&gt;import org.springframework.validation.BindingResult;&lt;br /&gt;import org.springframework.validation.Validator;&lt;br /&gt;import org.springframework.web.bind.annotation.ModelAttribute;&lt;br /&gt;&lt;br /&gt;@Aspect&lt;br /&gt;public class Validation implements ApplicationContextAware {&lt;br /&gt; private ApplicationContext applicationContext;&lt;br /&gt; &lt;br /&gt; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {&lt;br /&gt;  // TODO Auto-generated method stub&lt;br /&gt;  if (this.applicationContext == null)&lt;br /&gt;   this.applicationContext = applicationContext;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; private Validator getValidator(String validator) {&lt;br /&gt;  return (Validator) applicationContext.getBean(validator);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; @Before("execution(* (@org.springframework.stereotype.Controller *..*Controller).*(..)) &amp;&amp; " +&lt;br /&gt;   "@annotation(validateable)")&lt;br /&gt; public void validate(JoinPoint joinPoint, Validateable validateable) throws Throwable {&lt;br /&gt;  Object target = null;&lt;br /&gt;  BindingResult result = null;&lt;br /&gt;  &lt;br /&gt;  // Get validators' id/names&lt;br /&gt;  String[] validators = validateable.validators();&lt;br /&gt;  &lt;br /&gt;  // Get the arguments&lt;br /&gt;  Object[] args = joinPoint.getArgs();&lt;br /&gt;  &lt;br /&gt;  // Get annotations of the arguments&lt;br /&gt;  Annotation[][] annotations = ((MethodSignature) joinPoint.getSignature()).getMethod()&lt;br /&gt;                        .getParameterAnnotations();&lt;br /&gt;&lt;br /&gt;// Check arguments array and retrieve the target bean and the BindingResult object&lt;br /&gt;  for (int i = 0; i &amp;lt; args.length; i++) {&lt;br /&gt;   if (target != null &amp;&amp; result != null)&lt;br /&gt;    break;   &lt;br /&gt;   else if (args[i] instanceof BindingResult)&lt;br /&gt;    result = (BindingResult) args[i];&lt;br /&gt;   else if (annotations[i].length &amp;gt; 0) {&lt;br /&gt;    for (Annotation annotation: annotations[i]) {&lt;br /&gt;     if (annotation.annotationType()&lt;br /&gt;         .isAssignableFrom(ModelAttribute.class)) {&lt;br /&gt;      target = args[i];&lt;br /&gt;      break;&lt;br /&gt;     }&lt;br /&gt;    }&lt;br /&gt;   } &lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  if (validators.length == 0)&lt;br /&gt;   validators = new String[] { &lt;br /&gt;     StringUtils.uncapitalize(&lt;br /&gt;        target.getClass().getSimpleName())&lt;br /&gt;                         .concat("Validator") };&lt;br /&gt;  &lt;br /&gt;  for (String validator: validators) {&lt;br /&gt;  // Get the validator bean from application context and validate the target bean&lt;br /&gt;   getValidator(validator).validate(target, result);&lt;br /&gt;   &lt;br /&gt;   if (result.hasErrors())&lt;br /&gt;     break;&lt;br /&gt;   &lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;There are some modifications in the validate() method:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;The validate() method uses @Before instead of @Around. It will accept the normal join point.&lt;br /&gt;&lt;/li&gt;&lt;li&gt;The validate() method doesn't use Architecture.controllerExecution() pointcut. It uses a new pointcut to locate any class annotated with @Controller (I'm not sure if this approach will have performance issue. I usually use the previous approach where the pointcut is declared verbosely.)&lt;br /&gt;&lt;/li&gt;&lt;li&gt;The validate() method uses parameter annotations to locate the position of target bean. We can use AspectJ annotated parameter expression to retrieve the target bean but there is one problem: The position of BindingResult object and the target bean can be random!! This is why we stick to tradional way to locate the annotated parameter.&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;And with the new @Validateable annotation, you don't need to provide the class type and the validators value:&lt;br /&gt;&lt;pre class="brush:java"&gt;&lt;br /&gt; @RequestMapping(method=RequestMethod.POST, params="create")&lt;br /&gt; @Validateable&lt;br /&gt; public String create(@ModelAttribute("example") Example example,&lt;br /&gt;       BindingResult bindingResult, WebRequest webRequest) {&lt;br /&gt;  if (bindingResult.hasErrors()) {&lt;br /&gt;   System.out.println("AspectJ validation is amazing!!!");&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  return null;&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;Because we don't provide validators value, the default validators value will be &lt;span style="font-weight:bold;"&gt;exampleValidator&lt;/span&gt; ^^&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7189709087462033576-6578315359778702594?l=uberwolf.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://uberwolf.blogspot.com/feeds/6578315359778702594/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7189709087462033576&amp;postID=6578315359778702594' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7189709087462033576/posts/default/6578315359778702594'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7189709087462033576/posts/default/6578315359778702594'/><link rel='alternate' type='text/html' href='http://uberwolf.blogspot.com/2009/07/make-our-aspectj-validation-more.html' title='Make our AspectJ Validation more Awesome'/><author><name>dodol</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7189709087462033576.post-7580559480368261527</id><published>2009-07-04T15:25:00.041+08:00</published><updated>2009-07-08T10:37:15.531+08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='spring'/><category scheme='http://www.blogger.com/atom/ns#' term='validation'/><category scheme='http://www.blogger.com/atom/ns#' term='aspectj'/><title type='text'>AspectJ Validation for Spring annotated controller</title><content type='html'>Spring 2.5.x introduces annotated controller that makes web development with Spring MVC a lot easier. No more XML is required for each controller you define.&lt;br /&gt;&lt;br /&gt;Spring MVC's annotated controller reminds me of &lt;a href="http://en.wikipedia.org/wiki/Uncle_Ben"&gt;Ben Parker&lt;/a&gt;'s famous quote: "With great power comes great responsibility". What's the great responsibility you will have for using the powerful annotated controller? The validation!! &lt;br /&gt;&lt;br /&gt;With the old base-controller-classes approach you can just inject validators to your form controller and Spring MVC will take care of the validation for you. With annotated controller, this validation mechanism is removed. The common way to do validation with annotated controller is to inject Validator objects into your annotated controller and use its validate() method on the bean you want to validate. It's painful to write these validation steps in every request method I want to validate. The more of these validation codes I have in my method body, the uglier my method will be (Imagine if you have to use multiple validators to validate a single bean).&lt;br /&gt;&lt;br /&gt;Luckily, with Spring 2.x's @Aspect, we can write a nice validation mechanism for our annotated controllers. If you want your request method to be validated, you just add annotation on top of your method and condition to check if the binding result has errors.&lt;br /&gt;&lt;pre class="brush:java"&gt;&lt;br /&gt; @Validateable(type=Example.class, validators="exampleValidator")&lt;br /&gt; public String create(@ModelAttribute("example") Example example,&lt;br /&gt;       BindingResult bindingResult, WebRequest webRequest) {&lt;br /&gt;  &lt;br /&gt;  if (bindingResult.hasErrors())/*do something here*/;&lt;br /&gt;&lt;br /&gt;  return null;&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;First, write the @Validateable annotation:&lt;br /&gt;&lt;pre class="brush:java"&gt;&lt;br /&gt;package com.mycompany.myapp.aspect.bind;&lt;br /&gt;&lt;br /&gt;import java.lang.annotation.Documented;&lt;br /&gt;import java.lang.annotation.ElementType;&lt;br /&gt;import java.lang.annotation.Inherited;&lt;br /&gt;import java.lang.annotation.Retention;&lt;br /&gt;import java.lang.annotation.RetentionPolicy;&lt;br /&gt;import java.lang.annotation.Target;&lt;br /&gt;&lt;br /&gt;@Documented&lt;br /&gt;@Inherited&lt;br /&gt;@Target(ElementType.METHOD)&lt;br /&gt;@Retention(RetentionPolicy.RUNTIME)&lt;br /&gt;public @interface Validateable {&lt;br /&gt; Class&lt;?&gt; type();&lt;br /&gt; String[] validators();&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;@Validateable accepts two values: the target bean's class type and validator beans' name/id.&lt;br /&gt;&lt;br /&gt;Next, write the Architecture aspect for your application:&lt;br /&gt;&lt;pre class="brush:java"&gt;&lt;br /&gt;package com.mycompany.myapp.aspect.system;&lt;br /&gt;&lt;br /&gt;import org.aspectj.lang.annotation.Aspect;&lt;br /&gt;import org.aspectj.lang.annotation.Pointcut;&lt;br /&gt;&lt;br /&gt;@Aspect&lt;br /&gt;public class Architecture {&lt;br /&gt; @Pointcut("execution(* com.mycompany.myapp..*Controller*.*(..))")&lt;br /&gt; public final void controllerExecution() {}&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;The Architecture aspect will intercept any call that triggers any controller's method.&lt;br /&gt;&lt;br /&gt;Then write the validation aspect:&lt;br /&gt;&lt;pre class="brush:java"&gt;&lt;br /&gt;package com.mycompany.myapp.aspect.bind;&lt;br /&gt;&lt;br /&gt;import org.aspectj.lang.ProceedingJoinPoint;&lt;br /&gt;import org.aspectj.lang.annotation.Around;&lt;br /&gt;import org.aspectj.lang.annotation.Aspect;&lt;br /&gt;import org.springframework.beans.BeansException;&lt;br /&gt;import org.springframework.context.ApplicationContext;&lt;br /&gt;import org.springframework.context.ApplicationContextAware;&lt;br /&gt;import org.springframework.validation.BindingResult;&lt;br /&gt;import org.springframework.validation.Validator;&lt;br /&gt;&lt;br /&gt;@Aspect&lt;br /&gt;public class Validation implements ApplicationContextAware {&lt;br /&gt; private ApplicationContext applicationContext;&lt;br /&gt; &lt;br /&gt; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {&lt;br /&gt;  // TODO Auto-generated method stub&lt;br /&gt;  if (this.applicationContext == null)&lt;br /&gt;   this.applicationContext = applicationContext;&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; private Validator getValidator(String validator) {&lt;br /&gt;  return (Validator) applicationContext.getBean(validator);&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; @Around("com.mycompany.myapp.aspect.system.Architecture.controllerExecution() &amp;&amp; " +&lt;br /&gt;   "@annotation(validateable)")&lt;br /&gt; public Object validate(ProceedingJoinPoint joinPoint, Validateable validateable) throws Throwable {&lt;br /&gt;  Object target = null;&lt;br /&gt;  BindingResult result = null;&lt;br /&gt;  &lt;br /&gt;  // Get the class type and validators' id/names&lt;br /&gt;  Class&lt;?&gt; type = validateable.type();&lt;br /&gt;  String[] validators = validateable.validators();&lt;br /&gt;  &lt;br /&gt;  // Get the arguments&lt;br /&gt;  Object[] args = joinPoint.getArgs();&lt;br /&gt;  &lt;br /&gt;  // The bindingResultIndex will hold the index of BindingResult object&lt;br /&gt;  Integer bindingResultIndex = null;&lt;br /&gt;&lt;br /&gt;  // Check arguments array and retrieve the target bean and the BindingResult object&lt;br /&gt;  for (int i = 0; i &amp;lt; args.length; i++) {&lt;br /&gt;   if (args[i] instanceof BindingResult) {&lt;br /&gt;    if (bindingResultIndex == null) {&lt;br /&gt;     result = (BindingResult) args[i];&lt;br /&gt;     bindingResultIndex = new Integer(i);&lt;br /&gt;    }&lt;br /&gt;   } else if (type.isInstance(args[i])) {&lt;br /&gt;    target = args[i];   &lt;br /&gt;   } else if (target != null &amp;&amp; result != null)&lt;br /&gt;    break;&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  for (String validator: validators) {&lt;br /&gt;   // Get the validator bean from application context and validate the target bean&lt;br /&gt;   getValidator(validator).validate(target, result);&lt;br /&gt;   &lt;br /&gt;   if (result.hasErrors()) {&lt;br /&gt;    // Assign the BindingResults back to the arguments array&lt;br /&gt;    args[bindingResultIndex.intValue()] = result;&lt;br /&gt;    break;&lt;br /&gt;   }&lt;br /&gt;  }&lt;br /&gt;  &lt;br /&gt;  return joinPoint.proceed(args);&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;Validation aspect will intercept any call to a controller's request methods annotated with @Validateable annotation. It will validate the target bean and set the validation results to the BindingResult object.&lt;br /&gt;&lt;br /&gt;Declare the validation aspect as a Spring bean in one of your context:&lt;br /&gt;&lt;pre class="brush:xml"&gt;&lt;br /&gt;&amp;lt;aop:aspectj-autoproxy /&amp;gt;&lt;br /&gt;&amp;lt;bean class="com.mycompany.myapp.aspect.bind.Validation" factory-method="aspectOf" /&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Create the target bean:&lt;br /&gt;&lt;pre class="brush:java"&gt;&lt;br /&gt;package com.mycompany.myapp.mymodule.example.model;&lt;br /&gt;&lt;br /&gt;public class Example {&lt;br /&gt; private String value;&lt;br /&gt; &lt;br /&gt; public String getValue() {&lt;br /&gt;  return value;&lt;br /&gt; }&lt;br /&gt; &lt;br /&gt; public void setValue(String value) {&lt;br /&gt;  this.value = value;&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Write a simple validator for your bean:&lt;br /&gt;&lt;pre class="brush:java"&gt;&lt;br /&gt;package com.mycompany.myapp.mymodule.example.validator;&lt;br /&gt;&lt;br /&gt;import org.springframework.stereotype.Component;&lt;br /&gt;import org.springframework.validation.Errors;&lt;br /&gt;import org.springframework.validation.ValidationUtils;&lt;br /&gt;import org.springframework.validation.Validator;&lt;br /&gt;&lt;br /&gt;import com.mycompany.myapp.mymodule.example.model.Example;&lt;br /&gt;&lt;br /&gt;@Component("exampleValidator")&lt;br /&gt;public class ExampleValidator implements Validator {&lt;br /&gt; public boolean supports(Class arg) {&lt;br /&gt;  // TODO Auto-generated method stub&lt;br /&gt;  return Example.class.isAssignableFrom(arg);&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt; public void validate(Object object, Errors errors) {&lt;br /&gt;  // TODO Auto-generated method stub&lt;br /&gt;  ValidationUtils.rejectIfEmptyOrWhitespace(errors, "value", "field.required");&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Finally, add @Validateable annotation to the request method in your annotated controller:&lt;br /&gt;&lt;pre class="brush:java"&gt;&lt;br /&gt; @RequestMapping(method=RequestMethod.POST, params="create")&lt;br /&gt; @Validateable(type=Example.class, validators="exampleValidator")&lt;br /&gt; public String create(@ModelAttribute("example") Example example,&lt;br /&gt;       BindingResult bindingResult, WebRequest webRequest) {&lt;br /&gt;&lt;br /&gt;  if (bindingResult.hasErrors()) {&lt;br /&gt;    System.out.println("AspectJ Validation is amazing!!!");&lt;br /&gt;  }&lt;br /&gt;  return null;&lt;br /&gt; }&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Don't forget to add &lt;a href="http://mojo.codehaus.org/aspectj-maven-plugin/usage.html"&gt;AspectJ plugin&lt;/a&gt; to your pom.xml. If you use ant, you can use &lt;a href="http://www.eclipse.org/aspectj/doc/released/devguide/antTasks.html"&gt;AspectJ's Ant task&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Note: You can check an improved version of this validation system in the &lt;a href="http://uberwolf.blogspot.com/2009/07/make-our-aspectj-validation-more.html"&gt;next post&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7189709087462033576-7580559480368261527?l=uberwolf.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://uberwolf.blogspot.com/feeds/7580559480368261527/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7189709087462033576&amp;postID=7580559480368261527' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7189709087462033576/posts/default/7580559480368261527'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7189709087462033576/posts/default/7580559480368261527'/><link rel='alternate' type='text/html' href='http://uberwolf.blogspot.com/2009/07/aspectj-validation-for-spring-annotated.html' title='AspectJ Validation for Spring annotated controller'/><author><name>dodol</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7189709087462033576.post-6105621062252257537</id><published>2009-01-03T15:00:00.013+08:00</published><updated>2009-06-14T18:37:18.328+08:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='general'/><title type='text'>Infinite Goodies</title><content type='html'>&lt;div  style="text-align: center;font-family:trebuchet ms;"&gt;&lt;div style="text-align: left;"&gt;Albert Einstein once said:&lt;span style="font-style: italic;"&gt; "Only two things are infinite, the universe and human stupidity, and I'm not sure about the former".&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;IMHO, he was wrong. Both universe and human stupidity might be infinite. But there a lot of infinite things besides the two things he mentioned, especially the good things:&lt;span style="font-style: italic;"&gt;&lt;span style="font-weight: bold;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;span style="font-style: italic; font-weight: bold;"&gt;Creativity&lt;/span&gt;, &lt;span style="font-style: italic; font-weight: bold;"&gt;Imagination&lt;/span&gt;,&lt;span style="font-style: italic;"&gt; &lt;span style="font-weight: bold;"&gt;Innovation&lt;/span&gt;&lt;/span&gt;,&lt;span style="font-style: italic;"&gt; &lt;span style="font-weight: bold;"&gt;Knowledge&lt;/span&gt;&lt;/span&gt;,&lt;span style="font-style: italic;"&gt; &lt;/span&gt;and&lt;span style="font-style: italic;"&gt; &lt;span style="font-weight: bold;"&gt;Possibility&lt;/span&gt;&lt;/span&gt;. If these five things are finite, we would be living in stone age these days. Those are the only five infinite goodies I know, and I believe there are more.&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;span style="font-family:trebuchet ms;"&gt;The blog title is unrelated to whats inside, so don't expect too much of infinite goodies in my blog ;p&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7189709087462033576-6105621062252257537?l=uberwolf.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://uberwolf.blogspot.com/feeds/6105621062252257537/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7189709087462033576&amp;postID=6105621062252257537' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7189709087462033576/posts/default/6105621062252257537'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7189709087462033576/posts/default/6105621062252257537'/><link rel='alternate' type='text/html' href='http://uberwolf.blogspot.com/2009/01/albert-einstein-said-only-two-things.html' title='Infinite Goodies'/><author><name>dodol</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
