SlideShare a Scribd company logo
Beginning of Spring Part - 1 Author:  Santosh Kumar Kar santosh.bsil@yahoo.co.in
Who This Tutor Is For? This tutor is for them who just start learning Springs. After going through this session, you will have the basic understandings of Spring Framework. You will able to write small applications using Spring. Also you can understand other complicated Spring projects after going through this session. I would suggest you to refer other good Spring books and tutors to understand Springs in depth. Remember, you can understand only the basics of Springs through this tutor which will be helpful to go depth into Springs.  You can also download the source code of the examples from Download Links available at http://springs-download.blogspot.com/ Good Luck… Santosh
Introduction: In many books or sites, you will find about Spring that Spring supports inversion of control (IOC) and that it is a lightweight framework. You might be confused, what is inversion of control (IOC)? What is the purpose of using  IOC? Why this feature is desirable?
Course Contents ,[object Object]
Understanding ApplicationContextand method getBean("<BEAN NAME>")
ApplicationContext in Different Applications/java Frameworks
new FileSystemXmlApplicationContext
Servlet and Springs
Using new ClassPathXmlApplicationContext
Using new XmlWebApplicationContext
Using WebApplicationContextUtils.getWebApplicationContext(ServletContext),[object Object]
Problem:  There is a well known car racing computer game –NeedForSpeed. I have taken this example because most of us had played this game. You can Google if you want to know about this game ;)… In this game, there are the participants use varieties of cars, such as Ferrari Jaguar McLaren Ford Each type of car do have their own specifications say grip, accelerator, speed etc.  Now let’s write a program for this game.  There will be 1 generic class having common behavior. All the above different cars must inherit the common behavior of this generic class. We will name the generic class as:- NeedForSpeedCar which should be an interface. Other car classes such as Ferrari, Jaguar, McLaren, Fordetc. must implement that interface.
The generic class which contains the declaration of the common behavior of all the cars. This is NeedForSpeedCar.java //NeedForSpeedCar.java package spring.test.santosh.game;   public interface NeedForSpeedCar{ String startEngine();	 String accelerate(); } Now we do have variety of cars. The specification of Ferrari is different from Jaguar and McLaren. Similarly McLaren specification is different from other cars. So each variety of car do have it's own implementaton. So let's see the implementation of those cars.  
//Ferrari.java package spring.test.santosh.game;   public class Ferrari implements NeedForSpeedCar{ 	public String startEngine(){ return ("Start engine of your Ferrari Car"); 	} 	public String accelerate(){ return ("Accelerate and run fast your Ferrari Car"); }  }  //Jaguar.java package spring.test.santosh.game;   public class Jaguar implements NeedForSpeedCar{ 	public String startEngine(){ return ("Start engine of your Jaguar Car"); 	} 	public String accelerate(){ return ("Accelerate and run fast your Jaguar Car"); }  }
//McLaren.java package spring.test.santosh.game;   public class McLaren implements NeedForSpeedCar{ 	public String startEngine(){ return ("Start engine of your McLaren Car"); 	} 	public String accelerate(){ return ("Accelerate and run fast your McLaren Car"); }  }  //Ford.java package spring.test.santosh.game;   public class Ford implements NeedForSpeedCar{ 	public String startEngine(){ return ("Start engine of your Ford Car"); 	} 	public String accelerate(){ return ("Accelerate and run fast your Ford Car"); }  }
The Race is going to be started. So let’s write that class… //Race.java package spring.test.santosh.game;   public class Race{  	public void startRace(){ //You choose your car-Ferrari NeedForSpeedCarmyracingcar= new Ferrari(); System.out.println(myracingcar.startEngine()); System.out.println(myracingcar.accelerate()); 	} } class Race has a dependency to class Ferrari The class Race has a dependency to class Ferrari. Now the car modules are ready. The race is ready. Let’s ask the Participant to start the race.  The participant is ready to use Ferrari.
// Participant.java package spring.test.santosh.game; public class Participant{  public static void main(){ //You choose your car-Ferrari Racerace = new Race(); race.startRace(); 	} } Compile all the programs. And run Participant. The output would be: Now it looks pretty good. The Participant run the car Ferrari. See the next slide for what is the problem here…………………………………………
The participant wants to use the another car: McLaren McLaren is best for grip so he chosed this car for the race. But hey… Ohhhhnooooooooooo....  It's not really pretty good because of the Problem here is:the object of new Ferrari is hard coded. So to use McLaren, it should be: NeedForSpeedCarmyracingcar  =  new Ferrari(); NeedForSpeedCarmyracingcar  =  new McLaren(); So at any instance the participant wants to choose another car, it needs the code change in Race.java.  If the participant wants to change to Fordfrom McLaren, then it must be: NeedForSpeedCarmyracingcar  =  new McLaren(); NeedForSpeedCarmyracingcar  =  new Ford();
Solution:  So, what do you think, what could be the solution???? Can you change the program as per the participant’s requirement? Think a while before going down for the solution… How is it if the object of your racing cars provide from outside but not in the class itself?  That is NeedForSpeedCarmyracingcar= <get from outside> This will be known as the dependency Injection (IOC)   Yes, it's a good approach. But who will create the object and who can provide the dependency injection to the class Race and how?  The answer is the IOC container provided in Sprint framework.  The dependency injection will be provided through  ,[object Object]
asetter MethodSo let’s restructure the classes. You need to follow few steps to work with Spring IOC.
Step 1: Add property for the object you need to inject and write setter method. //Race.java package spring.test.santosh.game;   public class Race{  NeedForSpeedCarmyracingcar;   	public void setMyracingcar(NeedForSpeedCarmyracingcar){ this.myracingcar= myracingcar; 	} 	public void startRace(){ //You don’t create the object of any car type//NeedForSpeedCarmyracingcar = new Ferrari(); System.out.println(myracingcar.startEngine()); System.out.println(myracingcar.accelerate()); 	} } Newly added. Through the setter method the object will be injected into Race class. Now we need Spring Framework to provide the Dependent Injection into the class Race. So to use Spring, download the Spring Framework from http://www.springsource.org/download After you download set the library files into the classpath.
Step 2: Create the Spring Configuration XML file. This will contain the entries for the dependency injection. The object will be referred as a bean. We can give any name for this XML. Here we will name as: SpringConfig.xml <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"       "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>      <bean id="race" class="spring.test.santosh.game.Race">           <property name="myracingcar">                <ref local="ferrari"/>            </property>       </bean>      <bean id="ferrari" class="spring.test.santosh.game.Ferrari" />      <bean id="ford" class="spring.test.santosh.game.Ford" />      <bean id="mclaren" class="spring.test.santosh.game.McLaren" />      <bean id="jaguar" class="spring.test.santosh.game.Jaguar" /> </beans> myracingcar is the property in Race.java. Through the setter method the object is injected into Race.java You can change the ref as per the car chosen by the participant from the below bean id list.  These are the objects injected dynamically. The objects are created by IOC container and injected into Race.java
Step 3: Write the client. It must use the config springconfig.xml. packagespring.test.santosh.race.game;   importorg.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext;   publicclassParticipant{ publicstaticvoid main(String args[]){ ApplicationContextctx = newFileSystemXmlApplicationContext("springconfig.xml"); 		Race race = (Race) ctx.getBean("race"); race.startRace(); 	} } The instance of Ferrari  will be created. Now run Driver and see the output…
Now we will change the springconfig.xml to choose McLaren car instead of Ferrari. We will not touch any of the java file… And we will see the output after that… <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"       "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>      <bean id="race" class="spring.test.santosh.game.Race">           <property name="myracingcar">                <ref local="mclaren"/>            </property>       </bean>      <bean id="ferrari" class="spring.test.santosh.game.Ferrari" />      <bean id="ford" class="spring.test.santosh.game.Ford" />      <bean id="mclaren" class="spring.test.santosh.game.McLaren" />      <bean id="jaguar" class="spring.test.santosh.game.Jaguar" /> </beans> Now ref is changed  to "mclaren" from "ferrari". Output after you runDriver.class
To download the source code project for this example, you can choose the link*: NeedForSpeed.zip (Standalone Application) *athttp://springs-download.blogspot.com
Understanding  ApplicationContextandmethodgetBean("<BEAN NAME>") I believe you now understand what is IOC and how we can use the Dependency Injection in Springs to make java classes/components independent as possible of other java classes. Now let’s see more on the ApplicationContext and the method getBean("...") in  ApplicationContext. ApplicationContextis the central interface to provide configuration for an application in springs. An ApplicationContext provides: ,[object Object]
Bean factory methods for accessing application components.And many more… Now we will see how ApplicationContext is used to load the file resources. Here we are loading the Spring Config file -springconfig.xml
new FileSystemXmlApplicationContext //Partifipant.java public class Participant{ 	public static void main(String args[]){ ApplicationContextctx = new FileSystemXmlApplicationContext("springconfig.xml"); 		Race race = (Race) ctx.getBean("race"); race.startRace(); } } ApplicationContextctx = new FileSystemXmlApplicationContext("springconfig.xml") The Application Context object: ,[object Object]
collects all the bean id’s as defined in springconfig.xml. Say: race, ferrari, ford, McLaren.
saves the instances of each bean class into Map with key as the bean id such as:,[object Object]
ApplicationContextinDifferent Applications/java Frameworks Till now we learned how we configure the Spring IOC (Independent Injection) by configuring the XML configuration file. So in the example you can notice the below line in Participant.java: ApplicationContextctx = newFileSystemXmlApplicationContext("springconfig.xml"); Here we are creating the object of FileSystemXMLApplicationContext by providing the XML name as argument. Our application is a stand-alone application and so simply we could use new FileSystemXMLApplicaitonContext(<XML file path>). Also this class constructor is helpful to create the Spring application context for test harness or unit testing. But there are different applications say Web application such as servlets/jsp's, Enterpriseapplications  such as EJBs with Java Frameworks such as Struts, JSF etc. So in this section we will see how we can configure Springs IOC and how we will get the ApplicationContext in client.
Servlet andSprings I hope you are good in writing the Servlet and JSP application. So here I am not going to put how to build the servlet application and deploy it in the server. Just recall the directory structure in your servlet project: Now we will see where we can store our spring config XML file and how it could be accessible in the servlet classes. Note: It is very important that you don’t be confused this example with Springs MVC. We will see the Spring MVC latter. So now you concentrate how you will get the Spring ApplicationContext object in Servlet.
Achieving Springs Application Context in Servlet You can get the ApplicationContext of Spring in Servlet in 3 ways: Through new ClassPathXmlApplicationContext("<relative path of config XML>") Through new XmlWebApplicationContext() Through WebApplicationContextUtils.getWebApplicationContext(ServletContext);
Using ClassPathXmlApplicationContext The ClassPathXmlApplicationContextconstructor searches the given XML file from the classes folder in WEB-INF folder.  Just you need to follow some basic steps. step 1: Create the spring config XML file.  step 2: Name that file whatever you want. For example, in our example we named as: myspringconfig.xml step 3: Put this xml file in WEB-INF/classes folder while youare deploying. (If you use any editor like Eclipse, just put the XML in src folder, so when you build your project, it will automatically generate the same copy of XML along with the .class files in WEB-INF/classes folder.) Step 4: In servlet you can simply create the object of ClassPathXmlApplicationContextby passing the file relative path in constructor. E.g.  ApplicatonContextctx =  new ClassPathXmlApplicationContext("spring/test/santosh/config/myspringconfig.xml");  Herespring.test.santosh.configis a package created and stored the myspringconfig.xmlin this package. Let’s see the servlet example.
Let’s convert the same stand-alone application into servlet application by very minimum changes.  ,[object Object]

