tags:

views:

38

answers:

1

I'm trying to test out Spring Annotations to see how they work with some simple examples derived from the Spring 3.0 Source (in this case the "@Required" annotation specifically).

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~!").

I then added a DAO object field to the Spring3HelloWorld class. My intention was to deliberately cause an exception to occur by annotating the setter for the DAO with @Required but then not setting it. However, I get a null pointer exception (since this.dao is null) when I was expecting an exception based on not following the annotation "rules/requirements".

I thought I would have needed to set the DAO object before calling any method from Spring3HelloWorld, but apparently that's not the case. I assume I'm misunderstanding how @Required works.

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".

Spring3HelloWorldTest.java:

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();        

    }
}

Spring3HelloWorld.java:

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!!!";
    }
}

SpringHelloWorld.xml:

<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;

    <context:annotation-config/>

    <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

    <bean id="dao" class="src.DAO" ></bean>
    <bean id="spring3HelloWorldBean" class="src.Spring3HelloWorld" ></bean>

</beans>
+3  A: 

My first guess is you won't get any of the advanced behaviour with Spring and annotations because you are using an XmlBeanFactory instead of the recommended ApplicationContext.

-- edit --

Yup - see this Stack Overflow question/answer.

hbunny
Ah I wasn't aware of that. Thanks, I'll definitely try with ApplicationContext instead.
FromCanada
+1. And in `BeanFactory` `@Required` can be enabled by adding the postprocessor manually: `beanFactory.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor());`
axtavt
Thanks again. I tested both methods and they each worked.
FromCanada
+1 good spot...
skaffman