0% found this document useful (0 votes)
21 views

Spring

Spring module

Uploaded by

varun varun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
21 views

Spring

Spring module

Uploaded by

varun varun
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 21
What is loc ? IOC: Inversion of Control > 10C isa principle which is used to manage and collaborate dependencies among the objects in the application. Managing ( Creating the object) , collaborate (Injecting the object) > In Spring, OC is responsible for Dependency Injection. UserDe ‘Servic Repro freporsersie oa ‘AdminService K——|Adminserviet} UserDae object injected into UserService, UserService objet injected into UserServlet (am _/ annotations) Programmer are responsible to Configuration e ‘write configuration g Inspring boot-auto conguration aa - Classes roc [> soring seans Note: Fr IOC we need to pas ava Classes + Configuration as input then IOC wil produce Spring Beans. Soring Bean: The class which is managed by IOs called as Spring Bean How start IOC container? => We can start in 2 ways 1) BeanFactory (1) (outdated) 2) ApplicationContext (|) (recommended) (Interface) (class ClassPathXmlApplicationContext) ApplicationContext ctxt = new ClassPathxmlApplicationContext( String xmiFile) fo Benaensecce ————————————n 2 totaal CPoarimnensim ant — PERGRETRER Semunse meep://moven.opocne.org/run/s.0.8 2 Gmoselvereson>4.0.8¢/modelversions 3 thik /groupTad> 4 OC-APP«/ ar2sfactid> 5 SMAPSHOT< vers on> 6 8» dependencies? 18> Bi 12 Greitactidvepringreontert/artifactig> 23 Gersion$.3:25c/version> i esoentone PaymentService doPayment(..) Tayment.paynent; LI Debit credit] Sodex] [Amex ‘App Test public class Paymentservice { private 1Payment payment public PaymentService() q System.out.printtn(“PaymentService Constrictor Executed"); y public PaymentService(IPayment payment) 4 System.out.printtn("1 parameter PaymentService Constructor Executed"); this.paymentspayment } public void setpaynent(rPayment payment) 4 System.out.printin("Setter method called”); this.payment=payent } public void doPayment (Double billtnt) jayment..processPayment(billémt); if(status) ¢ System.out.println(“Recipt printing....") + else « System.out. println("Card Dectined. y } } public interface TPayment { public boolean processPayment (Double pubite class DebttCardPayment snplenents billamt); ‘TPayment{ a public DebitCardPayment() it Systen.out.printtn(“DebttCardPayment te ae eee ees Syston out. prentn(‘evtcardrom Irayment ( } public boolean processPayment (Double pubtic CreditcardPayment() bintawe) ¢ . System.out.printLn(“Debitcard Payment Systen.out.printtn(“Credttcardbaynent Saab Constructor Executed"); return trues y public boolean processPaynent(Double billémt) ¢ | System.out.println(” Credict ¢ Payment 3 Sucessfull...")3 return trues } } public class Test { public static void main(String[] args) { J/ Start IOC container using ApplicationContext ApplicationContext context=new ClassPathXmlApplicationContext("Beans. xml") // How to get the object from the TOC PaymentService service=context.getBean(PaymentService.class); service.doPayment (200.00); i - B>W OOD W0e+O-O-a Awe S 4b hiewer ora sremrtyow i Gtontigee = 8 Bieocamiemet Giperetne Dteeete sts aa ea Google stpteencenoutenan! fe x(n eee ss) (sptghat) aoe) (pe) (om) (Sa) (sap) (Hee) (Sn Sotasoe © iene en mtcndeesin f 40. XML Senema-bated confguation ‘Te cquale Hem eA Sehons sto wause thaaptaer Gramime =D) ReECeynn Boman armen entacarets Atyenine ta “| echeans anlnae"http://hom.springfranever.org/schena/beans* 3 etapa ney nna ymeascene nee 5 fier eae ser caeraheenWiees/atuasirtane WiGaT TAG aptagrrescseH 5 ce bean definitions hare ==> BS cibeane> setter injection Note: ref att ute represents which object should be “ —> public class car { private TEngine eng; public Car() { System.out.printin(" Car constructor"); public car(ZEngine ene ( Systen. out. printin("Ca Constructor"); ‘this.eng = engs + param - public void drive() { int status = eng.start()s if (status >= 1) { System.out.printIn( "Journey Starte } else { System.out printIn("Engine Trouble... y } ? public interface Engine { public int start(); y public class DieselEngine implements TEngine{ public Dieselengine() { System.out. println( ‘DteseLEngine. Constructor. y public int start() { System.out.println("DieseLEngine starte return 35 } Y public class Petrolengine implements Iengine { public PetrolEngine() { System.out. printin( ‘PetroLEngine. Constructor. y public int start() ¢ System.out.println("PetrotEngine Started... ")s return 15 y y | public class Test { public static void main(String[] args) { ApplicationContext ctxt_= new ClassPathxmlApplicationContext("Beans. xm") Car cl = ctxt.getBean(Car.class); cl.drive(); + ea ( cbeans xmlns="http://uww. springframework.org/schema/beans" | xmlns:xsi="http://umw.w3.0rg/2001/XMLSchema- instance" xsi:schemaLocation=" http://www. springframework.org/schema/beans http://www. springfranework.org/schema/beans/spring-beans.xsd"> | Spring Bean Scopes: -> Bean Scope will decide how many objs should be created for Spring Bean class. > We have 4 types of scopes 4) singleton (default) 2) prototype 3) request 4) session *\ xmins:xsi="http://www.w3.org/2001/XMLSchem; xsi:schemaLocation=" http://mww.springframework.org/schema/beans http://www. springframework.org/schema/beans/spring-beans.xsd"> ‘ « Ces © public class Test { public static void main(String{] args) { ApplicationContext ctxt = new ClassPathxmlApplicationContext("Beans.xm"); Car c1 = ctxt.getBean(Car.class); Ic1.drive(); ‘System. out.printin(cl.hashCode()); Car c2 = ctxt.getBean(Car.class); ‘System.out.printin(c2.hashCode()); UTE-8"2> nttp://www. springframework.org/schema/beans" "http:/ Awww.w3.0rg/2001/XMLSchem: xsi:schemaLocation=" http://mww.springframework.org/schema/beans http://www. springframework.org/schema/beans/spring-beans.xsd"> *\ public class Test { public static void main(Stringf] args) { ApplicationContext ctxt = new Cee eerie ClassPathxmlApplicationContext("‘Beans.xm"); ore ipeesie ens } public class Test { Public static void main(Stringl] args) { ‘ApplicationContext ctxt = new ClassPathXmlApplicationContext(""Beans.xml"); poor Soares Car ¢1 = ctxt.getBean(Car.class } eer eee ere ke ed eens "UTE-8"?> http://www.springframework.org/schema/beans” "http: //www.w3.org/2001/XMLSchema- instance” xsisschemaLocation=" http://www.springframework.org/schema/beans http://www. springframework.org/schema/beans/spring-beans.xsd"> \\ sfbeans> / public class Test { public static void main(stringf} args) { ‘ApplicationContext ctxt = new ClassPathXmlApplicationContext(""Beans.xml"); Car cl = ctxt.getBean(Car.class); Merdrive(); System.out.printin(c1.hashCode()); Car c2 = ctxt.getBean(Car.class); ‘System.out.printin(c2.hashCode()); When user login---> one time session object will be created till user logout we will use the same session object (Singleton) ores Peer tenes Ree een cee ey eee ere eee) ec eee ne eed poe eens ea 2. For every customer do | need to create a Pete ea een ed pemeey) eee ‘Token will be same for ll the customer or Pee ones) CO een

You might also like