More Related Content

Viewers also liked

Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
Serhat Can
 
Spring ppt
Spring pptSpring ppt
Spring ppt
Mumbai Academisc
 
Paytrail - Miksi verkossa ostaminen on nyt helpompaa kuin koskaan?
Paytrail - Miksi verkossa ostaminen on nyt helpompaa kuin koskaan?Paytrail - Miksi verkossa ostaminen on nyt helpompaa kuin koskaan?
Paytrail - Miksi verkossa ostaminen on nyt helpompaa kuin koskaan?Paytrail
 
Verkkokauppa-Akatemia: Automatisointi verkkokaupan keskiössä
Verkkokauppa-Akatemia: Automatisointi verkkokaupan keskiössäVerkkokauppa-Akatemia: Automatisointi verkkokaupan keskiössä
Verkkokauppa-Akatemia: Automatisointi verkkokaupan keskiössä
Flashnode Ltd.
 
Paytrail maksupalvelu pähkinänkuoressa
Paytrail maksupalvelu pähkinänkuoressa   Paytrail maksupalvelu pähkinänkuoressa
Paytrail maksupalvelu pähkinänkuoressa
Paytrail
 
Kuha on huumoria monikanavaisen asiakaspalvelun johtaminen
Kuha on huumoria   monikanavaisen asiakaspalvelun johtaminenKuha on huumoria   monikanavaisen asiakaspalvelun johtaminen
Kuha on huumoria monikanavaisen asiakaspalvelun johtaminen
Paytrail
 
