Posts

Spring 3 Part 7: Spring with Databases

Image
This is the 7 th part of the Spring 3 Series . Create MySQL 5.6 database ‘payroll’, CREATE DATABASE payroll; Next create a user for example, user ‘ojitha’ CREATE USER 'ojitha'@'localhost' IDENTIFIED BY 'ojitha'; Need to grant the ‘payroll’ access to user ‘ojitha’ as shown in the following. GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP ON payroll.* TO 'ojitha'@'localhost'; You can download freeware of TOAD for MySQL from here . Let create a first table, that contains all the states in Australia. You can create a table using visually using the icon given directly below the Tables tab in the Object Explorer as shown in the above diagram. CREATE TABLE payroll.STATE ( CODE CHAR(3) ASCII COMMENT 'State code', STATE VARCHAR(30), PRIMARY KEY (CODE) ) ENGINE = InnoDB COMMENT = 'Australian states' ROW_FORMAT = DEFAULT; In the my sql console you can the newly added table using the following command SHOW ...

How To: GitHub projects in Spring Tool Suite

Image
The first step is sign up in the GitHub and create a  new project. For example I’ve created a new project Spring3part7 in the GitHub. Before add the projects you need to configure STS for GitHub access. For example, you need to add configuration as shown in the following picture Git local repository also important Now you are ready to pull the project from the GitHub, in the STS import menu select the following Now type the project name and click the ‘Search’ as shown in the following. However, when you select the found project click ‘Finish’ nothing will change in the STS interface. Now you are going to create real project. Here I am going to create simple utility project. This template project need to be created inside the imported GitHub project You have to give the same project name of the GitHub project as shown in the following project Now your project is ready to push. Before that you need to add ignore flag to all the folders and files except pom...

Spring 3 part 6: Spring AOP

Image
This is the 6th part of the Spring 3 Series . AOP Concepts Joinpoint : Joinpoint is the point in an application at which additional logic can be inserted. For example method invocation that is the only Spring support joinpoint type. Advice : Advice is executed at a particular joinpoint in an application. NOTE: no advices for final classes. All the advisors need to implement the Advisor interface. There are two kind of advisors available in the Spring: IntrodcutionAdvisor and PointcutAdvisor. Spring defines 6 difference advices: before(MethodBeforeAdvice),  after returning (AfterReturningAdvice), after(AfterAdvice), around (MethodInterceptor), Throws (ThrowsAdvice), and Introduction (IntroductionInterceptor). Pointcuts : A pointcut is a collection of joinpoints that define when advice should be executed. For example, collection of all method invocations in a particular class. Aspect : An aspect is the combination of advice and pointcuts. This combination immerge t...

Spring 3 Part 5: Factory Beans

Image
This is the 5th part of the Spring 3 Series . This is the solution to create instances which are not possible to crate via “new” operator. It is very important to note that, Spring creates transactional proxies using this method. Most of the time lazy initialization is the tactic of delaying the creation of object. Singleton Pattern is the way to lazy initialization, in turn Factory patterns (both Abstract Factory and Factory Method ) are the way to inject an lazy instance to the Spring. Use the factory method to get an instance Use the lazy initialization to delay instantiation until first time request Store the instance for future delivery when request Inject the instance using Spring Factory Bean Here the class diagram for the example I am going to explain As shown in the class diagram, there are two concrete classes BlackDuck and ReadHeadDuck for the Duck (that is interface). Here the BlackDuck package com.blogspot.ojitha.spring3.part5.example.factory...

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...