Posts

Showing posts with the label Spring

Using Maven Dependency Tree in troubleshooting

In this blog post, I'm going to explain how to check the  maven dependency tree and how it is useful in troubleshooting certain run time exception which we might encounter while running Spring applications. Maven Dependency  resolution: When you use maven, it uses its own dependency resolution mechanism to decide which jar to use when there is a conflict. Lets say you are using two dependencies, each of which have the same jar. Now, unless you pay attention, maven might end up using the incorrect jar version, and you will start getting exceptions like 'ClassNotFound' or 'NoSuchMethodError'. So, it helps to know which version of jar has maven resolved and is added to our application. This is where the Maven dependency tree helps us. Maven Dependency Tree: A simple command, "mvn dependency:tree -Dverbose " will print out the entire maven dependency tree. I will show with an example how to analyze this tree. Since is based on a issue I re...

Building Webservices using Spring

In this post, I'm going to explain how to write a contract first webservice using Spring. We are going to create a service for creating a person record. We are going to call the service as 'CreatePersonService'. It will accept first name, middle name and last name as parameters and return a id parameter back as a response. We need to first define the XSD representing the person record. Also, we need an XSD to represent the request to create the Person record.  Let us first start with the maven dependencies required: pom.xml: We will be using jaxb for generating classes from xsd. <dependency> <groupId>org.springframework.ws</groupId> <artifactId>spring-ws-core</artifactId> <version>2.1.3.RELEASE</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.dom4j</groupId> <artifactId>dom4j</artif...

Spring Beans Autowiring

In this post I'm going to write about Autowiring feature in Spring. Overview: Generally, in Spring, you will define the beans and inject other beans into their properties by configuring them in xml. For eg, if we have a DAO, UserDAO and a service class UserService, which has a property reference for the DAO, then, the bean configuration would look like this: <bean id="userDAO" class="com.myorg.service.dao.UserDAO"> </bean> <bean id="userSvc" class="com.myorg.service.UserService"> <propertyname="userDAO" ref="userDAO"/> </bean> Spring provides a feature called Autowiring, wherein the decision to wire the bean properties could be left to the Spring framework and this saves us a lot of configuration hassle. There are four types of autowiring: byName byType constructor autodetect 1. Autowiring - byName: In this type of wiring, Spring attempts to find the beans which ha...

Spring AOP - Part III

For part I, refer to: Spring AOP - Part I For part II, refer to: Spring AOP - Part II Continuing further on AOP examples, in this post, we will see how can we pass arguments to the Aspect classes. Passing parameters to Aspect classes: In this example, we will write a very basic auditing service, which will store audit details of who accessed a particular service. The audit details could be stored in database. But that part is not shown here to keep the details simple. Let us say we have an Account service, which offers CRUD on the account. The username parameter which is the first parameter for all the methods, indicates the user who will perform a CRUD on the account. public interface AccountService { public long createAccount(String username, Account account); public void updateAccount(String username, Account account); public void deleteAccount(String username, Account account); } Define a corresponding AccountServiceImpl which will actually perform t...

Spring AOP - Part II

For Part -I, refer blog: Spring AOP-Part I Let me give a simple example of AOP to add logging service. Pom.xml <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.11</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.6.8</version> </dependency> <!-- logging --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> <version>1.7.5</ver...

Spring AOP - Part I

In this blog I'm going to write about AOP using Spring.  Overview: AOP which stands for Aspect Oriented Programming aims at separating out cross cutting concerns from the normal programming logic. This makes way for cleaner application code can focus on business logic and the other concerns like logging, security etc will be handled by the aspects. Illustration: To give a perspective of how AOP works, let us consider the following example: Let us say, in a big city, there are no proper sign boards. So the city Mayor decides to take up a project to install sign boards. This project is like a cross cutting concern in some way.  The city corporation will define certain rules to install the sign boards.  Sign board rules: A sign board indicating 'school nearby' should be placed 5 meters from the school in both directions.  A sign board indicating a sharp curve should be placed 5 meters before the curve.  A sign board 'Thank you. Visit again...

Spring Webflow Code example - Part IV

Image
For previous parts: For Part-I click on:  Spring Webflow Code example - Part I For Part-II click on:  Spring Webflow Code example - Part II For Part-II click on:  Spring Webflow Code example - Part  III Lets create the third page which is to provide a complementary drinks, if the user has selected 'Indian Spicy Pizza'.  Now since this page needs to be shown conditionally, we need to make use of 'action-state' element. Using this, we can evaluate a condition and then make the transition accordingly. In the view-state for 'selectPizza', insert the transition for 'next' to a state 'complementaryDrinks'. This will actually be a action state as shown below. Note the action state 'complementaryDrinks' will invoke a method hasComplementaryDrinks() to determine the transition. Modify the pizza-order-flow.xml  as below: <view-state id="selectPizza" model="viewScope.newpizza"> <on-render> ...

Spring Webflow Code example - Part III

Image
For Part-I click on:  Spring Webflow Code example - Part I For Part-II click on:  Spring Webflow Code example - Part II Let's create the second page 'Select Pizza'. In this page, we will show a drop down of available pizzas. This page will have buttons 'Add', 'clear' in addition to navigation buttons 'Next', 'Previous', 'Cancel'. Once the user has selected a pizza, the user can click on 'Add' button to add the pizza to the user's cart.  The user can use the 'Clear' button to clear the cart, if he wants to start all over again. Controller changes: In order to initialize the collection for this drop down, we will create a new method 'initPizzaList' in the controller. We also need a method to initialize the Pizza object which will be the model for the jsp. Further, we need a controller method 'addPizza' to update the Order model with the pizzas added. We also need a 'clearPizzas' ...

Spring Webflow Code example - Part II

Image
For Part-I click on: Spring Webflow Code example - Part I In order to invoke the webflow, let's create a link 'Create Order' in index.jsp. The link would refer to the path of the webflow xml. index.jsp <a href="http://www.blogger.com/webflowsample/pizza-order-flow.htm">Create Order</a> Let's try to create the first Page "Customer details". Before landing on this page, we need to instantiate the model objects in controller. We will be adding new method to the controller for this. Validation: In the customer details page, let us try to validate 'First Name' for  mandatory field. Let us create a method to validate the customer in the controller. The method should follow a naming convention 'validateXXX' where XXX refers to the field name. Changes to controller: @Controller public class PizzaOrderController { public Order createOrder(){ Order order = new Order(); ...

Spring Webflow Code example - Part I

This blog shows how to develop a Spring Webflow application having integration with Spring MVC. Spring WebFlow allows us to create a UI workflow with a series of page navigation and abiltity to navigate back and forth. It also provides a context to work with in addition to many other features. Overview: We will be creating a flow to order pizza with the following navigation pages.  The first page - Enter customer details The second page - Select pizza Review Order & submit Order confirmation Now, in the second page, you get a few options to select the pizza you want. If the customer selects 'Indian Spicy Pizza', then we will offer free coke (The pizza provider is kind enough to provide some relief to customers after eating spicy stuff. :)  ) So, after the second page, if the customer selects 'Indian Spicy Pizza', then a page showing complimentary coke is displayed. This is conditional navigation. After submitting the order in the third page,...