Digitaalisen markkinoinnin trendikatsaus
Digitaalisen markkinoinnin trendikatsausDigitaalisen markkinoinnin trendikatsaus
Digitaalisen markkinoinnin trendikatsaus
dynamo&son
 
Paytrail - Enemmän kuin maksutavat verkossa
Paytrail - Enemmän kuin maksutavat verkossaPaytrail - Enemmän kuin maksutavat verkossa
Paytrail - Enemmän kuin maksutavat verkossa
Paytrail
 
Paytrail – Näin saat verkkomaksupainikkeet ja kaikki muut maksutavat verkkoka...
Paytrail – Näin saat verkkomaksupainikkeet ja kaikki muut maksutavat verkkoka...Paytrail – Näin saat verkkomaksupainikkeet ja kaikki muut maksutavat verkkoka...
Paytrail – Näin saat verkkomaksupainikkeet ja kaikki muut maksutavat verkkoka...
Paytrail
 
Digitaalinen markkinointi.22.5
Digitaalinen markkinointi.22.5Digitaalinen markkinointi.22.5
Digitaalinen markkinointi.22.5Tuija Marstio
 
Webinar: Spring Framework - Introduction to Spring WebMVC & Spring with BigData
Webinar: Spring Framework - Introduction to Spring WebMVC & Spring with BigData Webinar: Spring Framework - Introduction to Spring WebMVC & Spring with BigData
Webinar: Spring Framework - Introduction to Spring WebMVC & Spring with BigData
Edureka!
 
