Your SlideShare is downloading. ×
0
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Spring IOC
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Spring IOC

180

Published on

Inversion of Control, Dependency Injection in Spring

Inversion of Control, Dependency Injection in Spring

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
180
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. The Spring Framework: A brief introduction to Inversion of Control Snehal Masne www.iSnehal.com
  • 2. What is Spring?2 Things: ◦ An Inversion of Control (IoC) Container ◦ Utilities that provide a consistent (and simple!) API to many other technologies (JDBC, ORM,AOP, Declarative Transactions, etc)
  • 3. Guiding Principles of Spring◦ Minimize dependencies on Spring◦ Minimize dependencies between all layers of anapplication.◦ All application code should be testable, without anapplication server or other complex environment◦ Fully factor the APIs (the 90% case should beaccomplished in one line of code!)
  • 4. What is Inversion of Control (IoC)?(besides yet another confusing term for a simple concept)IoC is all about Object dependencies.Traditional "Pull" approach: ◦ Direct instantiation ◦ Asking a Factory for an implementation ◦ Looking up a service via JNDI"Push" approach: ◦ Something outside of the Object "pushes" its dependencies into it. The Object has no knowledge of how it gets its dependencies, it just assumes they are there.The "Push" approach is called "Dependency Injection".
  • 5. The BookDemo Application:(A simple CRUD app) User Interface Domai n Service Layer Model Data Access Layer DB
  • 6. Pull Examplepublic class BookDemoServicePullImpl implements BookDemoService { public void addPublisherToBook(Book book) { BookDemoFactory factory = BookDemoFactory.getFactory(); BookDemoDao dao = factory.getBookDemoDao(); String isbn = book.getIsbn(); if (book.getPublisher() == null && isbn != null) { Publisher publisher = dao.findPublisherByIsbn(isbn); book.setPublisher(publisher); } }}
  • 7. Push Example(Dependency Injection)public class BookDemoServiceImpl implements BookDemoService { private BookDemoDao dao; public void addPublisherToBook(Book book) { String isbn = book.getIsbn(); if (book.getPublisher() == null && isbn != null) { Publisher publisher = dao.findPublisherByIsbn(isbn); book.setPublisher(publisher); } } public void setBookDemoDao(BookDemoDao dao) { this.dao = dao; }}
  • 8. BookDemoService Unit Test
  • 9. DefinitionsDependency Injection is the act of injecting dependencies into an Object.Inversion of Control is the general style of using Dependency Injection to wire together application layers.Hence Spring is an Inversion of Control container. That is, it is a container that handles Dependency Injection for you.
  • 10. Why is Dependency Injection better?2 reasons: ◦ Loose Coupling ◦ TestabilityLoose Coupling is improved because you dont hard-code dependencies between layers and modules. Instead you configure them outside of the code. This makes it easy to swap in a new implementation of a service, or break off a module and reuse it elsewhere.Testability is improved because your Objects dont know or care what environment theyre in as long as someone injects their dependencies. Hence you can deploy Objects into a test environment and inject Mock Objects for their dependencies with ease.
  • 11. How Spring does Inversion of Control◦ Write a configuration file in which you nameconcrete "beans" for the interfaces between yourlayers.◦ "Wire" the application together by stating whichbeans are dependent on each other.◦ Instantiate a Spring object called anApplicationContext. This is a type of bean factorythat will instantiate all your other beans and handledependency injection.
  • 12. Example Spring applicationContext.xml<beans> <bean id="bookDemoDao"class="com.bookdemo.dao.hibernate.BookDemoDaoHibernateImpl"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <bean id="bookDemoService" class="com.bookdemo.service.impl.BookDemoServiceImpl"> <property name="bookDemoDao"> <ref bean="bookDemoDao"/> </property> </bean></bean>
  • 13. Bootstapping the IoC containerTo start an app using IoC: ◦ Create an ApplicationContext object and tell it where applicationContext.xml is.ApplicationContext appContext = new ClassPathXmlApplicationContext("classpath*:applicationContext.xml");This just has to be done once on startup, and can be donein the main() method or whatever code bootstraps theapplication.
  • 14. Bootstapping the IoC containerThe appContext holds a single copy of each beandeclared in applicationContext.xml, so you could ask theContext for any bean by name:MyService service = (MyService)appContext.getBean("myService");But dont! That is a "Pull" technique that treats theApplicationContext like a Factory.Instead, make sure that applicationContext.xml connectsevery bean to every other bean that needs it. None of thebeans thus have a dependency on spring.jar
  • 15. Bootstapping the IoC containerFor web applications the situation is simpler:Web applications are bootstrapped by the webcontainer based on the web.xml file. Hence creatingan ApplicationContext on startup is as simple as asingle declaration in web.xml:<listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class></listener>
  • 16. A Spring app with no dependencieson Spring?When bootstrapping spring from web.xml, there areonly two pieces: ◦ The applicationContext.xml file ◦ A single tag within web.xmlNot a single line of Java code!Therefore, not one of your custom classes has adependency on spring.jar
  • 17. The BookDemo App
  • 18. What else has Spring got?Spring provides either implementations or fully-factored APIwrappers over these technologies: ◦ JDBC and DAOs ◦ ORM: Hibernate, iBatis, TopLink and others ◦ Declarative Transaction Support (without a full J2EE app server) ◦ Aspect-Oriented Programming ◦ Remote calls and Web Services (Axis) ◦ EJBs
  • 19. ResourcesSpring Website:http://www.springframework.org/Download Spring with sample applications:http://prdownloads.sourceforge.net/springframework/spring-framework-1.2.8-with-dependencies.zip? downloadRod Johnsons book on Spring:http://www.powells.com/biblio?PID=719&cgi=product&isbn=0764574833Wikipedia:http://en.wikipedia.org/wiki/Spring_Framework_%28Java%29EasyMock:http://www.easymock.org/Echo2 (Rich Web Inteface framework)http://www.nextapp.com/platform/echo2/echo/

×