You are on page 1of 6

What is Spring?

Spring is a set of libraries, frameworks and APIs that support Java "best practices" for ideas like IOC, I and AOP! What is IOC (Inversion Of Control)? "his is the #oll$wood principle or " on%t call us, we%ll call $ou!" "his basicall$ means that through ependenc$ In&ection, Ob&ects will be provided references to other Ob&ects created b$ Spring at 'untime! Callbacks are then used for interaction between Ob&ects! What is DI (Dependency Injection)? Ob&ects are given their dependencies at creation time b$ some e(ternal entit$ that coordinates each ob&ect in the s$stem! "he advantage is that information present onl$ at 'untime can be acted upon b$ the s$stem! What are 7 benefits of "Inversion Of Control"? )! IOC minimi*es the amount of code in $our application! +! ,ith IOC containers $ou do not care about how services are created or how $ou get references to the ones $ou need! -ou can also easil$ add additional services b$ adding a new constructor or a setter method with little or no e(tra configuration! .! IOC /akes $our application more testable b$ not re0uiring an$ singletons or J1 I lookup mechanisms in $our unit test cases! 2! IOC containers make unit testing and switching implementations ver$ eas$ b$ manuall$ allowing $ou to in&ect $our own ob&ects into the ob&ect under test! 3! 4oose coupling is promoted with minimal effort and least intrusive mechanism! "he factor$ design pattern is more intrusive because components or services need to be re0uested e(plicitl$ whereas in IOC the dependenc$ is in&ected into the appropriate piece of code! Also some containers promote the designing to interfaces rather than to implementations design concept b$ encouraging managed ob&ects to implement a well defined service interface! 5! IOC containers support eager instantiation and la*$ loading of services! 6! Containers also provide support for instantiation of managed ob&ects, c$clical dependencies, life c$cles management, and dependenc$ resolution between managed ob&ects etc! What is spect Orientated !rogra""ing ( O!)? In computing, aspect oriented programming 7AOP8 is a programming paradigm which aims to increase modularit$ b$ allowing the separation of cross cutting concerns! #ive e$a"ples of cross c%tting concerns& 4ogging or "ransaction /anagement are e(amples as the$ are likel$ to be utili*ed in a wide variet$ of places throughout the various patterns in the s$stem! What is a 'ean (actory? A 9ean:actor$ is like a factor$ class that contains a collection of beans! "he 9ean:actor$ holds 9ean efinitions of multiple beans within itself and then instantiates the bean whenever asked for b$ clients! 9ean:actor$ is able to create associations between collaborating ob&ects as the$ are instantiated! "his removes the burden of configuration from bean itself and the beans client! 9ean:actor$ also takes part in the life c$cle of a bean, making calls to custom initiali*ation and destruction methods! What is an pplication Conte$t?

A bean factor$ is fine for simple applications, but to take advantage of the full power of the Spring, $ou ma$ want to move up to Spring%s more advanced container, the application conte(t! On the surface, an application conte(t is same as a bean factor$!9oth load bean definitions, wire beans together, and dispense beans upon re0uest! It also provides; < A means for resolving te(t messages, including support for internationali*ation! < A generic wa$ to load file resources! < =vents to beans that are registered as listeners! What is the difference bet)een 'ean (actory and pplication Conte$t ? An application conte(t offers much more than a 9ean :actor$! )! Application conte(ts provide a means for resolving te(t messages, including support for i)>n of those messages! +! Application conte(ts provide a generic wa$ to load file resources, such as images! .! Application conte(ts can publish events to beans that are registered as listeners! 2! Certain operations on the container or beans in the container, which have to be handled in a programmatic fashion with a bean factor$, can be handled declarativel$ in an application conte(t! 3! Application Conte(ts facilitate Spring?s 'esource interface which is a fle(ible generic abstraction for handling low level resources! An application conte(t itself is a 'esource4oader, #ence the$ provide an application with access to deplo$ment specific 'esource instances! 5! /essageSource support; "he application conte(t implements /essageSource, an interface used to obtain locali*ed messages, with the actual implementation being pluggable! What are the co""on i"ple"entations of the pplication Conte$t ? "he three commonl$ used implementations of %Application Conte(t% are )! ClassPath@mlApplicationConte(t ; It 4oads conte(t definition from an @/4 file located in the classpath, treating conte(t definitions as classpath resources! "he application conte(t is loaded from the application%s classpath b$ using the code ! ApplicationConte(t conte(t A new ClassPath@mlApplicationConte(t7"bean!(ml"8B +! :ileS$stem@mlApplicationConte(t ; It loads conte(t definition from an @/4 file in the files$stem! "he application conte(t is loaded from the file s$stem b$ using the code ! ApplicationConte(t conte(t A new :ileS$stem@mlApplicationConte(t7"bean!(ml"8B .! @ml,ebApplicationConte(t ; It loads conte(t definition from an @/4 file contained within a web application! What does a typical Spring application loo* li*e? "$picall$ we would need the following; < An interface that defines the functions! < An Implementation that contains properties, its setter and getter methods, functions etc!, < Spring AOP 7Aspect Oriented Programming8 < A @/4 file called Spring configuration file! < Client program that uses the function! What is the typical 'ean life cycle in Spring 'ean (actory Container? )! "he spring container finds the bean?s definition from the @/4 file and instantiates the bean! +! Csing I, spring populates all of the properties as specified in the bean definition! .! If the bean implements the 9ean1ameAware interface, the factor$ calls set9ean1ame78 passing the bean?s I !

2! If the bean implements the 9ean:actor$Aware interface, the factor$ calls set9ean:actor$78, passing an instance of itself! 3! If there are an$ 9eanPostProcessors associated with the bean, their post Process9eforeInitiali*ation78 methods will be called! 5! If an init method is specified for the bean, it will be called! 6! :inall$, if there are an$ 9eanPostProcessors associated with the bean, their postProcessAfterInitiali*ation78 methods will be called! What is "eant by 'ean )iring? "he act of creating associations between application components 7beans8 within the Spring container is referred to as 9ean wiring! What is "eant by %to )iring? "he Spring container is able to autowire relationships between collaborating beans! "his means that it is possible to automaticall$ let Spring resolve collaborators 7other beans8 for $our bean b$ inspecting the contents of the 9ean:actor$! What is Delegating+ariable,esolver? elegatingDariable'esolver is an e(tension of the standard JS: managed beans mechanism which lets $ou use JS: and Spring together! -o) does one integrate a Str%ts application )ith Spring? "wo options; )! Configure Spring to manage $our Actions as beans, using the Conte(t4oaderPlugin, and set their dependencies in a Spring conte(t file! +! Subclass Spring%s ActionSupport classes and grab $our Spring managed beans e(plicitl$ using a get,ebApplicationConte(t78 method! What are e$a"ples of O,./s Spring s%pports? #ibernate i9atis JPA 7Java Persistence API8 "op4ink J O 7Java ata Ob&ects8 OJ9 What are the 0 )ays to access -ibernate %sing Spring ? IOC with a #ibernate"emplate and Callback or an e(tension of #ibernate aoSupport combined with the application of an AOP Interceptor -o) does one integrate Spring and -ibernate %sing -ibernateDaoS%pport? Cse Spring?s Session:actor$ called 4ocalSession:actor$! "he integration process is of . steps! )! Configure the #ibernate Session:actor$ +! =(tend $our AO Implementation from #ibernate aoSupport .! ,ire in "ransaction Support with AOP What are the bean scopes in the Spring (ra"e)or*? )! Singleton +! Protot$pe .! 'e0uest 7web aware applicationConte(t onl$8 2! Session 7web aware applicationConte(t onl$8 3! Elobal Session 7web aware applicationConte(t onl$8 Singleton Scopes a single bean definition to a single ob&ect instance per Spring IoC container! Protot$pe Scopes a single bean definition to an$ number of ob&ect instances! 'e0uest

Scopes a single bean definition to the lifec$cle of a single #""P re0uestB that is each and ever$ #""P re0uest will have its own instance of a bean created off the back of a single bean definition! Onl$ valid in the conte(t of a web aware Spring ApplicationConte(t! Session Scopes a single bean definition to the lifec$cle of a #""P Session! Onl$ valid in the conte(t of a web aware Spring ApplicationConte(t! Elobal Session Scopes a single bean definition to the lifec$cle of a global #""P Session! "$picall$ onl$ valid when used in a portlet conte(t! Onl$ valid in the conte(t of a web aware Spring ApplicationConte(t! In the conte$t of O! )hat is a 1oin !oint? A point during the e(ecution of a program, such as the e(ecution of a method or the handling of an e(ception! In Spring AOP, a &oin point alwa$s represents a method e(ecution! What is O! dvice? Action taken b$ an aspect at a particular &oin point! ifferent t$pes of advice include "around," "before" and "after" advice! /an$ AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors "around" the &oin point! What are the 2 types of O! dvice? )! 9efore +! After 'eturning .! After "hrowing 2! After :inall$ 3! Around

9efore Advice that e(ecutes before a &oin point, but which does not have the abilit$ to prevent e(ecution flow proceeding to the &oin point 7unless it throws an e(ception8! After 'eturning Advice to be e(ecuted after a &oin point completes normall$; for e(ample, if a method returns without throwing an e(ception! After "hrowing Advice to be e(ecuted if a method e(its b$ throwing an e(ception! After :inall$ Advice to be e(ecuted regardless of the means b$ which a &oin point e(its 7normal or e(ceptional return8! Around Advice that surrounds a &oin point such as a method invocation! "his is the most powerful kind of advice! Around advice can perform custom behavior before and after the method invocation! It is also responsible for choosing whether to proceed to the &oin point or to shortcut the advised method e(ecution b$ returning its own return value or throwing an e(ception What are the types of 3ransaction .anage"ent Spring s%pports? Programmatic and eclarative transaction management! What are the 4 benefits of the Spring (ra"e)or*5s transaction "anage"ent ? )! It provides a consistent programming model across different transaction APIs such as J"A, J 9C, #ibernate, JPA, and J O! +! It supports declarative transaction management! .! It provides a simpler API for programmatic transaction management than a number of comple( transaction APIs such as J"A! 2! It integrates ver$ well with Spring%s various data access abstractions! Why do "ost %sers of the Spring (ra"e)or* choose declarative transaction "anage"ent?

/ost users of the Spring :ramework choose declarative transaction management because it is the option with the least impact on application code, and hence is most consistent with the ideals of a non invasive lightweight container! 6$plain the si"ilarities and differences bet)een 61' C.3 and the Spring (ra"e)or*5s declarative transaction "anage"ent ? "he basic approach is similar; < It is possible to specif$ transaction behavior 7or lack of it8 down to individual method level! < It is possible to make a set'ollbackOnl$78 call within a transaction conte(t if necessar$! "he differences are; < Cnlike =J9 Container /anaged "ransactions, which are tied to J"A, Spring%s declarative transaction management works in an$ environment! It can work with J 9C, J O, #ibernate or other transactions under the covers, with configuration changes onl$ < Spring enables declarative transaction management to be applied to an$ class, not merel$ special classes such as =J9s! < Spring offers declarative rollback rules; this is a feature with no =J9 e0uivalent! 9oth programmatic and declarative support for rollback rules is provided! < Spring gives $ou an opportunit$ to customi*e transactional behavior, using AOP! ,ith =J9 C/", $ou have no wa$ to influence the container%s transaction management other than set'ollbackOnl$78! < Spring does not support propagation of transaction conte(ts across remote calls, as do high end application servers! When to %se progra""atic and declarative transaction "anage"ent? Programmatic transaction management is usuall$ a good idea onl$ if $ou have a small number of transactional operations, otherwise $ou are probabl$ going to want declarative transaction management! It keeps transaction management out of business logic, and is not difficult to configure! 3ell "e abo%t the Spring D O s%pport& "he ata Access Ob&ect 7 AO8 support in Spring is aimed at making it eas$ to work with data access technologies like J 9C, #ibernate or J O in a consistent wa$! "his allows one to switch between the persistence technologies fairl$ easil$ and it also allows one to code without worr$ing about catching e(ceptions that are specific to each technolog$! What are the e$ceptions thro)n by the Spring D O classes? Spring AO classes throw e(ceptions which are subclasses of ataAccess=(ception7org!springframework!dao! ataAccess=(ception8!Spring provides a convenient translation from technolog$ specific e(ceptions like SF4=(ception to its own e(ception class hierarch$ with the ataAccess=(ception as the root e(ception! "hese e(ceptions wrap the original e(ception! What is S786$ception3ranslator? SF4=(ception"ranslator, is an interface to be implemented b$ classes that can translate between SF4=(ceptions and Spring%s own data access strateg$ agnostic org!springframework!dao! ataAccess=(ception What is Spring5s 1dbc3e"plate? Spring%s Jdbc"emplate is the central class which interacts with a database through J 9C! Jdbc"emplate provides man$ convenience methods for doing things such as converting database data into primitives or ob&ects, e(ecuting prepared and callable statements, and providing custom database error handling! Jdbc"emplate template A new Jdbc"emplate7m$ ataSource8B

Concerning 1dbc3e"plate9 )hat is !reparedState"entCreator ? It%s one of the most common used interfaces for writing data to database! It has one method G createPreparedStatement7Connection8 'esponsible for creating a PreparedStatement! oes not need to handle SF4=(ceptions! Concerning 1dbc3e"plate9 )hat is S78!rovider ? SF4Provider is a class with one method, getS0l78! "$picall$ this is implemented b$ PreparedStatementCreator implementers and is useful for debugging! Concerning 1dbc3e"plate9 )hat is ,o)Callbac*-andler ? "he 'owCallback#andler interface e(tracts values from each row of a 'esultSet! #as one method G process'ow7'esultSet8 Called for each row in 'esultSet! "$picall$ stateful! What are the five "odes of Spring5s a%to)iring f%nctionality? )! no 1o autowiring is performed! All references to other beans must be e(plicitl$ in&ected! "his is the default mode! +! b$1ame 9ased on the name of a propert$, a matching bean name in the IoC container will be in&ected into this propert$ if it e(ists! .! b$"$pe 9ased on the t$pe of class on a setter, if onl$ one instance of the class e(ists in the IoC container it will be in&ected into this propert$! If there is more than one instance of the class a fatal e(ception is thrown! 2! constructor 9ased on a constructor argument%s class t$pes, if onl$ one instance of the class e(ists in the IoC container it will be used in the constructor! 3! autodetect If there is a valid constructor, the constructor autowiring mode will be chosen! Otherwise if there is a default *ero argument constructor the b$"$pe autowiring mode will be chosen!

You might also like