Posts

Spring 3 Prart 4: Lifecycle Management

Image
This is the 4th part of the Spring 3 Series . Regardless of bean scope, initialization lifecycle callback methods are called on objects in Spring. There are 3 ways to achieve this interface based method based annotation based Depend on your application requirements, you need to evaluate which mechanism is best. To achieve more portability third is the best because it is based on the JSR-250. If you are not concern of portability and in an application and needs lifecycle notification for many beans of the same type, then best is interface mechanism. When you share the bean among multiple spring projects , then interface mechanism is the best because interface mechanism based beans are self-contained. Above diagram shows a high-level overview of how Spring manages the life cycle of the beans within its container(Harrop, Rob; Ho, Clarence (2012-04-18). Pro Spring 3 (Professional Apress) (p. 115). Apress. Kindle Edition). It is important to remember that initializat...

Spring 3 part 3: Standard wiring

This is the 3rd part of the Spring 3 Series . In the part 2 of this series explained the Spring way of wiring. The part 2 examples are continued in this blog.mThis part is dedicated to standard wiring that is proposed by the Java Community Process. Spring 3 implements the JSR-330 that is @Inject annotation. The @Inject is lot common to the @Autowire but no “required” attribute. import javax.inject.Inject; import javax.inject.Named; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Parent { @Inject @Named("grandChild") private Child child; The Parent class’s child property is annotated with @Inject and @Named. Here @Named is similar to @Qualifier, for instance grandChild is declared as a Spring bean in the spring-config.xml file which is referenced in the @Named annotation. It is also possible to create custom qualifier as an alternative way. Here the custom qual...

Spring 3 part 2: Auto wiring

Image
This is the 2nd part of the Spring 3 Series . In the first part of this series, I discussed how to create maven Spring 3 project.The objective of the second part is to discuss the Spring 3 auto-wiring. Fist look into the Spring 2.5 auto-wiring which is based on the byName, byType, constructor and autodetect. <?xml version="1.0" encoding="UTF-8"?> <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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- context:annotation-config/--> <bean id="parent" class="au.com.blogspot.ojitha.spring3tut1.Pa...

Spring 3 part 1: Getting started with maven 3

Image
This is the 1st part of the Spring 3 Series . I know most of you know how to create spring project from maven. This blog I write as a part of the completion of series of my blogs on Spring 3. The my way of creating spring project is, first creating a simple java project and enhanced it up to the Spring project as I want. As shown above figure, the maven command will create first-tut project underneath the maven-projects directory. Now you are ready to import existing maven project to STS. I you are typical Eclipse user, then you can use mvn eclipse:eclipse before import. And you need to create global variable M2_REPO in the Eclipse. In STS there is m2Eclipse integration, therefore you can directly import as shown in the following figure Via STS, you can add spring dependencies as shown in the following You can find the following dependencies just typing “org.spring”. This will update the pom.xml file as follows. < project xmlns = " http://maven.apache.org/POM/4.0....

RESTFul Servlets

Image
I already wrote the blog on RESTFull Dynamic service proxy ( have a ). In this blog, address the use of Servlets, because Servlets are best candidates for RESTFul services. The main advantage is HTTPServlet which cover the HTTP communication completely. RESTFul (HTTP method) HTTPServlet GET doGet() POST doPost() PUT doPut() DELETE doDelete() In this blog, I just use doPost() to store Person object in the server side Map (employees) and get the complete list of people (employees) as a response. All of the implementation from the RESTFull Dynamic service proxy blog, except the PersonService, which is replaced by the PersonServlet.java as shown in the following listing. package com.blogspot.ojitha.wsex7.web; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.servlet.ServletException; import javax.servlet...

RESTFull Dynamic service proxy

Image
A RESTFull provider implements the method public Source invoke(Source request) in the server side and the twin Dispatch object in the client side.  This way of handling the RESTFull is not easy. The best way is to use of JAX-RS as shown in my previous blog entries JAX-RS: 1 min RESTFul with JAXB   and  JAX-RS Tutorial: 1 min RESTFul service .   As shown in the above figure, The person is JavaBean with JAXB xml root as shown in the following listing. As well it compose an Address object for each and every Person. package com.blogspot.ojitha.wsex6.person; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import com.blogspot.ojitha.wsex6.common.Address; @XmlRootElement @XmlType(propOrder={"id", "firstName", "lastName", "age","address"}, namespace="com.blogspot.ojitha.wsex6.person") public class Person { private String firstName; private String lastN...

JAX-RS: 1 min RESTFul with JAXB

Image
This is second post of the JAX-RS Tutorial: 1 min RESTFul service post extended to use JAXB. Follow the first tutorial to understand the Grizzly framework usage. I’ve created two classes Person and the Address as shown in the above class diagram. package com.blogspot.ojitha.rest.example.first; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Person { private String firstName; private String lastName; private Long id; private Address address; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); return result; ...