Showing posts with label spring. Show all posts
Showing posts with label spring. Show all posts

Thursday, March 4, 2010

Spring Roo with NetBeans

I am a NetBeans user and there are no reason to change the IDE for any tool as NetBeans try to accommodate most of the frameworks and tools. However most of the modern frameworks/tools has easy integration with the Eclipse, Spring Roo is latest in the list. I started evaluating Spring Roo for one of our internal small test application.

Due to the fact that Spring Roo integrated already with Spring Tool Suite (STS) they support using STS IDE for all development. After few here and there i could finally run as well as modify the Spring Roo application in NetBeans IDE.

Maven is a compulsory requirement (until the Ant/Ivy integration is available) for Spring Roo to run, so make sure Maven is configured and NetBeans is referring to the proper Maven repository.

1. Create the project from  the Roo Shell and fill the required artifacts for the basic startup.






2. Do perform:eclipse to generate the Eclipse artifacts and open the project in STS

3. Right click on the project and go to the Refactoring, select Push All option. A dialog box will show with number of files, select all the files and click on Ok. This will convert all the AspectJ code (.aj) to pure Java code. This is the point for which Spring Roo is considered to be non-intrusive and you can actually detach the Spring Roo from the application altogether.



4. Open the created application in NetBeans through Import Eclipse Project/Workspace option. Do clean and build and go for Run application. If not configured anything NetBeans will ask for the Web Server which can be selected to installed Apache Server.




Still there are many things in Spring Roo you can not do in NetBeans but for all those things, again one can go back to the command prompt or to the STS for required changes, refactor again and refresh the NetBeans project to take the new changes.

Thursday, February 18, 2010

Vaadin-Spring Integration

I remember the time when i was trying to integrate Spring with Struts2 and GWT applications. The problem was rather documenting the integration stuff i tried to remember them and as usual tend to forgot them. So this time have decided to document the Spring integration with Vaadin.

  • web.xml changes
So first we need to add the Spring Context listener to load the bean configuration file.

<listener>
  <listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>

Add the Spring Servlet to intercept Vaadin requests

<servlet>
  <servlet-name>VaadinSpringServlet</servlet-name>
  <servlet-class>com.dhaval.web.vaadin.spring.servlet.SpringApplicationServlet</servlet-class>
  <init-param>
    <param-name>applicationBean</param-name>
    <param-value>testApplication</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>VaadinSpringServlet</servlet-name>
  <url-pattern>/vaadin/*</url-pattern>
</context-param>
  • Spring Servlet
This servlet connects the Spring with Vaadin. The servlet loads the Vaadin Application class and dispatches all the HTTP requests/responses. The Servlet is available in the Simple Application provided by Vaadin Team for Spring integration.
  • Spring bean configuration
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property>name="driverClassName" value="${db.driver}"/>
  <property>name="url" value="${db.url}"/>
  <property>name="username" value="${db.user}"/>
  <property>name="password" value="${db.pass}"/>    
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property>name="dataSource" ref="dataSource"/>
</bean>

<bean id="dao.user" class="com.dhaval.web.vaadin.spring.dao.UserDaoImpl">
  <constructor-arg>ref="jdbcTemplate"/>
</bean>

<bean id="testApplication" class="com.dhaval.web.vaadin.spring.MyApplication">
  <constructor-arg>ref="dao.user"/>
</bean>

testApplication bean id is same as we have given in the web.xml as an init-param to Spring Servlet.
  • Vaadin Main Application class
This is my Vaadin Application class

public class MyApplication extends Application{
    private UserDaoImpl userDao;

    public MyApplication(UserDaoImpl userDao){
        this.userDao = userDao;
    }

    public void init() {
        Window mainWindow = new Window("Simple Vaadin Spring Integration");

        final TextField txtUser = new TextField("Enter username");
        Button btnCheckUser = new Button("Check Username");
        final Label lblCheckMsg = new Label("");

        btnCheckUser.addListener(new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                boolean result = userDao.hasUser(txtUser.getValue().toString());
                if(result){
                    lblCheckMsg.setValue("Available");
                }else{
                    lblCheckMsg.setValue("Not Available");
                }
            }
        });

        mainWindow.addComponent(txtUser);
        mainWindow.addComponent(btnCheckUser);
        mainWindow.addComponent(lblCheckMsg);

        setMainWindow(mainWindow);
   }
}

The full source code is available at vaadin-spring-integration in download section.