Spring Plugin

VRaptor2 offers a spring plugin which allows you to inject Spring beans into your logic classes.

This example is avaible for download. Please check the download page.

Constructor Injection x Setters

Important note: Only constructor injection is supported right now. We do not offer setter injection as it breaks the "Good Citizen" pattern. For more info read this document which in part 3 shows why setter injection might leave your object in an "invalid" state, checking for nulls in each and every method.

As soon as you move to constructor injection, you will notice that your test cases are much cleaner.

vraptor.xml

Open your vraptor.xml file and register the plugin:

<vraptor>
        ...
        <plugin>org.vraptor.plugin.spring.SpringPlugin</plugin>
        ...
</vraptor>

Default settings and how to override them

By default, VRaptor will search for a file named applicationContext.xml in the directory /WEB-INF/classes of your project. It is possible to change this behaviour by passing the filename as a property to the plugin element:

<vraptor>
        ...
        <plugin type="org.vraptor.plugin.spring.SpringPlugin">
                <property name="configFile">/WEB-INF/another-spring-context.xml</property>
        </plugin>
        ....
</vraptor>

Note that now the plugin class is specified in the type attribute.

Accessing the Spring Context from anywhere

Sometimes you may want to have access to the Spring context to, say, manually retrieve a bean. This is specially good when developing JSP Custom Libraries, for example. By default, VRaptor will add the Spring instance to the ServletContext in a key named springContext. To retrieve it, just do something like

...
ApplicationContext springContext = (ApplicationContext)servletContext.getAttribute("springContext");
MyBean bean = (MyBean)springContext.getBean("id.of.the.bean");
...

In this example, servletContext is any instance from the ServletContext you have access to.

Also, it is possible to specify a custom name for the context key:

<vraptor>
        ...
        <plugin type="org.vraptor.plugin.spring.SpringPlugin">
                <property name="contextName">springApplicationContext</property>
        </plugin>
        ....
</vraptor>

Spring bean class

Let's write a simple class which we're going to use as Spring bean.

This example creates a user and offers a getter to access this user:

public class SystemUser {

        private final User user;

        public SystemUser() {
                //create a user
                this.user = new User();
                this.user.setName("Nico Steppat");
                this.user.setLogin("nico");
        }

        public User getUser() {
                return user;
        }
}

Spring's configuration file

In order to use Spring beans you must register them in the /WEB-INF/classes/applicationContext.xml in the default configuration, or in the file you defined in the plugin configuration.

Put this file within your WEB-INF classes by default:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

        <!-- Spring bean -->
        <bean id="org.vraptor.spring.model.SystemUser" class="org.vraptor.spring.model.SystemUser"/>

</beans>

Spring bean injection

Now we are ready to inject the Spring bean into a logic class.

Let's inject the SystemUser into the following logic class:

@Component("user")
public class HomeComponent {

        private final SystemUser user;

        /**
         * Injection using Spring!
         *
         * The system user gets injected automatically ....
         * @param user
         */
        public HomeComponent(SystemUser user) {
                this.user = user;
        }

        /**
         * URL: user.home.login
         * View: user.home.ok (user/home.ok.jsp)
         *
         * Just an empty logic method to call the view.
         */
        public void home() {
        }

        /**
         * Eject the user.
         *
         * @return
         */
        public User getUser() {
                return user.getUser();
        }

}

Finished. To summarize, you have to do three things:

  1. Register the plugin in the vraptor.xml file
  2. Register your bean in the spring configuration file (/WEB-INF/classes/applicationContext.xml or one you specify in the plugin's configuration)
  3. Create your logic class and use your bean through constructor injection