Java Web Services [4/5]: Java API for XML Web Services
Java Web Services [4/5]: Java API for XML Web ServicesJava Web Services [4/5]: Java API for XML Web Services
Java Web Services [4/5]: Java API for XML Web Services
IMC Institute
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
Ankit Minocha
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
BG Java EE Course
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
kamal kotecha
 
Web Service Presentation
Web Service PresentationWeb Service Presentation
Web Service Presentation
guest0df6b0
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
Christopher Bartling
 
Seminar on cloud computing by Prashant Gupta
Seminar on cloud computing by Prashant GuptaSeminar on cloud computing by Prashant Gupta
Seminar on cloud computing by Prashant Gupta
Prashant Gupta
 

Viewers also liked (18)

Introduction to Spring Framework
Introduction to Spring FrameworkIntroduction to Spring Framework
Introduction to Spring Framework
 
Spring ppt
Spring pptSpring ppt
Spring ppt
 
Paytrail - Miksi verkossa ostaminen on nyt helpompaa kuin koskaan?
Paytrail - Miksi verkossa ostaminen on nyt helpompaa kuin koskaan?Paytrail - Miksi verkossa ostaminen on nyt helpompaa kuin koskaan?
Paytrail - Miksi verkossa ostaminen on nyt helpompaa kuin koskaan?
 
Verkkokauppa-Akatemia: Automatisointi verkkokaupan keskiössä
Verkkokauppa-Akatemia: Automatisointi verkkokaupan keskiössäVerkkokauppa-Akatemia: Automatisointi verkkokaupan keskiössä
Verkkokauppa-Akatemia: Automatisointi verkkokaupan keskiössä
 
Paytrail maksupalvelu pähkinänkuoressa
Paytrail maksupalvelu pähkinänkuoressa   Paytrail maksupalvelu pähkinänkuoressa
Paytrail maksupalvelu pähkinänkuoressa
 
Kuha on huumoria monikanavaisen asiakaspalvelun johtaminen
Kuha on huumoria   monikanavaisen asiakaspalvelun johtaminenKuha on huumoria   monikanavaisen asiakaspalvelun johtaminen
Kuha on huumoria monikanavaisen asiakaspalvelun johtaminen
 
Digitaalisen markkinoinnin trendikatsaus
Digitaalisen markkinoinnin trendikatsausDigitaalisen markkinoinnin trendikatsaus
Digitaalisen markkinoinnin trendikatsaus
 
Paytrail - Enemmän kuin maksutavat verkossa
Paytrail - Enemmän kuin maksutavat verkossaPaytrail - Enemmän kuin maksutavat verkossa
Paytrail - Enemmän kuin maksutavat verkossa
 
Paytrail – Näin saat verkkomaksupainikkeet ja kaikki muut maksutavat verkkoka...
Paytrail – Näin saat verkkomaksupainikkeet ja kaikki muut maksutavat verkkoka...Paytrail – Näin saat verkkomaksupainikkeet ja kaikki muut maksutavat verkkoka...
Paytrail – Näin saat verkkomaksupainikkeet ja kaikki muut maksutavat verkkoka...
 
Digitaalinen markkinointi.22.5
Digitaalinen markkinointi.22.5Digitaalinen markkinointi.22.5
Digitaalinen markkinointi.22.5
 
Webinar: Spring Framework - Introduction to Spring WebMVC & Spring with BigData
Webinar: Spring Framework - Introduction to Spring WebMVC & Spring with BigData Webinar: Spring Framework - Introduction to Spring WebMVC & Spring with BigData
Webinar: Spring Framework - Introduction to Spring WebMVC & Spring with BigData
 
Java Web Services [4/5]: Java API for XML Web Services
Java Web Services [4/5]: Java API for XML Web ServicesJava Web Services [4/5]: Java API for XML Web Services
Java Web Services [4/5]: Java API for XML Web Services
 
1 java servlets and jsp
1   java servlets and jsp1   java servlets and jsp
1 java servlets and jsp
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Java servlet life cycle - methods ppt
Java servlet life cycle - methods pptJava servlet life cycle - methods ppt
Java servlet life cycle - methods ppt
 
Web Service Presentation
Web Service PresentationWeb Service Presentation
Web Service Presentation
 
RESTful Web Services
RESTful Web ServicesRESTful Web Services
RESTful Web Services
 
Seminar on cloud computing by Prashant Gupta
Seminar on cloud computing by Prashant GuptaSeminar on cloud computing by Prashant Gupta
Seminar on cloud computing by Prashant Gupta
 

Similar to Springs

Di code steps
Di code stepsDi code steps
Di code steps
Brian Kiptoo
 
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docxBroncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
curwenmichaela
 
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docxBroncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
hartrobert670
 
