javawaveblogs-20

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

Monday, November 26, 2012

Restful webservice using CFX and Spring




1. Create a new Maven Web project using Netbeans IDE. (Version used for this post is 7.3 beta3)



2. Run through the wizard to create the Maven Web Application. here i have given the name "CFXSpring" as the project name.


3. In the next screen of the wizard select the server of your choice.  Here i have selected Glassfish server which comes bundled with Netbeans.


4. Then click on finish, your project is ready to use. Here you can notice the following folder structure is created by Maven for you.


5. Now its time to add Spring MVC Framework to our application. So right click on the project node in project window and go to properties menu item. IDE will pop up a properties window for you. In that window go to "Frameworks" categories.  In the right panel of the window you will get a empty text area with an "Add" button to its right. Just Click on the Add button.Another framework window will pop up with the list of frameworks works available to choose from.  Select Spring Web MVC from that window.  See the screen shot below for clarity.



6. Now select the Spring Framework version you like to use with this application.  Here i have selected 2.5.6 to use with my application.  see the figure below.


7. Once the above step is done.  Spring frame work will be added to the project by the IDE, by including the needed dependencies to the POM.XML file. By the way adding the dependencies to the project dependencies folder. see the figure below.


8. Now you can see there is a change in the project folder structure also.  A new folder "WEB-INF" is added to the project. That folder will also contain "applicationContext.xml" which is nothing but the Spring Configuration file where we will be wiring our beans and bean dependencies through property or constrructer injection. also you can see "web.xml" which is nothing but the web applications deployment descriptor.

9. In "web.xml" you can see already "org.springframework.web.servlet.DispatcherServlet" is configured for you by the IDE. so no need for us to do any extra configuration with respect to spring.

10. Now its time to add CFX dependencies to POM.XML.  This we will do manually with out using any of the IDE's wizards.

11. Open the "pom.xml" file which will be located in the projectfiles folder of the project.

12. Go to "dependencies" tag. and add the following dependency to it.

<dependency>             <groupId>org.apache.cxf</groupId>             <artifactId>cxf-rt-frontend-jaxrs</artifactId>             <version>2.6.2</version>         </dependency>

Monday, December 3, 2007

Spring - Part I

Here is the simple example to start up with spring,

To write a simple spring application we need one interface, one implementation of that interface and a test client to test the implementation. Apart form that we also need a configuration XML file.

One interface --> Hello.java
One implementation --> HelloImpl.java
One Test Client --> HelloClient.java
One Spring XML File --> Hello.xml (Should be in the classpath).

Now let us see the interface Hello.java.


package com.javawave.spring.cli;

/**
* @author dhanago
*/
public interface Hello
{
/**
* This method will return the salutation for the name passed as input
* param to it.
*
* @param name
* @return
*/
public String sayHello( String name );
}


The above interface has only one method to say hello. This method has to be implemented in the implementation class "HelloImpl.java"


package com.javawave.spring.cli;

/**
* @author dhanago
*/
public class HelloImpl implements Hello
{

private String greet;

/**
* zero-arg constructor
*/
public HelloImpl()
{

}

/**
* @param greet
*/
public HelloImpl( String greet )
{
this.greet = greet;
}

/*
* (non-Javadoc)
*
* @see com.javawave.spring.cli.Hello#sayHello(java.lang.String)
*/
public String sayHello( String name )
{

return this.greet + name;
}

/**
* @param greet the greet to set
*/
public void setGreet( String greet )
{
this.greet = greet;
}

}

The Impl class has a property called greet. This also has a setter method "setGreet" to set the value for this property.

Now let us see the spring xml. This XML file will be used for creating the objects in spring framework. This XML is also called wiring xml. Through this XML we will set the greet property of the HelloImpl class.

Hello.XML
~~~~~~~~~


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC
"-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="hello"
class="com.javawave.spring.cli.HelloImpl">
<property name="greet">
<value>Good Morning!...</value>
</property>
</bean>
</beans>

Here in the XML you can see the tag called bean which will map to the bean what you are using in the application. It has an attribute called "id" and a attribute called "class". The "class" attribute maps to the HelloImpl.java in our case. The property tag inside the bean tag maps to the property inside the bean class. Here in our case it is the "greet" property. The value for the property is given in the value element inside the property element. This is the value passed to the bean's property "greet" . This is how spring does the dependency injection (DI). Here what you are doing is injecting dependency through setter method. This is also called Setter Injection.

Now we will see the client application "HelloClient.java" to test the "HelloImpl.java".

/**
*
*/
package com.javawave.spring.cli;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
* @author dhanago
*/
public class HelloClient
{

/**
* @param args
*/
public static void main( String[] args )
{
try
{
System.out.println( "Inside main of HelloClient.." );
Resource resource = new ClassPathResource(
"com/javawave/spring/cli/Hello.xml" );
BeanFactory factory = new XmlBeanFactory( resource );
Hello hello = (Hello) factory.getBean( "hello" );
String result = hello.sayHello( "Man" );
System.out.println( result );
}
catch (Exception e)
{
System.out.println( "Exception/Error:" + e.toString() );
}
}
}

This client application uses ClassPathResource to load the resource. In our case Hello.xml and it creates the factory via XmlBeanFactory. factory class has getBean method which will return a object. Here we get Hello object. So from that we can call sayHello method as shown in the above code and get the result.

The Jars used for this application is:
spring.jar
commons-logging.jar

The Output is shown below:

Inside main of HelloClient..
Good Morning!...Man
Dec 3, 2007 1:59:01 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [com/javawave/spring/cli/Hello.xml]

diggthis