title: Spring @Required annotation not working as expected
body: <p>I'm trying to test out Spring Annotations to see how they work with some simple examples derived from the <a href="http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-annotation-config" rel="nofollow noreferrer">Spring 3.0 Source</a> (in this case the "@Required" annotation specifically).</p>
  
  <p>To start, I came up with a basic "Hello World" type example that doesn't use any annotations.  This works as expected (i.e. prints "Hello Spring 3.0~!").</p>
  
  <p>I then added a <code>DAO</code> object field to the <code>Spring3HelloWorld</code> class.  My intention was to deliberately cause an exception to occur by annotating the setter for the <code>DAO</code> with <code>@Required</code> but then not setting it.  However, I get a null pointer exception (since <code>this.dao</code> is null) when I was expecting an exception based on not following the annotation "rules/requirements".</p>
  
  <p>I thought I would have needed to set the <code>DAO</code> object before calling any method from <code>Spring3HelloWorld</code>, but apparently that's not the case.  I assume I'm misunderstanding how <code>@Required</code> works.</p>
  
  <p>So basically how would I get the following to give me an error along the lines of "Hey you can't do that, you forgot to set DAO blah blah blah".</p>
  
  <p>Spring3HelloWorldTest.java:</p>
  
  <pre><code>import org.springframework.beans.factory.xml.XmlBeanFactory;
  import org.springframework.core.io.ClassPathResource;
  
  public class Spring3HelloWorldTest {
  
      public static void main(String[] args) {
  
          XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource     ("SpringHelloWorld.xml"));
  
          Spring3HelloWorld myBean = (Spring3HelloWorld) beanFactory.getBean("spring3HelloWorldBean");
          myBean.sayHello();        
  
      }
  }
  </code></pre>
  
  <p>Spring3HelloWorld.java:</p>
  
  <pre><code>import org.springframework.beans.factory.annotation.Required;
  
  public class Spring3HelloWorld {
  
      private DAO dao;
  
      @Required
      public void setDAO( DAO dao ){
          this.dao = dao;
      }
  
      public void sayHello(){
          System.out.println( "Hello Spring 3.0~!" );
  
          //public field just for testing
          this.dao.word = "BANANA!!!";
      }
  }
  </code></pre>
  
  <p>SpringHelloWorld.xml:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="http://www.springframework.org/schema/beans 
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;context:annotation-config/&gt;
  
      &lt;bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/&gt;
  
      &lt;bean id="dao" class="src.DAO" &gt;&lt;/bean&gt;
      &lt;bean id="spring3HelloWorldBean" class="src.Spring3HelloWorld" &gt;&lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>