Public class race track {public static void main(string[] args
Public class race track {public static void main(string[] argsPublic class race track {public static void main(string[] args
Public class race track {public static void main(string[] args
YASHU40
 
Goal The goal of this assignment is to help students understand the.pdf
Goal The goal of this assignment is to help students understand the.pdfGoal The goal of this assignment is to help students understand the.pdf
Goal The goal of this assignment is to help students understand the.pdf
arsmobiles
 
4.Spring IoC&DI(Spring Ioc실습_어노테이션 기반)
4.Spring IoC&DI(Spring Ioc실습_어노테이션 기반)4.Spring IoC&DI(Spring Ioc실습_어노테이션 기반)
4.Spring IoC&DI(Spring Ioc실습_어노테이션 기반)
탑크리에듀(구로디지털단지역3번출구 2분거리)
 
(5) c sharp introduction_object_orientation_part_ii
(5) c sharp introduction_object_orientation_part_ii(5) c sharp introduction_object_orientation_part_ii
(5) c sharp introduction_object_orientation_part_ii
Nico Ludwig
 
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
Matt Raible
 
Going web native
Going web nativeGoing web native
Going web native
Marcus Hellberg
 
Spring boot jpa
Spring boot jpaSpring boot jpa
Spring boot jpa
Hamid Ghorbani
 
L9
L9L9
L9
lksoo
 
Neoito — Design patterns and depenedency injection
Neoito — Design patterns and depenedency injectionNeoito — Design patterns and depenedency injection
Neoito — Design patterns and depenedency injection
Neoito
 
Force.com migration utility
Force.com migration utilityForce.com migration utility
Force.com migration utilityAmit Sharma
 
User and group security migration
User and group security migrationUser and group security migration
User and group security migrationAmit Sharma
 
Demystifying Maven
Demystifying MavenDemystifying Maven
Demystifying Maven
Mike Desjardins
 
Force.com migration utility
Force.com migration utilityForce.com migration utility
Force.com migration utilityAmit Sharma
 
Things to consider for testable Code
Things to consider for testable CodeThings to consider for testable Code
Things to consider for testable Code
Frank Kleine
 
Angular components
Angular componentsAngular components
Angular components
Sultan Ahmed
 
J Unit
J UnitJ Unit

Similar to Springs (20)

Di code steps
Di code stepsDi code steps
Di code steps
 
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docxBroncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
 
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docxBroncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
Broncosbuild.xmlBuilds, tests, and runs the project Broncos..docx
 
Public class race track {public static void main(string[] args
Public class race track {public static void main(string[] argsPublic class race track {public static void main(string[] args
Public class race track {public static void main(string[] args
 
Goal The goal of this assignment is to help students understand the.pdf
Goal The goal of this assignment is to help students understand the.pdfGoal The goal of this assignment is to help students understand the.pdf
Goal The goal of this assignment is to help students understand the.pdf
 
4.Spring IoC&DI(Spring Ioc실습_어노테이션 기반)
4.Spring IoC&DI(Spring Ioc실습_어노테이션 기반)4.Spring IoC&DI(Spring Ioc실습_어노테이션 기반)
4.Spring IoC&DI(Spring Ioc실습_어노테이션 기반)
 
Story ofcorespring infodeck
Story ofcorespring infodeckStory ofcorespring infodeck
Story ofcorespring infodeck
 
(5) c sharp introduction_object_orientation_part_ii
(5) c sharp introduction_object_orientation_part_ii(5) c sharp introduction_object_orientation_part_ii
(5) c sharp introduction_object_orientation_part_ii
 
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
Comparing Hot JavaScript Frameworks: AngularJS, Ember.js and React.js - Sprin...
 
Going web native
Going web nativeGoing web native
Going web native
 
Spring boot jpa
Spring boot jpaSpring boot jpa
Spring boot jpa
 
L9
L9L9
L9
 
Neoito — Design patterns and depenedency injection
Neoito — Design patterns and depenedency injectionNeoito — Design patterns and depenedency injection
Neoito — Design patterns and depenedency injection
 
Force.com migration utility
Force.com migration utilityForce.com migration utility
Force.com migration utility
 
User and group security migration
User and group security migrationUser and group security migration
User and group security migration
 
Demystifying Maven
Demystifying MavenDemystifying Maven
Demystifying Maven
 
Force.com migration utility
Force.com migration utilityForce.com migration utility
Force.com migration utility
 
Things to consider for testable Code
Things to consider for testable CodeThings to consider for testable Code
Things to consider for testable Code
 
Angular components
Angular componentsAngular components
Angular components
 
J Unit
J UnitJ Unit
J Unit
 

More from Santosh Kumar Kar

Smart home arduino
Smart home   arduinoSmart home   arduino
Smart home arduino
Santosh Kumar Kar
 
Operating electrical devices with PIR sensor. No coding, No controller
Operating electrical devices with PIR sensor. No coding, No controllerOperating electrical devices with PIR sensor. No coding, No controller
Operating electrical devices with PIR sensor. No coding, No controller
Santosh Kumar Kar
 
Temperature sensor with raspberry pi
Temperature sensor with raspberry piTemperature sensor with raspberry pi
Temperature sensor with raspberry pi
Santosh Kumar Kar
 
Pir motion sensor with raspberry pi
Pir motion sensor with raspberry piPir motion sensor with raspberry pi
Pir motion sensor with raspberry pi
Santosh Kumar Kar
 
Angular js for Beginnners
Angular js for BeginnnersAngular js for Beginnners
Angular js for Beginnners
Santosh Kumar Kar
 
Raspberry pi complete setup
Raspberry pi complete setupRaspberry pi complete setup
Raspberry pi complete setup
Santosh Kumar Kar
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
Santosh Kumar Kar
 

More from Santosh Kumar Kar (7)

Smart home arduino
Smart home   arduinoSmart home   arduino
Smart home arduino
 
Operating electrical devices with PIR sensor. No coding, No controller
Operating electrical devices with PIR sensor. No coding, No controllerOperating electrical devices with PIR sensor. No coding, No controller
Operating electrical devices with PIR sensor. No coding, No controller
 
Temperature sensor with raspberry pi
Temperature sensor with raspberry piTemperature sensor with raspberry pi
Temperature sensor with raspberry pi
 
Pir motion sensor with raspberry pi
Pir motion sensor with raspberry piPir motion sensor with raspberry pi
Pir motion sensor with raspberry pi
 
Angular js for Beginnners
Angular js for BeginnnersAngular js for Beginnners
Angular js for Beginnners
 
Raspberry pi complete setup
Raspberry pi complete setupRaspberry pi complete setup
Raspberry pi complete setup
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 

Recently uploaded

The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
Steve Thomason
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
rosedainty
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
BhavyaRajput3
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 

Recently uploaded (20)

The Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve ThomasonThe Art Pastor's Guide to Sabbath | Steve Thomason
The Art Pastor's Guide to Sabbath | Steve Thomason
 
Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)Template Jadual Bertugas Kelas (Boleh Edit)
Template Jadual Bertugas Kelas (Boleh Edit)
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCECLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
CLASS 11 CBSE B.St Project AIDS TO TRADE - INSURANCE
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 

Springs

  • 1. Beginning of Spring Part - 1 Author: Santosh Kumar Kar santosh.bsil@yahoo.co.in
  • 2. Who This Tutor Is For? This tutor is for them who just start learning Springs. After going through this session, you will have the basic understandings of Spring Framework. You will able to write small applications using Spring. Also you can understand other complicated Spring projects after going through this session. I would suggest you to refer other good Spring books and tutors to understand Springs in depth. Remember, you can understand only the basics of Springs through this tutor which will be helpful to go depth into Springs. You can also download the source code of the examples from Download Links available at http://springs-download.blogspot.com/ Good Luck… Santosh
  • 3. Introduction: In many books or sites, you will find about Spring that Spring supports inversion of control (IOC) and that it is a lightweight framework. You might be confused, what is inversion of control (IOC)? What is the purpose of using IOC? Why this feature is desirable?
  • 4.
  • 6. ApplicationContext in Different Applications/java Frameworks
  • 11.
  • 12. Problem: There is a well known car racing computer game –NeedForSpeed. I have taken this example because most of us had played this game. You can Google if you want to know about this game ;)… In this game, there are the participants use varieties of cars, such as Ferrari Jaguar McLaren Ford Each type of car do have their own specifications say grip, accelerator, speed etc. Now let’s write a program for this game. There will be 1 generic class having common behavior. All the above different cars must inherit the common behavior of this generic class. We will name the generic class as:- NeedForSpeedCar which should be an interface. Other car classes such as Ferrari, Jaguar, McLaren, Fordetc. must implement that interface.
  • 13. The generic class which contains the declaration of the common behavior of all the cars. This is NeedForSpeedCar.java //NeedForSpeedCar.java package spring.test.santosh.game;   public interface NeedForSpeedCar{ String startEngine(); String accelerate(); } Now we do have variety of cars. The specification of Ferrari is different from Jaguar and McLaren. Similarly McLaren specification is different from other cars. So each variety of car do have it's own implementaton. So let's see the implementation of those cars.  
  • 14. //Ferrari.java package spring.test.santosh.game;   public class Ferrari implements NeedForSpeedCar{ public String startEngine(){ return ("Start engine of your Ferrari Car"); } public String accelerate(){ return ("Accelerate and run fast your Ferrari Car"); }  }  //Jaguar.java package spring.test.santosh.game;   public class Jaguar implements NeedForSpeedCar{ public String startEngine(){ return ("Start engine of your Jaguar Car"); } public String accelerate(){ return ("Accelerate and run fast your Jaguar Car"); }  }
  • 15. //McLaren.java package spring.test.santosh.game;   public class McLaren implements NeedForSpeedCar{ public String startEngine(){ return ("Start engine of your McLaren Car"); } public String accelerate(){ return ("Accelerate and run fast your McLaren Car"); }  }  //Ford.java package spring.test.santosh.game;   public class Ford implements NeedForSpeedCar{ public String startEngine(){ return ("Start engine of your Ford Car"); } public String accelerate(){ return ("Accelerate and run fast your Ford Car"); }  }
  • 16. The Race is going to be started. So let’s write that class… //Race.java package spring.test.santosh.game;   public class Race{  public void startRace(){ //You choose your car-Ferrari NeedForSpeedCarmyracingcar= new Ferrari(); System.out.println(myracingcar.startEngine()); System.out.println(myracingcar.accelerate()); } } class Race has a dependency to class Ferrari The class Race has a dependency to class Ferrari. Now the car modules are ready. The race is ready. Let’s ask the Participant to start the race. The participant is ready to use Ferrari.
  • 17. // Participant.java package spring.test.santosh.game; public class Participant{  public static void main(){ //You choose your car-Ferrari Racerace = new Race(); race.startRace(); } } Compile all the programs. And run Participant. The output would be: Now it looks pretty good. The Participant run the car Ferrari. See the next slide for what is the problem here…………………………………………
  • 18. The participant wants to use the another car: McLaren McLaren is best for grip so he chosed this car for the race. But hey… Ohhhhnooooooooooo.... It's not really pretty good because of the Problem here is:the object of new Ferrari is hard coded. So to use McLaren, it should be: NeedForSpeedCarmyracingcar = new Ferrari(); NeedForSpeedCarmyracingcar = new McLaren(); So at any instance the participant wants to choose another car, it needs the code change in Race.java. If the participant wants to change to Fordfrom McLaren, then it must be: NeedForSpeedCarmyracingcar = new McLaren(); NeedForSpeedCarmyracingcar = new Ford();
  • 19.
  • 20. asetter MethodSo let’s restructure the classes. You need to follow few steps to work with Spring IOC.
  • 21. Step 1: Add property for the object you need to inject and write setter method. //Race.java package spring.test.santosh.game;   public class Race{  NeedForSpeedCarmyracingcar;   public void setMyracingcar(NeedForSpeedCarmyracingcar){ this.myracingcar= myracingcar; } public void startRace(){ //You don’t create the object of any car type//NeedForSpeedCarmyracingcar = new Ferrari(); System.out.println(myracingcar.startEngine()); System.out.println(myracingcar.accelerate()); } } Newly added. Through the setter method the object will be injected into Race class. Now we need Spring Framework to provide the Dependent Injection into the class Race. So to use Spring, download the Spring Framework from http://www.springsource.org/download After you download set the library files into the classpath.
  • 22. Step 2: Create the Spring Configuration XML file. This will contain the entries for the dependency injection. The object will be referred as a bean. We can give any name for this XML. Here we will name as: SpringConfig.xml <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="race" class="spring.test.santosh.game.Race"> <property name="myracingcar"> <ref local="ferrari"/> </property> </bean> <bean id="ferrari" class="spring.test.santosh.game.Ferrari" /> <bean id="ford" class="spring.test.santosh.game.Ford" /> <bean id="mclaren" class="spring.test.santosh.game.McLaren" /> <bean id="jaguar" class="spring.test.santosh.game.Jaguar" /> </beans> myracingcar is the property in Race.java. Through the setter method the object is injected into Race.java You can change the ref as per the car chosen by the participant from the below bean id list. These are the objects injected dynamically. The objects are created by IOC container and injected into Race.java
  • 23. Step 3: Write the client. It must use the config springconfig.xml. packagespring.test.santosh.race.game;   importorg.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext;   publicclassParticipant{ publicstaticvoid main(String args[]){ ApplicationContextctx = newFileSystemXmlApplicationContext("springconfig.xml"); Race race = (Race) ctx.getBean("race"); race.startRace(); } } The instance of Ferrari will be created. Now run Driver and see the output…
  • 24. Now we will change the springconfig.xml to choose McLaren car instead of Ferrari. We will not touch any of the java file… And we will see the output after that… <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="race" class="spring.test.santosh.game.Race"> <property name="myracingcar"> <ref local="mclaren"/> </property> </bean> <bean id="ferrari" class="spring.test.santosh.game.Ferrari" /> <bean id="ford" class="spring.test.santosh.game.Ford" /> <bean id="mclaren" class="spring.test.santosh.game.McLaren" /> <bean id="jaguar" class="spring.test.santosh.game.Jaguar" /> </beans> Now ref is changed to "mclaren" from "ferrari". Output after you runDriver.class
  • 25. To download the source code project for this example, you can choose the link*: NeedForSpeed.zip (Standalone Application) *athttp://springs-download.blogspot.com
  • 26.
  • 27. Bean factory methods for accessing application components.And many more… Now we will see how ApplicationContext is used to load the file resources. Here we are loading the Spring Config file -springconfig.xml
  • 28.
  • 29. collects all the bean id’s as defined in springconfig.xml. Say: race, ferrari, ford, McLaren.
  • 30.
  • 31. ApplicationContextinDifferent Applications/java Frameworks Till now we learned how we configure the Spring IOC (Independent Injection) by configuring the XML configuration file. So in the example you can notice the below line in Participant.java: ApplicationContextctx = newFileSystemXmlApplicationContext("springconfig.xml"); Here we are creating the object of FileSystemXMLApplicationContext by providing the XML name as argument. Our application is a stand-alone application and so simply we could use new FileSystemXMLApplicaitonContext(<XML file path>). Also this class constructor is helpful to create the Spring application context for test harness or unit testing. But there are different applications say Web application such as servlets/jsp's, Enterpriseapplications such as EJBs with Java Frameworks such as Struts, JSF etc. So in this section we will see how we can configure Springs IOC and how we will get the ApplicationContext in client.
  • 32. Servlet andSprings I hope you are good in writing the Servlet and JSP application. So here I am not going to put how to build the servlet application and deploy it in the server. Just recall the directory structure in your servlet project: Now we will see where we can store our spring config XML file and how it could be accessible in the servlet classes. Note: It is very important that you don’t be confused this example with Springs MVC. We will see the Spring MVC latter. So now you concentrate how you will get the Spring ApplicationContext object in Servlet.
  • 33. Achieving Springs Application Context in Servlet You can get the ApplicationContext of Spring in Servlet in 3 ways: Through new ClassPathXmlApplicationContext("<relative path of config XML>") Through new XmlWebApplicationContext() Through WebApplicationContextUtils.getWebApplicationContext(ServletContext);
  • 34. Using ClassPathXmlApplicationContext The ClassPathXmlApplicationContextconstructor searches the given XML file from the classes folder in WEB-INF folder. Just you need to follow some basic steps. step 1: Create the spring config XML file. step 2: Name that file whatever you want. For example, in our example we named as: myspringconfig.xml step 3: Put this xml file in WEB-INF/classes folder while youare deploying. (If you use any editor like Eclipse, just put the XML in src folder, so when you build your project, it will automatically generate the same copy of XML along with the .class files in WEB-INF/classes folder.) Step 4: In servlet you can simply create the object of ClassPathXmlApplicationContextby passing the file relative path in constructor. E.g. ApplicatonContextctx = new ClassPathXmlApplicationContext("spring/test/santosh/config/myspringconfig.xml"); Herespring.test.santosh.configis a package created and stored the myspringconfig.xmlin this package. Let’s see the servlet example.
  • 35.
  • 36. The Spring Configfile – myspringconfig.xml will also remain unchanged. But you create a new packagespring.test.santosh.config and place this XML there.
  • 37.
  • 38. In the service (doPost) method, get the Bean public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Race race = (Race) ctx.getBean("race"); PrintStream out = new PrintStream(response.getOutputStream()); race.startRace(out); } Output To download the source code for this project for this example, you can choose the link*: NeedForSpeedServletOne.zip (Using ClassPathXmlApplicationContext) *athttp://springs-download.blogspot.com
  • 39. UsingXmlWebApplicationContext The XmlWebApplicationContextconstructor searches the applicationContext.xmlfile fromthe WEB-INF folder where the web.xml is located. See the constructor, you don’t pass the XML file name. So to get the context you simply use as: XmlWebApplicationContextctx = new XmlWebApplicationContext(); ServletContextservletContext = servletConfig.getServletContext(); ctx.setServletContext(servletContext); ctx.refresh(); Race race = (Race) ctx.getBean("race"); XML File searches the applicationContext.xml from WEB-INF folder and then loads the bean classes. To download the source code for this project for this example, you can choose the link*: NeedForSpeedServletTwo.zip (Using XmlWebApplicationContext) *athttp://springs-download.blogspot.com
  • 40. UsingWebApplicationContextUtils.getWebApplicationContext(ServletContext) When you use WebApplicationContextUtils.getWebApplicationContext(servletContext), you need to add ContextLoaderListener in web.xml as: <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> And when you use ServletContextservletContext = config.getServletContext(); ctx= WebApplicationContextUtils.getWebApplicationContext(servletContext); Race race = (Race) ctx.getBean("race"); XML File searches the applicationContext.xml from WEB-INF folder and then loads the bean classes.
  • 41. So now you understood that, you can use ApplicationContextctx= WebApplicationContextUtils.getWebApplicationContext(ServletContextservletCtx); For that you need to add the ContextLoaderListenerclass in web.xml under the tag <listener> To download the source code for this project for this example, you can choose the link*: NeedForSpeedServletThree.zip (Using WebApplicationContextUtils) *athttp://springs-download.blogspot.com
  • 42.
  • 43.
  • 44. And the second is, adding the parameters in <context-param> TAG with param-name as contextConfigLocation.xml.<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/springconfig.xml, /WEB-INF/racingcar.xml </param-value> </context-param>
  • 45. Here you need not worry on updating/changing the XML file names in the java file such as ParticipantServlet.java. When you add any extra more spring config xml, just you need to update in web.xml under the tag <param-value> as shown in the previous slide. To download the source code for this project for this example, you can choose the link*: NeedForSpeedServletFour.zip (Using multiple spring config XMLs) *athttp://springs-download.blogspot.com
  • 46.
  • 47. Do you have Questions ? Please write to:santosh.bsil@yahoo.co.in